From 5a98f42e8de696ab0d1f8f8a212c7e3cefa15ebc Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 22 Aug 2023 12:54:26 -0700 Subject: [PATCH 1/4] Upgrade CLI, restore prev status check value Summary: Follow-up to https://github.com/facebook/react-native/pull/38988. - Upgrade to RN CLI `12.0.0-alpha.10`. - Restore previous value check against `/status` response in test scripts (restored in above CLI alpha). Changelog: [Internal] Differential Revision: https://internalfb.com/D48432629 fbshipit-source-id: 538a4cbf52c3ef8e5951bd5a33c4d665dccba6e7 --- packages/community-cli-plugin/package.json | 4 +- .../React/Base/RCTBundleURLProvider.mm | 7 - .../react/devsupport/PackagerStatusCheck.java | 9 -- packages/react-native/package.json | 6 +- scripts/objc-test.sh | 22 +-- scripts/testing-utils.js | 6 - yarn.lock | 152 +++++++++--------- 7 files changed, 86 insertions(+), 120 deletions(-) diff --git a/packages/community-cli-plugin/package.json b/packages/community-cli-plugin/package.json index ae4ca20d5e05..881a5bc68fba 100644 --- a/packages/community-cli-plugin/package.json +++ b/packages/community-cli-plugin/package.json @@ -23,8 +23,8 @@ ], "dependencies": { "@react-native/dev-middleware": "^0.73.0", - "@react-native-community/cli-server-api": "12.0.0-alpha.9", - "@react-native-community/cli-tools": "12.0.0-alpha.9", + "@react-native-community/cli-server-api": "12.0.0-alpha.11", + "@react-native-community/cli-tools": "12.0.0-alpha.11", "@react-native/metro-babel-transformer": "^0.73.11", "chalk": "^4.0.0", "execa": "^5.1.1", diff --git a/packages/react-native/React/Base/RCTBundleURLProvider.mm b/packages/react-native/React/Base/RCTBundleURLProvider.mm index 14302e11ab7a..7b5dd2b3641f 100644 --- a/packages/react-native/React/Base/RCTBundleURLProvider.mm +++ b/packages/react-native/React/Base/RCTBundleURLProvider.mm @@ -107,13 +107,6 @@ + (BOOL)isPackagerRunning:(NSString *)hostPort scheme:(NSString *)scheme dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); NSString *status = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; - - // TODO(huntie): Temporary string match in JSON response. We will revert this - // endpoint back to "packager-status:running" in the next CLI alpha. - if ([status containsString:@"\"status\":\"running\""]) { - return true; - } - return [status isEqualToString:@"packager-status:running"]; } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.java index 6c990430f7f1..0814a12dea93 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/devsupport/PackagerStatusCheck.java @@ -81,15 +81,6 @@ public void onResponse(Call call, Response response) throws IOException { String bodyString = body.string(); // cannot call body.string() twice, stored it into variable. // https://github.com/square/okhttp/issues/1240#issuecomment-68142603 - - // TODO(huntie): Temporary string match in JSON response. We - // will revert this endpoint back to "packager-status:running" - // in the next CLI alpha. - if (bodyString.contains("\"status\":\"running\"")) { - callback.onPackagerStatusFetched(true); - return; - } - if (!PACKAGER_OK_STATUS.equals(bodyString)) { FLog.e( ReactConstants.TAG, diff --git a/packages/react-native/package.json b/packages/react-native/package.json index f7649e227b6e..6c511b7f0eb6 100644 --- a/packages/react-native/package.json +++ b/packages/react-native/package.json @@ -92,9 +92,9 @@ }, "dependencies": { "@jest/create-cache-key-function": "^29.6.3", - "@react-native-community/cli": "12.0.0-alpha.9", - "@react-native-community/cli-platform-android": "12.0.0-alpha.9", - "@react-native-community/cli-platform-ios": "12.0.0-alpha.9", + "@react-native-community/cli": "12.0.0-alpha.11", + "@react-native-community/cli-platform-android": "12.0.0-alpha.11", + "@react-native-community/cli-platform-ios": "12.0.0-alpha.11", "@react-native/assets-registry": "^0.73.0", "@react-native/community-cli-plugin": "^0.73.0", "@react-native/codegen": "^0.73.0", diff --git a/scripts/objc-test.sh b/scripts/objc-test.sh index 7b08d79e8400..4ba9f4eea035 100755 --- a/scripts/objc-test.sh +++ b/scripts/objc-test.sh @@ -73,26 +73,6 @@ waitForWebSocketServer() { echo "WebSocket Server is ready!" } -waitForPackagerAlt() { - local -i max_attempts=60 - local -i attempt_num=1 - - # TODO(huntie): Temporary string match in JSON response. We will revert - # this endpoint back to "packager-status:running" in the next CLI alpha. - until curl -s http://localhost:8081/status | grep "\"status\":\"running\"" -q; do - if (( attempt_num == max_attempts )); then - echo "Packager did not respond in time. No more attempts left." - exit 1 - else - (( attempt_num++ )) - echo "Packager did not respond. Retrying for attempt number $attempt_num..." - sleep 1 - fi - done - - echo "Packager is ready!" -} - runTests() { # shellcheck disable=SC1091 source "$ROOT/scripts/.tests.env" @@ -151,7 +131,7 @@ main() { # Start the packager yarn start --max-workers=1 || echo "Can't start packager automatically" & - waitForPackagerAlt + waitForPackager preloadBundlesRNTester preloadBundlesRNIntegrationTests diff --git a/scripts/testing-utils.js b/scripts/testing-utils.js index c89bf3d85aba..be4bc78b9c7f 100644 --- a/scripts/testing-utils.js +++ b/scripts/testing-utils.js @@ -117,12 +117,6 @@ function isPackagerRunning( silent: true, }).stdout; - // TODO(huntie): Temporary string match in JSON response. We will revert - // this endpoint back to "packager-status:running" in the next CLI alpha. - if (status.includes('"status":"running"')) { - return 'running'; - } - return status === 'packager-status:running' ? 'running' : 'unrecognized'; } catch (_error) { return 'not_running'; diff --git a/yarn.lock b/yarn.lock index 37db24adfe29..f8be8ce9a5ef 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2615,44 +2615,44 @@ optionalDependencies: npmlog "2 || ^3.1.0 || ^4.0.0" -"@react-native-community/cli-clean@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-clean/-/cli-clean-12.0.0-alpha.9.tgz#f5b9615e65860fec8fdc60d80944bc6f06b76726" - integrity sha512-o0lnATX3GNA4MK7aVBpJj5nulPi9BucWlavnVrGpBRLGsNca2js1KrlIVhYYSZaVlCwf71IZsEMYa/1KN8YiRQ== +"@react-native-community/cli-clean@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-clean/-/cli-clean-12.0.0-alpha.11.tgz#22a21bf5f937b9a58541612db3a231a5f044acd6" + integrity sha512-KU6nrnwAb7LeuPgDapTz/B7nFaII21cg0Dm90gII/+gwjTogsowIaoSAFLxK8DO83wn/vlJlJBxN4Brs/Wby3Q== dependencies: - "@react-native-community/cli-tools" "12.0.0-alpha.9" + "@react-native-community/cli-tools" "12.0.0-alpha.11" chalk "^4.1.2" execa "^5.0.0" prompts "^2.4.2" -"@react-native-community/cli-config@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-config/-/cli-config-12.0.0-alpha.9.tgz#bfe992b8abfb3f5e4ef578a5e984d52ef2cc605a" - integrity sha512-Kq/cAUITcZKGhA5HoN14ZRZrJen3Y+QWU2Zkyi2ho3nmgjUbCW5Y8AYERKL/uuEkWi8JB14+Ky8LJmfNbO1Rnw== +"@react-native-community/cli-config@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-config/-/cli-config-12.0.0-alpha.11.tgz#daa7425cc891857f8894d8636f8881933f159f07" + integrity sha512-a68/F4queh7MA7Kr/3+92SD2miZ+tmSjFjM8Lg8+3hEAoBi3rRk+6fgXtfJqpqQnaJDviTXIMZZyY0Sn1Ff2gw== dependencies: - "@react-native-community/cli-tools" "12.0.0-alpha.9" + "@react-native-community/cli-tools" "12.0.0-alpha.11" chalk "^4.1.2" cosmiconfig "^5.1.0" deepmerge "^4.3.0" glob "^7.1.3" joi "^17.2.1" -"@react-native-community/cli-debugger-ui@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-debugger-ui/-/cli-debugger-ui-12.0.0-alpha.9.tgz#5ac44925cd024882c39b056f62ec8712cf751252" - integrity sha512-l7Awe3TlBfnFmMQ2N+JoWSfYMIgHcySk056wI+tskc2OS2JJvnmHkASeHIrl0tnQuFcHluFxpK6nyvgkzcXJ7w== +"@react-native-community/cli-debugger-ui@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-debugger-ui/-/cli-debugger-ui-12.0.0-alpha.11.tgz#d4c5aae29d57291e1b12db40143e5bc8b94acd03" + integrity sha512-J45vZXwY5mW7mkHDrFJ3Iz5EDJnqUh0uX+ar+uOW5RdVzJ3aurn9mfmLb5txX04k+ee7+hmqF7OZue1eVrBepg== dependencies: serve-static "^1.13.1" -"@react-native-community/cli-doctor@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-doctor/-/cli-doctor-12.0.0-alpha.9.tgz#d27956902bc81f97e23d5dd15daa2ede668f507a" - integrity sha512-rTuCkcmMBmbFzJ9V8/7sipdM2ggjL2I04W5/Xn9OUwKVsHqydRaO9vyqOag+jwDgHMxHZO6OOr41nMQaa32z3w== +"@react-native-community/cli-doctor@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-doctor/-/cli-doctor-12.0.0-alpha.11.tgz#5d7515416a9c8144e4b84afdcb10b27e88b43bd0" + integrity sha512-yUPmi2wRcASIomvdUyWqwSo3F7B+EoUeMNFMxhleyIQ3HvnmZQi/kNLhKwjVEIPM7tDBMFi8dSTK27DTl/Pxhg== dependencies: - "@react-native-community/cli-config" "12.0.0-alpha.9" - "@react-native-community/cli-platform-android" "12.0.0-alpha.9" - "@react-native-community/cli-platform-ios" "12.0.0-alpha.9" - "@react-native-community/cli-tools" "12.0.0-alpha.9" + "@react-native-community/cli-config" "12.0.0-alpha.11" + "@react-native-community/cli-platform-android" "12.0.0-alpha.11" + "@react-native-community/cli-platform-ios" "12.0.0-alpha.11" + "@react-native-community/cli-tools" "12.0.0-alpha.11" chalk "^4.1.2" command-exists "^1.2.8" envinfo "^7.7.2" @@ -2668,47 +2668,48 @@ wcwidth "^1.0.1" yaml "^2.2.1" -"@react-native-community/cli-hermes@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-hermes/-/cli-hermes-12.0.0-alpha.9.tgz#a10b0795e3f29f65d055b480af6284981869bb43" - integrity sha512-CSHkkaUsdZpMSFhsCT5Xy6upl3HXB5jk06b8is8XObDHPxQGOt+U7GPuQ8O2O5Doa87mQ9y3uHVhFEmb0ANioQ== +"@react-native-community/cli-hermes@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-hermes/-/cli-hermes-12.0.0-alpha.11.tgz#1b5dd1c5e61f3d4c149cafe9e9c9bfd7b484d6e4" + integrity sha512-1DcR76pzp+pFflYUq+sleZedHWt3noK8ledGeRH5MJz9sBpPFD6GmpIa/tQP5gwLPZaHVGAkZs6l6FT6OoBgVA== dependencies: - "@react-native-community/cli-platform-android" "12.0.0-alpha.9" - "@react-native-community/cli-tools" "12.0.0-alpha.9" + "@react-native-community/cli-platform-android" "12.0.0-alpha.11" + "@react-native-community/cli-tools" "12.0.0-alpha.11" chalk "^4.1.2" hermes-profile-transformer "^0.0.6" ip "^1.1.5" -"@react-native-community/cli-platform-android@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-platform-android/-/cli-platform-android-12.0.0-alpha.9.tgz#609ff08357cbee51540c796f4df1bf6bdbd0cbdc" - integrity sha512-Q2eIOQ6iYyVrdjyB4pcSRqKvzp0S9130IIocLjvc/8+YmeU0bMoSLZ/uYAkVAoKD+VFP3Ry6US6CLzNvxFwDzw== +"@react-native-community/cli-platform-android@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-platform-android/-/cli-platform-android-12.0.0-alpha.11.tgz#550a27017a8909211fb3ad4ecb1d8baac6a66d61" + integrity sha512-bffvGJkRwhon5I5fIB1R/rZZlufJXbUddRxEjHi3hn0I0kwQ5ahgEBqRVMKyxtQRzRS5dDdFAm8CLqcSLrapWg== dependencies: - "@react-native-community/cli-tools" "12.0.0-alpha.9" + "@react-native-community/cli-tools" "12.0.0-alpha.11" chalk "^4.1.2" execa "^5.0.0" + fast-xml-parser "^4.2.4" glob "^7.1.3" logkitty "^0.7.1" -"@react-native-community/cli-platform-ios@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-platform-ios/-/cli-platform-ios-12.0.0-alpha.9.tgz#f0a126c192c1f4188a233e1f51dcf1030bd783ce" - integrity sha512-Nl1+MyCcpGKvkv8kxDlN4jvht4bIR8N2mrQwazgtZeOURd3Mn8spOe8cfLexSNKhBaHHcb0JdqWLWyujlQTlyw== +"@react-native-community/cli-platform-ios@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-platform-ios/-/cli-platform-ios-12.0.0-alpha.11.tgz#9a24a5bc96e4b1cdb0b80a00427590c932fb6e10" + integrity sha512-YMBeYMwcjYyvqimj6fGHtrkqwF83jR9ZaZ/Xovk7udEL1qxs3ktQUW0dWtyzNz7dlhDEEzoC501tty0boTOmBg== dependencies: - "@react-native-community/cli-tools" "12.0.0-alpha.9" + "@react-native-community/cli-tools" "12.0.0-alpha.11" chalk "^4.1.2" execa "^5.0.0" fast-xml-parser "^4.0.12" glob "^7.1.3" ora "^5.4.1" -"@react-native-community/cli-plugin-metro@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-plugin-metro/-/cli-plugin-metro-12.0.0-alpha.9.tgz#83a1906b8e3ad250910896ec450b6215d2dd88fe" - integrity sha512-4oQoKPPKRTLfgVKKaVTtJRVBZpyDZh6CfzMeQ1NuobQbf0IzvpypbDJlrVcDz/vzm2DjiZEvlD0D51XvErKpXg== +"@react-native-community/cli-plugin-metro@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-plugin-metro/-/cli-plugin-metro-12.0.0-alpha.11.tgz#bf51d906361e5cc01a211c6dd949dc291d4c7562" + integrity sha512-HPVXMNFmYfd3ljY48hQVNKy0scEJqvfhD/sjxAdrDc5H00AE3qr9XBSpCih3LcTLm2pVFrnEHU5dsUtNySR6sQ== dependencies: - "@react-native-community/cli-server-api" "12.0.0-alpha.9" - "@react-native-community/cli-tools" "12.0.0-alpha.9" + "@react-native-community/cli-server-api" "12.0.0-alpha.11" + "@react-native-community/cli-tools" "12.0.0-alpha.11" chalk "^4.1.2" execa "^5.0.0" metro "0.78.0" @@ -2716,13 +2717,13 @@ metro-core "0.78.0" readline "^1.3.0" -"@react-native-community/cli-server-api@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-server-api/-/cli-server-api-12.0.0-alpha.9.tgz#bde13526941e3f82daeecd264ce3dcc23846ec0b" - integrity sha512-z47N3CxYBhv027HN7fBvNAZNORx4VSOE0WHQS78iBc2X9IiJdo/wi6BA3OO3mHA9w9ZCGVE3ejz/89wxx0vbdg== +"@react-native-community/cli-server-api@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-server-api/-/cli-server-api-12.0.0-alpha.11.tgz#34b240d172f6b2d3e64a3e8cdf3ea82e48b88ce0" + integrity sha512-XfwiG0W+h5WoVvoXHHoEHtIgOSpWERMOp8CKKMi7c9oAKjUt/ayAn7M9PGcaCUVz2J43DgCaow8jKVZyfTXkxg== dependencies: - "@react-native-community/cli-debugger-ui" "12.0.0-alpha.9" - "@react-native-community/cli-tools" "12.0.0-alpha.9" + "@react-native-community/cli-debugger-ui" "12.0.0-alpha.11" + "@react-native-community/cli-tools" "12.0.0-alpha.11" compression "^1.7.1" connect "^3.6.5" errorhandler "^1.5.1" @@ -2731,10 +2732,10 @@ serve-static "^1.13.1" ws "^7.5.1" -"@react-native-community/cli-tools@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-tools/-/cli-tools-12.0.0-alpha.9.tgz#a8533a19e98696355826751597b7535cd1e70c3c" - integrity sha512-77XKc240Uc4wswhoF1bJm9EolX8sziGzz8z+vqgFKUob1igXvJ9oEIX+Tci8ScFgD/duY4/8ydKgYFehGf6KjA== +"@react-native-community/cli-tools@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-tools/-/cli-tools-12.0.0-alpha.11.tgz#fe719a8db55b727c90b31079f772a4e024c20d33" + integrity sha512-PDAfNUG4e6FpY4rTo0jFQ5p8gwHTZkLlNo/JEQgn3hvQsZk5LN2t0wwtEnaLKzZ9LEu9avZ/7MyiiRdW14fdOg== dependencies: appdirsjs "^1.2.4" chalk "^4.1.2" @@ -2746,27 +2747,27 @@ semver "^7.5.2" shell-quote "^1.7.3" -"@react-native-community/cli-types@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli-types/-/cli-types-12.0.0-alpha.9.tgz#73fc3b709880a60370194e6d0c060e465b945368" - integrity sha512-PGE3H+jM73ZugEe3bTCxA2UjI+IyPbdZetXj+4TDkfLU3WTnW/wqo0TeIZTvfBNpKg4PdyLEtdOcQePeywr8Zw== +"@react-native-community/cli-types@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli-types/-/cli-types-12.0.0-alpha.11.tgz#140b96450c3d94cba2cec750f560dfea86350b65" + integrity sha512-iG4/Q4WDjJAlt8eeEKhlgnIpEHYcsYDepQClef7hq6mtuzQyn+Qk0LZbIwXBA1xdTp93+NTZJiHUS6iAk0sIOg== dependencies: joi "^17.2.1" -"@react-native-community/cli@12.0.0-alpha.9": - version "12.0.0-alpha.9" - resolved "https://registry.yarnpkg.com/@react-native-community/cli/-/cli-12.0.0-alpha.9.tgz#ff0227bafb135a6c91e162bf299e24db39d15274" - integrity sha512-MxZ+/mXjvQ88ndo0IaiJ7NDr1P7jO5ZOTtpvxB9xl8HdufZde2pL+xeVTqqN/NVWCSkdDEaP7120OZ9kzXRIKw== - dependencies: - "@react-native-community/cli-clean" "12.0.0-alpha.9" - "@react-native-community/cli-config" "12.0.0-alpha.9" - "@react-native-community/cli-debugger-ui" "12.0.0-alpha.9" - "@react-native-community/cli-doctor" "12.0.0-alpha.9" - "@react-native-community/cli-hermes" "12.0.0-alpha.9" - "@react-native-community/cli-plugin-metro" "12.0.0-alpha.9" - "@react-native-community/cli-server-api" "12.0.0-alpha.9" - "@react-native-community/cli-tools" "12.0.0-alpha.9" - "@react-native-community/cli-types" "12.0.0-alpha.9" +"@react-native-community/cli@12.0.0-alpha.11": + version "12.0.0-alpha.11" + resolved "https://registry.yarnpkg.com/@react-native-community/cli/-/cli-12.0.0-alpha.11.tgz#d8307a7127e200d521e47ee5f1ae1f5d9219ddf4" + integrity sha512-iR8MUzxtiuaR9yS2b1F27lWHlOrhzyWNUy/QJUF2Lbi4+yZHPNBR8ICE4L5IyWprKVtT9hkkNeAVtmyNA4CFjQ== + dependencies: + "@react-native-community/cli-clean" "12.0.0-alpha.11" + "@react-native-community/cli-config" "12.0.0-alpha.11" + "@react-native-community/cli-debugger-ui" "12.0.0-alpha.11" + "@react-native-community/cli-doctor" "12.0.0-alpha.11" + "@react-native-community/cli-hermes" "12.0.0-alpha.11" + "@react-native-community/cli-plugin-metro" "12.0.0-alpha.11" + "@react-native-community/cli-server-api" "12.0.0-alpha.11" + "@react-native-community/cli-tools" "12.0.0-alpha.11" + "@react-native-community/cli-types" "12.0.0-alpha.11" chalk "^4.1.2" commander "^9.4.1" execa "^5.0.0" @@ -6010,6 +6011,13 @@ fast-xml-parser@^4.0.12: dependencies: strnum "^1.0.5" +fast-xml-parser@^4.2.4: + version "4.2.7" + resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.2.7.tgz#871f2ca299dc4334b29f8da3658c164e68395167" + integrity sha512-J8r6BriSLO1uj2miOk1NW0YVm8AGOOu3Si2HQp/cSmo6EA4m3fcwu2WKjJ4RK9wMLBtg69y1kS8baDiQBR41Ig== + dependencies: + strnum "^1.0.5" + fastq@^1.6.0: version "1.13.0" resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.13.0.tgz#616760f88a7526bdfc596b7cab8c18938c36b98c" From 10dcaa02a82c5c794cc1c7f3ec6074d2fab1cf44 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 22 Aug 2023 12:54:26 -0700 Subject: [PATCH 2/4] Refactor CLI command entry points (#39075) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39075 Small refactor: reorganise command entry points and types into a consistent pattern. Changelog: [Internal] Differential Revision: https://internalfb.com/D48433284 fbshipit-source-id: e7ed0a6e6c6f808cd4897c066d7136e4a85b6121 --- .../src/commands/bundle/buildBundle.js | 45 ++++-- .../src/commands/bundle/bundle.js | 38 ------ .../commands/bundle/bundleCommandLineArgs.js | 129 ------------------ .../src/commands/bundle/index.js | 120 ++++++++++++++++ .../ramBundle.js => ram-bundle/index.js} | 32 ++--- .../src/commands/start/index.js | 8 +- .../src/commands/start/runServer.js | 8 +- .../community-cli-plugin/src/index.flow.js | 4 +- 8 files changed, 182 insertions(+), 202 deletions(-) delete mode 100644 packages/community-cli-plugin/src/commands/bundle/bundle.js delete mode 100644 packages/community-cli-plugin/src/commands/bundle/bundleCommandLineArgs.js create mode 100644 packages/community-cli-plugin/src/commands/bundle/index.js rename packages/community-cli-plugin/src/commands/{bundle/ramBundle.js => ram-bundle/index.js} (53%) diff --git a/packages/community-cli-plugin/src/commands/bundle/buildBundle.js b/packages/community-cli-plugin/src/commands/bundle/buildBundle.js index ca265c1a1c74..525f46748bb8 100644 --- a/packages/community-cli-plugin/src/commands/bundle/buildBundle.js +++ b/packages/community-cli-plugin/src/commands/bundle/buildBundle.js @@ -12,20 +12,43 @@ import type {Config} from '@react-native-community/cli-types'; import type {RequestOptions} from 'metro/src/shared/types.flow'; import type {ConfigT} from 'metro-config'; -import type {CommandLineArgs} from './bundleCommandLineArgs'; import Server from 'metro/src/Server'; -const outputBundle = require('metro/src/shared/output/bundle'); +import metroBundle from 'metro/src/shared/output/bundle'; +import metroRamBundle from 'metro/src/shared/output/RamBundle'; import path from 'path'; import chalk from 'chalk'; import saveAssets from './saveAssets'; -import {default as loadMetroConfig} from '../../utils/loadMetroConfig'; +import loadMetroConfig from '../../utils/loadMetroConfig'; import {logger} from '@react-native-community/cli-tools'; +export type BundleCommandArgs = { + assetsDest?: string, + assetCatalogDest?: string, + entryFile: string, + resetCache: boolean, + resetGlobalCache: boolean, + transformer?: string, + minify?: boolean, + config?: string, + platform: string, + dev: boolean, + bundleOutput: string, + bundleEncoding?: 'utf8' | 'utf16le' | 'ascii', + maxWorkers?: number, + sourcemapOutput?: string, + sourcemapSourcesRoot?: string, + sourcemapUseAbsolutePath: boolean, + verbose: boolean, + unstableTransformProfile: string, + indexedRamBundle?: boolean, +}; + async function buildBundle( - args: CommandLineArgs, + _argv: Array, ctx: Config, - output: typeof outputBundle = outputBundle, + args: BundleCommandArgs, + bundleImpl: typeof metroBundle | typeof metroRamBundle = metroBundle, ): Promise { const config = await loadMetroConfig(ctx, { maxWorkers: args.maxWorkers, @@ -33,13 +56,13 @@ async function buildBundle( config: args.config, }); - return buildBundleWithConfig(args, config, output); + return buildBundleWithConfig(args, config, bundleImpl); } async function buildBundleWithConfig( - args: CommandLineArgs, + args: BundleCommandArgs, config: ConfigT, - output: typeof outputBundle = outputBundle, + bundleImpl: typeof metroBundle | typeof metroRamBundle = metroBundle, ): Promise { if (config.resolver.platforms.indexOf(args.platform) === -1) { logger.error( @@ -80,11 +103,13 @@ async function buildBundleWithConfig( const server = new Server(config); try { - const bundle = await output.build(server, requestOpts); + const bundle = await bundleImpl.build(server, requestOpts); // $FlowIgnore[class-object-subtyping] // $FlowIgnore[incompatible-call] - await output.save(bundle, args, logger.info); + // $FlowIgnore[prop-missing] + // $FlowIgnore[incompatible-exact] + await bundleImpl.save(bundle, args, logger.info); // Save the assets of the bundle const outputAssets = await server.getAssets({ diff --git a/packages/community-cli-plugin/src/commands/bundle/bundle.js b/packages/community-cli-plugin/src/commands/bundle/bundle.js deleted file mode 100644 index b81024d24191..000000000000 --- a/packages/community-cli-plugin/src/commands/bundle/bundle.js +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow strict-local - * @format - * @oncall react_native - */ - -import type {Config} from '@react-native-community/cli-types'; -import type {CommandLineArgs} from './bundleCommandLineArgs'; - -import buildBundle from './buildBundle'; -import bundleCommandLineArgs from './bundleCommandLineArgs'; - -/** - * Builds the bundle starting to look for dependencies at the given entry path. - */ -export function bundleWithOutput( - _: Array, - config: Config, - args: CommandLineArgs, - // $FlowFixMe[unclear-type] untyped metro/src/shared/output/bundle or metro/src/shared/output/RamBundle - output: any, -): Promise { - return buildBundle(args, config, output); -} - -const bundleCommand = { - name: 'bundle', - description: 'Build the bundle for the provided JavaScript entry file.', - func: bundleWithOutput, - options: bundleCommandLineArgs, -}; - -export default bundleCommand; diff --git a/packages/community-cli-plugin/src/commands/bundle/bundleCommandLineArgs.js b/packages/community-cli-plugin/src/commands/bundle/bundleCommandLineArgs.js deleted file mode 100644 index 58f4211b5670..000000000000 --- a/packages/community-cli-plugin/src/commands/bundle/bundleCommandLineArgs.js +++ /dev/null @@ -1,129 +0,0 @@ -/** - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - * @flow strict-local - * @format - * @oncall react_native - */ - -import path from 'path'; - -export interface CommandLineArgs { - assetsDest?: string; - assetCatalogDest?: string; - entryFile: string; - resetCache: boolean; - resetGlobalCache: boolean; - transformer?: string; - minify?: boolean; - config?: string; - platform: string; - dev: boolean; - bundleOutput: string; - bundleEncoding?: 'utf8' | 'utf16le' | 'ascii'; - maxWorkers?: number; - sourcemapOutput?: string; - sourcemapSourcesRoot?: string; - sourcemapUseAbsolutePath: boolean; - verbose: boolean; - unstableTransformProfile: string; -} - -export default [ - { - name: '--entry-file ', - description: - 'Path to the root JS file, either absolute or relative to JS root', - }, - { - name: '--platform ', - description: 'Either "ios" or "android"', - default: 'ios', - }, - { - name: '--transformer ', - description: 'Specify a custom transformer to be used', - }, - { - name: '--dev [boolean]', - description: 'If false, warnings are disabled and the bundle is minified', - parse: (val: string): boolean => val !== 'false', - default: true, - }, - { - name: '--minify [boolean]', - description: - 'Allows overriding whether bundle is minified. This defaults to ' + - 'false if dev is true, and true if dev is false. Disabling minification ' + - 'can be useful for speeding up production builds for testing purposes.', - parse: (val: string): boolean => val !== 'false', - }, - { - name: '--bundle-output ', - description: - 'File name where to store the resulting bundle, ex. /tmp/groups.bundle', - }, - { - name: '--bundle-encoding ', - description: - 'Encoding the bundle should be written in (https://nodejs.org/api/buffer.html#buffer_buffer).', - default: 'utf8', - }, - { - name: '--max-workers ', - description: - 'Specifies the maximum number of workers the worker-pool ' + - 'will spawn for transforming files. This defaults to the number of the ' + - 'cores available on your machine.', - parse: (workers: string): number => Number(workers), - }, - { - name: '--sourcemap-output ', - description: - 'File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map', - }, - { - name: '--sourcemap-sources-root ', - description: - "Path to make sourcemap's sources entries relative to, ex. /root/dir", - }, - { - name: '--sourcemap-use-absolute-path', - description: 'Report SourceMapURL using its full path', - default: false, - }, - { - name: '--assets-dest ', - description: - 'Directory name where to store assets referenced in the bundle', - }, - { - name: '--unstable-transform-profile ', - description: - 'Experimental, transform JS for a specific JS engine. Currently supported: hermes, hermes-canary, default', - default: 'default', - }, - { - name: '--asset-catalog-dest [string]', - description: 'Path where to create an iOS Asset Catalog for images', - }, - { - name: '--reset-cache', - description: 'Removes cached files', - default: false, - }, - { - name: '--read-global-cache', - description: - 'Try to fetch transformed JS code from the global cache, if configured.', - default: false, - }, - { - name: '--config ', - description: 'Path to the CLI configuration file', - parse: (val: string): string => path.resolve(val), - }, -]; diff --git a/packages/community-cli-plugin/src/commands/bundle/index.js b/packages/community-cli-plugin/src/commands/bundle/index.js new file mode 100644 index 000000000000..6a480c4e333a --- /dev/null +++ b/packages/community-cli-plugin/src/commands/bundle/index.js @@ -0,0 +1,120 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + * @oncall react_native + */ + +import type {Command} from '@react-native-community/cli-types'; + +import path from 'path'; +import buildBundle from './buildBundle'; + +export type {BundleCommandArgs} from './buildBundle'; + +const bundleCommand: Command = { + name: 'bundle', + description: 'Build the bundle for the provided JavaScript entry file.', + func: buildBundle, + options: [ + { + name: '--entry-file ', + description: + 'Path to the root JS file, either absolute or relative to JS root', + }, + { + name: '--platform ', + description: 'Either "ios" or "android"', + default: 'ios', + }, + { + name: '--transformer ', + description: 'Specify a custom transformer to be used', + }, + { + name: '--dev [boolean]', + description: 'If false, warnings are disabled and the bundle is minified', + parse: (val: string): boolean => val !== 'false', + default: true, + }, + { + name: '--minify [boolean]', + description: + 'Allows overriding whether bundle is minified. This defaults to ' + + 'false if dev is true, and true if dev is false. Disabling minification ' + + 'can be useful for speeding up production builds for testing purposes.', + parse: (val: string): boolean => val !== 'false', + }, + { + name: '--bundle-output ', + description: + 'File name where to store the resulting bundle, ex. /tmp/groups.bundle', + }, + { + name: '--bundle-encoding ', + description: + 'Encoding the bundle should be written in (https://nodejs.org/api/buffer.html#buffer_buffer).', + default: 'utf8', + }, + { + name: '--max-workers ', + description: + 'Specifies the maximum number of workers the worker-pool ' + + 'will spawn for transforming files. This defaults to the number of the ' + + 'cores available on your machine.', + parse: (workers: string): number => Number(workers), + }, + { + name: '--sourcemap-output ', + description: + 'File name where to store the sourcemap file for resulting bundle, ex. /tmp/groups.map', + }, + { + name: '--sourcemap-sources-root ', + description: + "Path to make sourcemap's sources entries relative to, ex. /root/dir", + }, + { + name: '--sourcemap-use-absolute-path', + description: 'Report SourceMapURL using its full path', + default: false, + }, + { + name: '--assets-dest ', + description: + 'Directory name where to store assets referenced in the bundle', + }, + { + name: '--unstable-transform-profile ', + description: + 'Experimental, transform JS for a specific JS engine. Currently supported: hermes, hermes-canary, default', + default: 'default', + }, + { + name: '--asset-catalog-dest [string]', + description: 'Path where to create an iOS Asset Catalog for images', + }, + { + name: '--reset-cache', + description: 'Removes cached files', + default: false, + }, + { + name: '--read-global-cache', + description: + 'Try to fetch transformed JS code from the global cache, if configured.', + default: false, + }, + { + name: '--config ', + description: 'Path to the CLI configuration file', + parse: (val: string): string => path.resolve(val), + }, + ], +}; + +export default bundleCommand; diff --git a/packages/community-cli-plugin/src/commands/bundle/ramBundle.js b/packages/community-cli-plugin/src/commands/ram-bundle/index.js similarity index 53% rename from packages/community-cli-plugin/src/commands/bundle/ramBundle.js rename to packages/community-cli-plugin/src/commands/ram-bundle/index.js index eea8404f5612..1cf978ffc8d4 100644 --- a/packages/community-cli-plugin/src/commands/bundle/ramBundle.js +++ b/packages/community-cli-plugin/src/commands/ram-bundle/index.js @@ -10,30 +10,22 @@ */ import type {Command, Config} from '@react-native-community/cli-types'; -import type {CommandLineArgs} from './bundleCommandLineArgs'; +import type {BundleCommandArgs} from '../bundle'; -import outputUnbundle from 'metro/src/shared/output/RamBundle'; -import {bundleWithOutput} from './bundle'; -import bundleCommandLineArgs from './bundleCommandLineArgs'; +import metroRamBundle from 'metro/src/shared/output/RamBundle'; +import bundleCommand from '../bundle'; +import buildBundle from '../bundle/buildBundle'; -/** - * Builds the bundle starting to look for dependencies at the given entry path. - */ -function ramBundle( - argv: Array, - config: Config, - args: CommandLineArgs, -): Promise { - return bundleWithOutput(argv, config, args, outputUnbundle); -} - -export default ({ +const ramBundleCommand: Command = { name: 'ram-bundle', description: 'Build the RAM bundle for the provided JavaScript entry file. See https://reactnative.dev/docs/ram-bundles-inline-requires.', - func: ramBundle, + func: (argv: Array, config: Config, args: BundleCommandArgs) => { + return buildBundle(argv, config, args, metroRamBundle); + }, options: [ - ...bundleCommandLineArgs, + // $FlowFixMe[incompatible-type] options is nonnull + ...bundleCommand.options, { name: '--indexed-ram-bundle', description: @@ -41,6 +33,6 @@ export default ({ default: false, }, ], -}: Command); +}; -export {ramBundle}; +export default ramBundleCommand; diff --git a/packages/community-cli-plugin/src/commands/start/index.js b/packages/community-cli-plugin/src/commands/start/index.js index 573f4e470629..8bcf6e1ac4b0 100644 --- a/packages/community-cli-plugin/src/commands/start/index.js +++ b/packages/community-cli-plugin/src/commands/start/index.js @@ -9,10 +9,14 @@ * @oncall react_native */ +import type {Command} from '@react-native-community/cli-types'; + import path from 'path'; import runServer from './runServer'; -export default { +export type {StartCommandArgs} from './runServer'; + +const startCommand: Command = { name: 'start', func: runServer, description: 'Start the React Native development server.', @@ -93,3 +97,5 @@ export default { }, ], }; + +export default startCommand; diff --git a/packages/community-cli-plugin/src/commands/start/runServer.js b/packages/community-cli-plugin/src/commands/start/runServer.js index a6397147f441..482d0a6378dc 100644 --- a/packages/community-cli-plugin/src/commands/start/runServer.js +++ b/packages/community-cli-plugin/src/commands/start/runServer.js @@ -34,7 +34,7 @@ import { import loadMetroConfig from '../../utils/loadMetroConfig'; import attachKeyHandlers from './attachKeyHandlers'; -export type Args = { +export type StartCommandArgs = { assetPlugins?: string[], cert?: string, customLogReporterPath?: string, @@ -53,7 +53,11 @@ export type Args = { interactive: boolean, }; -async function runServer(_argv: Array, ctx: Config, args: Args) { +async function runServer( + _argv: Array, + ctx: Config, + args: StartCommandArgs, +) { let port = args.port ?? 8081; let packager = true; const packagerStatus = await isPackagerRunning(port); diff --git a/packages/community-cli-plugin/src/index.flow.js b/packages/community-cli-plugin/src/index.flow.js index 5d5e140970a2..80adc7e51beb 100644 --- a/packages/community-cli-plugin/src/index.flow.js +++ b/packages/community-cli-plugin/src/index.flow.js @@ -9,8 +9,8 @@ * @oncall react_native */ -export {default as bundleCommand} from './commands/bundle/bundle'; -export {default as ramBundleCommand} from './commands/bundle/ramBundle'; +export {default as bundleCommand} from './commands/bundle'; +export {default as ramBundleCommand} from './commands/ram-bundle'; export {default as startCommand} from './commands/start'; export {unstable_buildBundleWithConfig} from './commands/bundle/buildBundle'; From 02bca359080311a826809fee27d1d8371c8e60ab Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 22 Aug 2023 12:54:26 -0700 Subject: [PATCH 3/4] Simplify occupied port handling in start command (#39078) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39078 Simplifies and hardens behaviour for detecting other processes / dev server instances when running `react-native start`. - New flow: - Exits with error message if port is taken by another process (*no longer suggests next port*). - Exits with info message if port is taken by another instance of this dev server (**unchanged**). - Continues if result unknown. - *(No longer logs dedicated message for another RN server running in a different project root.)* - This now checks if the TCP port is in use before attempting an HTTP fetch. Previous behaviour: [`handlePortUnavailable`](https://github.com/react-native-community/cli/blob/734222118707fff41c71463528e4e0c227b31cc6/packages/cli-tools/src/handlePortUnavailable.ts#L8). This decouples us from some lower-level `react-native-community/cli-tools` utils, which remain reused by the `android` and `ios` commands. Changelog: [Internal] Differential Revision: https://internalfb.com/D48433285 fbshipit-source-id: c893444eafc19dd3bfc7cc91995550f6e8372e3f --- .../cli-tools_v12.x.x.js | 43 ++--------- .../src/commands/start/runServer.js | 72 +++++++++---------- .../src/utils/isDevServerRunning.js | 71 ++++++++++++++++++ 3 files changed, 107 insertions(+), 79 deletions(-) create mode 100644 packages/community-cli-plugin/src/utils/isDevServerRunning.js diff --git a/flow-typed/npm/@react-native-community/cli-tools_v12.x.x.js b/flow-typed/npm/@react-native-community/cli-tools_v12.x.x.js index a855762f78c9..aef9a4fc6959 100644 --- a/flow-typed/npm/@react-native-community/cli-tools_v12.x.x.js +++ b/flow-typed/npm/@react-native-community/cli-tools_v12.x.x.js @@ -18,50 +18,15 @@ declare module '@react-native-community/cli-tools' { constructor(msg: string, originalError?: Error | mixed | string): this; } - declare export function getPidFromPort(port: number): number | null; - - declare export function handlePortUnavailable( - initialPort: number, - projectRoot: string, - initialPackager?: boolean, - ): Promise<{ - port: number, - packager: boolean, - }>; - - declare export function hookStdout(callback: Function): () => void; - - declare export function isPackagerRunning( - packagerPort: string | number | void, - ): Promise< - | { - status: 'running', - root: string, - } - | 'not_running' - | 'unrecognized', - >; - declare export const logger: $ReadOnly<{ - success: (...message: Array) => void, - info: (...message: Array) => void, - warn: (...message: Array) => void, - error: (...message: Array) => void, debug: (...message: Array) => void, + error: (...message: Array) => void, log: (...message: Array) => void, - setVerbose: (level: boolean) => void, - isVerbose: () => boolean, - disable: () => void, - enable: () => void, + info: (...message: Array) => void, + warn: (...message: Array) => void, + ... }>; - declare export function logAlreadyRunningBundler(port: number): void; - - declare export function resolveNodeModuleDir( - root: string, - packageName: string, - ): string; - declare export const version: $ReadOnly<{ logIfUpdateAvailable: (projectRoot: string) => Promise, }>; diff --git a/packages/community-cli-plugin/src/commands/start/runServer.js b/packages/community-cli-plugin/src/commands/start/runServer.js index 482d0a6378dc..16d9f9cce20e 100644 --- a/packages/community-cli-plugin/src/commands/start/runServer.js +++ b/packages/community-cli-plugin/src/commands/start/runServer.js @@ -23,14 +23,9 @@ import { createDevServerMiddleware, indexPageMiddleware, } from '@react-native-community/cli-server-api'; -import { - isPackagerRunning, - logger, - version, - logAlreadyRunningBundler, - handlePortUnavailable, -} from '@react-native-community/cli-tools'; +import {logger, version} from '@react-native-community/cli-tools'; +import isDevServerRunning from '../../utils/isDevServerRunning'; import loadMetroConfig from '../../utils/loadMetroConfig'; import attachKeyHandlers from './attachKeyHandlers'; @@ -58,41 +53,38 @@ async function runServer( ctx: Config, args: StartCommandArgs, ) { - let port = args.port ?? 8081; - let packager = true; - const packagerStatus = await isPackagerRunning(port); - - if ( - typeof packagerStatus === 'object' && - packagerStatus.status === 'running' - ) { - if (packagerStatus.root === ctx.root) { - packager = false; - logAlreadyRunningBundler(port); - } else { - const result = await handlePortUnavailable(port, ctx.root, packager); - [port, packager] = [result.port, result.packager]; - } - } else if (packagerStatus === 'unrecognized') { - const result = await handlePortUnavailable(port, ctx.root, packager); - [port, packager] = [result.port, result.packager]; - } - - if (packager === false) { - process.exit(); - } - - logger.info(`Starting dev server on port ${chalk.bold(String(port))}`); - const metroConfig = await loadMetroConfig(ctx, { config: args.config, maxWorkers: args.maxWorkers, - port: port, + port: args.port ?? 8081, resetCache: args.resetCache, watchFolders: args.watchFolders, projectRoot: args.projectRoot, sourceExts: args.sourceExts, }); + const host = args.host?.length ? args.host : 'localhost'; + const { + projectRoot, + server: {port}, + watchFolders, + } = metroConfig; + + const serverStatus = await isDevServerRunning(host, port, projectRoot); + + if (serverStatus === 'matched_server_running') { + logger.info( + `A dev server is already running for this project on port ${port}. Exiting.`, + ); + return; + } else if (serverStatus === 'port_taken') { + logger.error( + `Another process is running on port ${port}. Please terminate this ` + + 'process and try again, or use another port with "--port".', + ); + return; + } + + logger.info(`Starting dev server on port ${chalk.bold(String(port))}`); if (args.assetPlugins) { // $FlowIgnore[cannot-write] Assigning to readonly property @@ -107,14 +99,14 @@ async function runServer( messageSocketEndpoint, eventsSocketEndpoint, } = createDevServerMiddleware({ - host: args.host, - port: metroConfig.server.port, - watchFolders: metroConfig.watchFolders, + host, + port, + watchFolders, }); const {middleware, websocketEndpoints} = createDevMiddleware({ - host: args.host?.length ? args.host : 'localhost', - port: metroConfig.server.port, - projectRoot: metroConfig.projectRoot, + host, + port, + projectRoot, logger, }); diff --git a/packages/community-cli-plugin/src/utils/isDevServerRunning.js b/packages/community-cli-plugin/src/utils/isDevServerRunning.js new file mode 100644 index 000000000000..42239bb18194 --- /dev/null +++ b/packages/community-cli-plugin/src/utils/isDevServerRunning.js @@ -0,0 +1,71 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @flow strict-local + * @format + * @oncall react_native + */ + +import net from 'net'; +import fetch from 'node-fetch'; + +/** + * Determine whether we can run the dev server. + * + * Return values: + * - `not_running`: The port is unoccupied. + * - `matched_server_running`: The port is occupied by another instance of this + * dev server (matching the passed `projectRoot`). + * - `port_taken`: The port is occupied by another process. + * - `unknown`: An error was encountered; attempt server creation anyway. + */ +export default async function isDevServerRunning( + host: string, + port: number, + projectRoot: string, +): Promise< + 'not_running' | 'matched_server_running' | 'port_taken' | 'unknown', +> { + try { + if (!(await isPortOccupied(host, port))) { + return 'not_running'; + } + + const statusResponse = await fetch(`http://localhost:${port}/status`); + const body = await statusResponse.text(); + + return body === 'packager-status:running' && + statusResponse.headers.get('X-React-Native-Project-Root') === projectRoot + ? 'matched_server_running' + : 'port_taken'; + } catch (e) { + return 'unknown'; + } +} + +async function isPortOccupied(host: string, port: number): Promise { + let result = false; + const server = net.createServer(); + + return new Promise((resolve, reject) => { + server.once('error', e => { + server.close(); + if (e.code === 'EADDRINUSE') { + result = true; + } else { + reject(e); + } + }); + server.once('listening', () => { + result = false; + server.close(); + }); + server.once('close', () => { + resolve(result); + }); + server.listen({host, port}); + }); +} From c22d67eae5551f1c69516b74dac1705031c83cf5 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 22 Aug 2023 12:54:52 -0700 Subject: [PATCH 4/4] Bump community-cli-plugin and dev-middleware, sync changes (#39074) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/39074 Changelog: [Internal] Publish new versions of `react-native/community-cli-plugin` and `react-native/dev-middleware` (which are now in a minimum viable state) for early integrators. Also syncs final upstream changes from CLI: - https://github.com/react-native-community/cli/pull/2035 - https://github.com/react-native-community/cli/pull/2052 #publish-packages-to-npm bypass-github-export-checks Reviewed By: motiz88 Differential Revision: D48465522 fbshipit-source-id: 692d245d02c5880b30fa49b885aa599ced06cfd1 --- packages/community-cli-plugin/README.md | 6 ++-- packages/community-cli-plugin/package.json | 2 +- .../src/commands/start/attachKeyHandlers.js | 36 ++++++++++++------- packages/dev-middleware/package.json | 2 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/packages/community-cli-plugin/README.md b/packages/community-cli-plugin/README.md index 2f39b8770ecf..423512ee9ceb 100644 --- a/packages/community-cli-plugin/README.md +++ b/packages/community-cli-plugin/README.md @@ -15,7 +15,7 @@ Start the React Native development server. #### Usage ```sh -react-native start [options] +npx react-native start [options] ``` #### Options @@ -45,7 +45,7 @@ Build the bundle for the provided JavaScript entry file. #### Usage ```sh -react-native bundle --entry-file [options] +npx react-native bundle --entry-file [options] ``` #### Options @@ -75,7 +75,7 @@ Build the [RAM bundle](https://reactnative.dev/docs/ram-bundles-inline-requires) #### Usage ```sh -react-native ram-bundle --entry-file [options] +npx react-native ram-bundle --entry-file [options] ``` #### Options diff --git a/packages/community-cli-plugin/package.json b/packages/community-cli-plugin/package.json index 881a5bc68fba..ed13c21b34ac 100644 --- a/packages/community-cli-plugin/package.json +++ b/packages/community-cli-plugin/package.json @@ -1,6 +1,6 @@ { "name": "@react-native/community-cli-plugin", - "version": "0.73.0", + "version": "0.73.1", "description": "Core CLI commands for React Native", "keywords": [ "react-native", diff --git a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js index 4ccb4ba0f66b..31bb21800b44 100644 --- a/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js +++ b/packages/community-cli-plugin/src/commands/start/attachKeyHandlers.js @@ -39,7 +39,11 @@ export default function attachKeyHandlers( // $FlowIgnore[prop-missing] process.stdin.setRawMode(true); - const onPressAsync = async (key: string) => { + const execaOptions = { + env: {FORCE_COLOR: chalk.supportsColor ? 'true' : 'false'}, + }; + + const onPress = async (key: string) => { switch (key) { case 'r': messageSocket.broadcast('reload', null); @@ -51,19 +55,27 @@ export default function attachKeyHandlers( break; case 'i': logger.info('Opening app on iOS...'); - execa('npx', [ - 'react-native', - 'run-ios', - ...(cliConfig.project.ios?.watchModeCommandParams ?? []), - ]).stdout?.pipe(process.stdout); + execa( + 'npx', + [ + 'react-native', + 'run-ios', + ...(cliConfig.project.ios?.watchModeCommandParams ?? []), + ], + execaOptions, + ).stdout?.pipe(process.stdout); break; case 'a': logger.info('Opening app on Android...'); - execa('npx', [ - 'react-native', - 'run-android', - ...(cliConfig.project.android?.watchModeCommandParams ?? []), - ]).stdout?.pipe(process.stdout); + execa( + 'npx', + [ + 'react-native', + 'run-android', + ...(cliConfig.project.android?.watchModeCommandParams ?? []), + ], + execaOptions, + ).stdout?.pipe(process.stdout); break; case CTRL_Z: process.emit('SIGTSTP', 'SIGTSTP'); @@ -73,7 +85,7 @@ export default function attachKeyHandlers( } }; - const keyPressHandler = new KeyPressHandler(onPressAsync); + const keyPressHandler = new KeyPressHandler(onPress); const listener = keyPressHandler.createInteractionListener(); addInteractionListener(listener); keyPressHandler.startInterceptingKeyStrokes(); diff --git a/packages/dev-middleware/package.json b/packages/dev-middleware/package.json index 1d7767aef020..7782439c4711 100644 --- a/packages/dev-middleware/package.json +++ b/packages/dev-middleware/package.json @@ -1,6 +1,6 @@ { "name": "@react-native/dev-middleware", - "version": "0.73.0", + "version": "0.73.1", "description": "Dev server middleware for React Native", "keywords": [ "react-native",