From fc5e33b582c6bfe935eccdedbb335aa086a40bb1 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Tue, 8 Jul 2025 06:10:36 -0700 Subject: [PATCH] Reorganise shared script utils (#52473) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/52473 Shared utils that were located in the root of `scripts/` are now colocated closer to their dependencies or moved to `scripts/shared/` — simplifying the root directory layout. Changelog: [Internal] Reviewed By: robhogan Differential Revision: D77873875 fbshipit-source-id: e04dba41a1ef811d32793931033fdfa93afad0cd --- jest/preprocessor.js | 2 +- packages/community-cli-plugin/src/index.js | 2 +- packages/core-cli-utils/src/index.js | 2 +- packages/core-cli-utils/src/public/version.js | 2 +- packages/debugger-shell/src/electron/index.js | 2 +- packages/debugger-shell/src/node/index.js | 2 +- packages/dev-middleware/src/index.js | 2 +- packages/metro-config/src/index.js | 2 +- .../scripts/featureflags/index.js | 2 +- packages/rn-tester/cli.js | 2 +- private/helloworld/cli.js | 2 +- .../config/metro-babel-transformer.js | 2 +- private/react-native-fantom/runner/index.js | 2 +- .../runner/warmup/index.js | 2 +- scripts/build/build.js | 4 +- scripts/build/clean.js | 2 +- scripts/debugger-frontend/sync-and-build.js | 2 +- scripts/e2e/init-project-e2e.js | 6 +-- scripts/e2e/utils/verdaccio.js | 2 +- .../js-api/build-types/buildApiSnapshot.js | 4 +- .../js-api/build-types/buildGeneratedTypes.js | 2 +- scripts/js-api/build-types/index.js | 2 +- .../resolution/resolveTypeInputFile.js | 2 +- .../build-types/resolution/simpleResolve.js | 4 +- scripts/js-api/diff-api-snapshot/index.js | 2 +- scripts/monorepo/print/index.js | 48 +++++++++++++++++- scripts/release-testing/test-e2e-local.js | 2 +- .../releases-ci/__tests__/publish-npm-test.js | 16 +++--- .../publish-updated-packages-test.js | 2 +- scripts/releases-ci/publish-npm.js | 6 +-- .../releases-ci/publish-updated-packages.js | 4 +- .../set-rn-artifacts-version-test.js | 2 +- .../releases/__tests__/set-version-test.js | 2 +- scripts/releases/create-release-commit.js | 2 +- scripts/releases/prepare-ios-prebuilds.js | 2 +- scripts/releases/set-rn-artifacts-version.js | 4 +- scripts/releases/set-version.js | 4 +- .../utils}/__tests__/npm-utils-test.js | 47 +---------------- .../utils}/__tests__/scm-utils-test.js | 0 scripts/{ => releases/utils}/npm-utils.js | 50 +------------------ scripts/{ => releases/utils}/scm-utils.js | 0 .../babelRegister.js} | 4 +- scripts/{ => shared}/consts.js | 2 +- scripts/shared/isGitRepo.js | 35 +++++++++++++ .../monorepo.js => shared/monorepoUtils.js} | 2 +- 45 files changed, 141 insertions(+), 153 deletions(-) rename scripts/{ => releases/utils}/__tests__/npm-utils-test.js (64%) rename scripts/{ => releases/utils}/__tests__/scm-utils-test.js (100%) rename scripts/{ => releases/utils}/npm-utils.js (73%) rename scripts/{ => releases/utils}/scm-utils.js (100%) rename scripts/{babel-register.js => shared/babelRegister.js} (91%) rename scripts/{ => shared}/consts.js (95%) create mode 100644 scripts/shared/isGitRepo.js rename scripts/{utils/monorepo.js => shared/monorepoUtils.js} (98%) diff --git a/jest/preprocessor.js b/jest/preprocessor.js index 7436bac2d09..dc03ccb3ed0 100644 --- a/jest/preprocessor.js +++ b/jest/preprocessor.js @@ -32,7 +32,7 @@ if (process.env.FBSOURCE_ENV === '1') { require('@fb-tools/babel-register'); } else { // Register Babel to allow local packages to be loaded from source - require('../scripts/babel-register').registerForMonorepo(); + require('../scripts/shared/babelRegister').registerForMonorepo(); } const transformer = require('@react-native/metro-babel-transformer'); diff --git a/packages/community-cli-plugin/src/index.js b/packages/community-cli-plugin/src/index.js index 81e2ea61e5e..275e475bf58 100644 --- a/packages/community-cli-plugin/src/index.js +++ b/packages/community-cli-plugin/src/index.js @@ -13,7 +13,7 @@ export type * from './index.flow'; */ if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) { - require('../../../scripts/babel-register').registerForMonorepo(); + require('../../../scripts/shared/babelRegister').registerForMonorepo(); } module.exports = require('./index.flow'); diff --git a/packages/core-cli-utils/src/index.js b/packages/core-cli-utils/src/index.js index f7b2e26c41c..8d76ba451f1 100644 --- a/packages/core-cli-utils/src/index.js +++ b/packages/core-cli-utils/src/index.js @@ -13,7 +13,7 @@ export type * from './index.flow'; */ if (process.env.BUILD_EXCLUDE_BABEL_REGISTER == null) { - require('../../../scripts/babel-register').registerForMonorepo(); + require('../../../scripts/shared/babelRegister').registerForMonorepo(); } module.exports = require('./index.flow'); diff --git a/packages/core-cli-utils/src/public/version.js b/packages/core-cli-utils/src/public/version.js index f003627058c..a5aa0613fa5 100644 --- a/packages/core-cli-utils/src/public/version.js +++ b/packages/core-cli-utils/src/public/version.js @@ -13,7 +13,7 @@ export type * from './version.flow'; */ if (process.env.BUILD_EXCLUDE_BABEL_REGISTER == null) { - require('../../../../scripts/babel-register').registerForMonorepo(); + require('../../../../scripts/shared/babelRegister').registerForMonorepo(); } module.exports = require('./version.flow'); diff --git a/packages/debugger-shell/src/electron/index.js b/packages/debugger-shell/src/electron/index.js index 66d021e59e1..f1ddf573392 100644 --- a/packages/debugger-shell/src/electron/index.js +++ b/packages/debugger-shell/src/electron/index.js @@ -13,7 +13,7 @@ export type * from './index.flow'; */ if (!Boolean(process.env.BUILD_EXCLUDE_BABEL_REGISTER)) { - require('../../../../scripts/babel-register').registerForMonorepo(); + require('../../../../scripts/shared/babelRegister').registerForMonorepo(); } module.exports = require('./index.flow'); diff --git a/packages/debugger-shell/src/node/index.js b/packages/debugger-shell/src/node/index.js index 17be3f3de43..b30f2d626f9 100644 --- a/packages/debugger-shell/src/node/index.js +++ b/packages/debugger-shell/src/node/index.js @@ -13,7 +13,7 @@ export type * from './index.flow'; */ if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) { - require('../../../../scripts/babel-register').registerForMonorepo(); + require('../../../../scripts/shared/babelRegister').registerForMonorepo(); } export * from './index.flow'; diff --git a/packages/dev-middleware/src/index.js b/packages/dev-middleware/src/index.js index c94a6211499..900adaec2c4 100644 --- a/packages/dev-middleware/src/index.js +++ b/packages/dev-middleware/src/index.js @@ -13,7 +13,7 @@ export type * from './index.flow'; */ if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) { - require('../../../scripts/babel-register').registerForMonorepo(); + require('../../../scripts/shared/babelRegister').registerForMonorepo(); } export * from './index.flow'; diff --git a/packages/metro-config/src/index.js b/packages/metro-config/src/index.js index 81e2ea61e5e..275e475bf58 100644 --- a/packages/metro-config/src/index.js +++ b/packages/metro-config/src/index.js @@ -13,7 +13,7 @@ export type * from './index.flow'; */ if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) { - require('../../../scripts/babel-register').registerForMonorepo(); + require('../../../scripts/shared/babelRegister').registerForMonorepo(); } module.exports = require('./index.flow'); diff --git a/packages/react-native/scripts/featureflags/index.js b/packages/react-native/scripts/featureflags/index.js index c88a46560de..a8f2f7e8a5a 100644 --- a/packages/react-native/scripts/featureflags/index.js +++ b/packages/react-native/scripts/featureflags/index.js @@ -9,7 +9,7 @@ */ if (require.main === module) { - require('../../../../scripts/babel-register').registerForMonorepo(); + require('../../../../scripts/shared/babelRegister').registerForMonorepo(); let command; diff --git a/packages/rn-tester/cli.js b/packages/rn-tester/cli.js index 3dfd0fbc853..72cfc6f7c13 100644 --- a/packages/rn-tester/cli.js +++ b/packages/rn-tester/cli.js @@ -31,7 +31,7 @@ function injectCoreCLIUtilsRuntimePatch() { if (process.env.BUILD_EXCLUDE_BABEL_REGISTER == null) { // $FlowFixMe[cannot-resolve-module] - require('../../scripts/babel-register').registerForMonorepo(); + require('../../scripts/shared/babelRegister').registerForMonorepo(); } injectCoreCLIUtilsRuntimePatch(); diff --git a/private/helloworld/cli.js b/private/helloworld/cli.js index 3dfd0fbc853..72cfc6f7c13 100644 --- a/private/helloworld/cli.js +++ b/private/helloworld/cli.js @@ -31,7 +31,7 @@ function injectCoreCLIUtilsRuntimePatch() { if (process.env.BUILD_EXCLUDE_BABEL_REGISTER == null) { // $FlowFixMe[cannot-resolve-module] - require('../../scripts/babel-register').registerForMonorepo(); + require('../../scripts/shared/babelRegister').registerForMonorepo(); } injectCoreCLIUtilsRuntimePatch(); diff --git a/private/react-native-fantom/config/metro-babel-transformer.js b/private/react-native-fantom/config/metro-babel-transformer.js index 9d6b82df66c..51593b0b4ef 100644 --- a/private/react-native-fantom/config/metro-babel-transformer.js +++ b/private/react-native-fantom/config/metro-babel-transformer.js @@ -10,5 +10,5 @@ 'use strict'; -require('../../../scripts/babel-register').registerForMonorepo(); +require('../../../scripts/shared/babelRegister').registerForMonorepo(); module.exports = require('@react-native/metro-babel-transformer'); diff --git a/private/react-native-fantom/runner/index.js b/private/react-native-fantom/runner/index.js index a4bf32ccd4c..8486d6a2004 100644 --- a/private/react-native-fantom/runner/index.js +++ b/private/react-native-fantom/runner/index.js @@ -8,6 +8,6 @@ * @format */ -require('../../../scripts/babel-register').registerForMonorepo(); +require('../../../scripts/shared/babelRegister').registerForMonorepo(); module.exports = require('./runner'); diff --git a/private/react-native-fantom/runner/warmup/index.js b/private/react-native-fantom/runner/warmup/index.js index ef652c3df5d..83bbd4da758 100644 --- a/private/react-native-fantom/runner/warmup/index.js +++ b/private/react-native-fantom/runner/warmup/index.js @@ -8,6 +8,6 @@ * @format */ -require('../../../../scripts/babel-register').registerForMonorepo(); +require('../../../../scripts/shared/babelRegister').registerForMonorepo(); module.exports = require('./warmup'); diff --git a/scripts/build/build.js b/scripts/build/build.js index 884e9d9115e..d44d215ab24 100644 --- a/scripts/build/build.js +++ b/scripts/build/build.js @@ -8,9 +8,9 @@ * @format */ -require('../babel-register').registerForScript(); +require('../shared/babelRegister').registerForScript(); -const {PACKAGES_DIR, REPO_ROOT} = require('../consts'); +const {PACKAGES_DIR, REPO_ROOT} = require('../shared/consts'); const { buildConfig, getBabelConfig, diff --git a/scripts/build/clean.js b/scripts/build/clean.js index a51413a3659..13d17b6b9fb 100644 --- a/scripts/build/clean.js +++ b/scripts/build/clean.js @@ -8,7 +8,7 @@ * @format */ -require('../babel-register').registerForScript(); +require('../shared/babelRegister').registerForScript(); const {BUILD_DIR, PACKAGES_DIR} = require('./build'); const {buildConfig} = require('./config'); diff --git a/scripts/debugger-frontend/sync-and-build.js b/scripts/debugger-frontend/sync-and-build.js index ccbf62222c6..53829b92d2c 100644 --- a/scripts/debugger-frontend/sync-and-build.js +++ b/scripts/debugger-frontend/sync-and-build.js @@ -8,7 +8,7 @@ * @format */ -const {PACKAGES_DIR} = require('../consts'); +const {PACKAGES_DIR} = require('../shared/consts'); // $FlowFixMe[untyped-import]: TODO type ansi-styles const ansiStyles = require('ansi-styles'); const {execSync, spawnSync} = require('child_process'); diff --git a/scripts/e2e/init-project-e2e.js b/scripts/e2e/init-project-e2e.js index 2f77e9f8c76..c807038a9e2 100644 --- a/scripts/e2e/init-project-e2e.js +++ b/scripts/e2e/init-project-e2e.js @@ -10,10 +10,10 @@ 'use strict'; -/*:: import type {ProjectInfo} from '../utils/monorepo'; */ +/*:: import type {ProjectInfo} from '../shared/monorepoUtils'; */ -const {PRIVATE_DIR, REPO_ROOT} = require('../consts'); -const {getPackages} = require('../utils/monorepo'); +const {PRIVATE_DIR, REPO_ROOT} = require('../shared/consts'); +const {getPackages} = require('../shared/monorepoUtils'); const {retry} = require('./utils/retry'); const { VERDACCIO_SERVER_URL, diff --git a/scripts/e2e/utils/verdaccio.js b/scripts/e2e/utils/verdaccio.js index 156e283fe9e..f287a653dbf 100644 --- a/scripts/e2e/utils/verdaccio.js +++ b/scripts/e2e/utils/verdaccio.js @@ -10,7 +10,7 @@ 'use strict'; -const {REPO_ROOT} = require('../../consts'); +const {REPO_ROOT} = require('../../shared/consts'); const {execSync, spawn} = require('child_process'); const fs = require('fs'); const path = require('path'); diff --git a/scripts/js-api/build-types/buildApiSnapshot.js b/scripts/js-api/build-types/buildApiSnapshot.js index eb9af6dce87..c0e75fec139 100644 --- a/scripts/js-api/build-types/buildApiSnapshot.js +++ b/scripts/js-api/build-types/buildApiSnapshot.js @@ -11,8 +11,8 @@ import type {PluginObj} from '@babel/core'; -const {PACKAGES_DIR, REACT_NATIVE_PACKAGE_DIR} = require('../../consts'); -const {isGitRepo} = require('../../scm-utils'); +const {PACKAGES_DIR, REACT_NATIVE_PACKAGE_DIR} = require('../../shared/consts'); +const isGitRepo = require('../../shared/isGitRepo'); const {API_EXTRACTOR_CONFIG_FILE, TYPES_OUTPUT_DIR} = require('../config'); const apiSnapshotTemplate = require('./templates/ReactNativeApi.d.ts-template.js'); const applyBabelTransformsSeq = require('./utils/applyBabelTransformsSeq'); diff --git a/scripts/js-api/build-types/buildGeneratedTypes.js b/scripts/js-api/build-types/buildGeneratedTypes.js index 404f14c4629..c4572a23ff7 100644 --- a/scripts/js-api/build-types/buildGeneratedTypes.js +++ b/scripts/js-api/build-types/buildGeneratedTypes.js @@ -8,7 +8,7 @@ * @format */ -const {PACKAGES_DIR, REPO_ROOT} = require('../../consts'); +const {PACKAGES_DIR, REPO_ROOT} = require('../../shared/consts'); const {ENTRY_POINT, IGNORE_PATTERNS, TYPES_OUTPUT_DIR} = require('../config'); const getRequireStack = require('./resolution/getRequireStack'); const translatedModuleTemplate = require('./templates/translatedModule.d.ts-template'); diff --git a/scripts/js-api/build-types/index.js b/scripts/js-api/build-types/index.js index 0a99a33bffe..757fe58663a 100644 --- a/scripts/js-api/build-types/index.js +++ b/scripts/js-api/build-types/index.js @@ -8,7 +8,7 @@ * @format */ -require('../../babel-register').registerForScript(); +require('../../shared/babelRegister').registerForScript(); const buildApiSnapshot = require('./buildApiSnapshot'); const buildGeneratedTypes = require('./buildGeneratedTypes'); diff --git a/scripts/js-api/build-types/resolution/resolveTypeInputFile.js b/scripts/js-api/build-types/resolution/resolveTypeInputFile.js index f1237ca5d2d..bd965c7c79a 100644 --- a/scripts/js-api/build-types/resolution/resolveTypeInputFile.js +++ b/scripts/js-api/build-types/resolution/resolveTypeInputFile.js @@ -8,7 +8,7 @@ * @format */ -const {REPO_ROOT} = require('../../../consts'); +const {REPO_ROOT} = require('../../../shared/consts'); const debug = require('debug')('build-types:resolution'); const fs = require('fs'); const path = require('path'); diff --git a/scripts/js-api/build-types/resolution/simpleResolve.js b/scripts/js-api/build-types/resolution/simpleResolve.js index 33e748af3ae..a571f04a642 100644 --- a/scripts/js-api/build-types/resolution/simpleResolve.js +++ b/scripts/js-api/build-types/resolution/simpleResolve.js @@ -8,8 +8,8 @@ * @format */ -const {PACKAGES_DIR} = require('../../../consts'); -const {getPackages} = require('../../../utils/monorepo'); +const {PACKAGES_DIR} = require('../../../shared/consts'); +const {getPackages} = require('../../../shared/monorepoUtils'); const {existsSync} = require('fs'); const path = require('path'); diff --git a/scripts/js-api/diff-api-snapshot/index.js b/scripts/js-api/diff-api-snapshot/index.js index 777d281461a..4c9f3fc6c0e 100644 --- a/scripts/js-api/diff-api-snapshot/index.js +++ b/scripts/js-api/diff-api-snapshot/index.js @@ -9,7 +9,7 @@ * @oncall react_native */ -require('../../babel-register').registerForScript(); +require('../../shared/babelRegister').registerForScript(); const {diffApiSnapshot} = require('./diffApiSnapshot'); const fs = require('fs'); diff --git a/scripts/monorepo/print/index.js b/scripts/monorepo/print/index.js index 1fc7dd10dc7..7e31f3e98e6 100644 --- a/scripts/monorepo/print/index.js +++ b/scripts/monorepo/print/index.js @@ -8,7 +8,6 @@ * @noflow */ -const {getVersionsBySpec} = require('../../npm-utils'); const {getPackages} = require('../../utils/monorepo'); const {exit} = require('shelljs'); const yargs = require('yargs'); @@ -42,6 +41,53 @@ function reversePatchComp(semverA, semverB) { return patchB - patchA; } +/** + * `packageName`: name of npm package + * `spec`: spec range ex. '^0.72.0' + * + * Return an array of versions of the specified spec range or throw an error + */ +function getVersionsBySpec( + packageName /*: string */, + spec /*: string */, +) /*: Array */ { + const npmString = `npm view ${packageName}@'${spec}' version --json`; + const result = exec(npmString, {silent: true}); + + if (result.code) { + // Special handling if no such package spec exists + if (result.stderr.includes('npm ERR! code E404')) { + /** + * npm ERR! code E404 + * npm ERR! 404 No match found for version ^0.72.0 + * npm ERR! 404 + * npm ERR! 404 '@react-native/community-cli-plugin@^0.72.0' is not in this registry. + * npm ERR! 404 + * npm ERR! 404 Note that you can also install from a + * npm ERR! 404 tarball, folder, http url, or git url. + * { + * "error": { + * "code": "E404", + * "summary": "No match found for version ^0.72.0", + * "detail": "\n '@react-native/community-cli-plugin@^0.72.0' is not in this registry.\n\nNote that you can also install from a\ntarball, folder, http url, or git url." + * } + * } + */ + const error = JSON.parse( + result.stderr + .split('\n') + .filter(line => !line.includes('npm ERR')) + .join(''), + ).error; + throw new Error(error.summary); + } else { + throw new Error(`Failed: ${npmString}`); + } + } + const versions = JSON.parse(result.stdout.trim()); + return !Array.isArray(versions) ? [versions] : versions; +} + async function main() { const data = []; const packages = await getPackages({ diff --git a/scripts/release-testing/test-e2e-local.js b/scripts/release-testing/test-e2e-local.js index 5749c19ba64..9a0768bb474 100644 --- a/scripts/release-testing/test-e2e-local.js +++ b/scripts/release-testing/test-e2e-local.js @@ -17,8 +17,8 @@ * and to make it more accessible for other devs to play around with. */ -const {REPO_ROOT} = require('../consts'); const {initNewProjectFromSource} = require('../e2e/init-project-e2e'); +const {REPO_ROOT} = require('../shared/consts'); const { checkPackagerRunning, launchPackagerInSeparateWindow, diff --git a/scripts/releases-ci/__tests__/publish-npm-test.js b/scripts/releases-ci/__tests__/publish-npm-test.js index d289a509d97..0ab84e5536d 100644 --- a/scripts/releases-ci/__tests__/publish-npm-test.js +++ b/scripts/releases-ci/__tests__/publish-npm-test.js @@ -21,7 +21,7 @@ const getNpmInfoMock = jest.fn(); const generateAndroidArtifactsMock = jest.fn(); const getPackagesMock = jest.fn(); -const {REPO_ROOT} = require('../../consts'); +const {REPO_ROOT} = require('../../shared/consts'); const {publishNpm} = require('../publish-npm'); const path = require('path'); @@ -33,7 +33,7 @@ describe('publish-npm', () => { .mock('shelljs', () => ({ exec: execMock, })) - .mock('./../../scm-utils', () => ({ + .mock('../../releases/utils/scm-utils', () => ({ exitIfNotOnGit: command => command(), getCurrentCommit: () => 'currentco_mmit', isTaggedLatest: isTaggedLatestMock, @@ -49,8 +49,8 @@ describe('publish-npm', () => { .mock('../../releases/set-rn-artifacts-version', () => ({ updateReactNativeArtifacts: updateReactNativeArtifactsMock, })) - .mock('../../npm-utils', () => ({ - ...jest.requireActual('../../npm-utils'), + .mock('../../releases/utils/npm-utils', () => ({ + ...jest.requireActual('../../releases/utils/npm-utils'), publishPackage: publishPackageMock, getNpmInfo: getNpmInfoMock, })); @@ -78,7 +78,7 @@ describe('publish-npm', () => { it('should fail when invalid build type is passed', async () => { // Call actual function // $FlowExpectedError[underconstrained-implicit-instantiation] - const npmUtils = jest.requireActual('../../npm-utils'); + const npmUtils = jest.requireActual('../../releases/utils/npm-utils'); getNpmInfoMock.mockImplementation(npmUtils.getNpmInfo); await expect(async () => { @@ -117,14 +117,14 @@ describe('publish-npm', () => { describe("publishNpm('nightly')", () => { beforeAll(() => { - jest.mock('../../utils/monorepo', () => ({ - ...jest.requireActual('../../utils/monorepo'), + jest.mock('../../shared/monorepoUtils', () => ({ + ...jest.requireActual('../../shared/monorepoUtils'), getPackages: getPackagesMock, })); }); afterAll(() => { - jest.unmock('../../utils/monorepo'); + jest.unmock('../../shared/monorepoUtils'); }); it('should publish', async () => { diff --git a/scripts/releases-ci/__tests__/publish-updated-packages-test.js b/scripts/releases-ci/__tests__/publish-updated-packages-test.js index f927fda5c16..25ffcf2c358 100644 --- a/scripts/releases-ci/__tests__/publish-updated-packages-test.js +++ b/scripts/releases-ci/__tests__/publish-updated-packages-test.js @@ -20,7 +20,7 @@ const fetchMock = jest.fn(); jest.mock('child_process', () => ({execSync})); jest.mock('shelljs', () => ({exec: execMock})); -jest.mock('../../utils/monorepo', () => ({ +jest.mock('../../shared/monorepoUtils', () => ({ getPackages: getPackagesMock, })); // $FlowIgnore[cannot-write] diff --git a/scripts/releases-ci/publish-npm.js b/scripts/releases-ci/publish-npm.js index 5bc22c84e97..9e01175ac1d 100755 --- a/scripts/releases-ci/publish-npm.js +++ b/scripts/releases-ci/publish-npm.js @@ -14,17 +14,17 @@ import type {BuildType} from '../releases/utils/version-utils'; */ -const {REPO_ROOT} = require('../consts'); -const {getNpmInfo, publishPackage} = require('../npm-utils'); const { updateReactNativeArtifacts, } = require('../releases/set-rn-artifacts-version'); const {setVersion} = require('../releases/set-version'); +const {getNpmInfo, publishPackage} = require('../releases/utils/npm-utils'); const { publishAndroidArtifactsToMaven, publishExternalArtifactsToMaven, } = require('../releases/utils/release-utils'); -const {getPackages} = require('../utils/monorepo'); +const {REPO_ROOT} = require('../shared/consts'); +const {getPackages} = require('../shared/monorepoUtils'); const path = require('path'); const yargs = require('yargs'); diff --git a/scripts/releases-ci/publish-updated-packages.js b/scripts/releases-ci/publish-updated-packages.js index 3b6701df304..2a9e7c18173 100644 --- a/scripts/releases-ci/publish-updated-packages.js +++ b/scripts/releases-ci/publish-updated-packages.js @@ -8,8 +8,8 @@ * @format */ -const {publishPackage} = require('../npm-utils'); -const {getPackages} = require('../utils/monorepo'); +const {publishPackage} = require('../releases/utils/npm-utils'); +const {getPackages} = require('../shared/monorepoUtils'); const {execSync} = require('child_process'); const {parseArgs} = require('util'); diff --git a/scripts/releases/__tests__/set-rn-artifacts-version-test.js b/scripts/releases/__tests__/set-rn-artifacts-version-test.js index 0d1e332a6c9..109bfff0a4b 100644 --- a/scripts/releases/__tests__/set-rn-artifacts-version-test.js +++ b/scripts/releases/__tests__/set-rn-artifacts-version-test.js @@ -20,7 +20,7 @@ jest.mock('fs', () => ({ }, })); -const {REPO_ROOT} = require('../../consts'); +const {REPO_ROOT} = require('../../shared/consts'); const {updateReactNativeArtifacts} = require('../set-rn-artifacts-version'); const path = require('path'); diff --git a/scripts/releases/__tests__/set-version-test.js b/scripts/releases/__tests__/set-version-test.js index 01d6ec976fe..fcc18077e80 100644 --- a/scripts/releases/__tests__/set-version-test.js +++ b/scripts/releases/__tests__/set-version-test.js @@ -11,7 +11,7 @@ const {setVersion} = require('../set-version'); const path = require('path'); -jest.mock('../../consts', () => ({ +jest.mock('../../shared/consts', () => ({ REPO_ROOT: path.join(__dirname, '__fixtures__', 'set-version'), PACKAGES_DIR: path.join(__dirname, '__fixtures__', 'set-version', 'packages'), REACT_NATIVE_PACKAGE_DIR: path.join( diff --git a/scripts/releases/create-release-commit.js b/scripts/releases/create-release-commit.js index 7d85a783253..50d2f8a8acd 100644 --- a/scripts/releases/create-release-commit.js +++ b/scripts/releases/create-release-commit.js @@ -9,7 +9,7 @@ */ const {setVersion} = require('../releases/set-version'); -const {getBranchName} = require('../scm-utils'); +const {getBranchName} = require('../releases/utils/scm-utils'); const {parseVersion} = require('./utils/version-utils'); const {execSync} = require('child_process'); const yargs = require('yargs'); diff --git a/scripts/releases/prepare-ios-prebuilds.js b/scripts/releases/prepare-ios-prebuilds.js index e4d1ebcb888..532369817e8 100644 --- a/scripts/releases/prepare-ios-prebuilds.js +++ b/scripts/releases/prepare-ios-prebuilds.js @@ -16,7 +16,7 @@ const {setupDependencies} = require('./ios-prebuild/setupDependencies'); const {createSwiftPackageFile} = require('./ios-prebuild/swift-package'); const path = require('path'); -require('../babel-register').registerForScript(); +require('../shared/babelRegister').registerForScript(); const THIRD_PARTY_PATH = 'packages/react-native/third-party'; const BUILD_DESTINATION = '.build'; diff --git a/scripts/releases/set-rn-artifacts-version.js b/scripts/releases/set-rn-artifacts-version.js index bb1a2465ff0..2b1cd29573c 100755 --- a/scripts/releases/set-rn-artifacts-version.js +++ b/scripts/releases/set-rn-artifacts-version.js @@ -12,8 +12,8 @@ import type {BuildType, Version} from './utils/version-utils'; */ -const {REPO_ROOT} = require('../consts'); -const {getNpmInfo} = require('../npm-utils'); +const {getNpmInfo} = require('../releases/utils/npm-utils'); +const {REPO_ROOT} = require('../shared/consts'); const {parseVersion, validateBuildType} = require('./utils/version-utils'); const {promises: fs} = require('fs'); const path = require('path'); diff --git a/scripts/releases/set-version.js b/scripts/releases/set-version.js index 65f41468474..33f5054a094 100644 --- a/scripts/releases/set-version.js +++ b/scripts/releases/set-version.js @@ -11,14 +11,14 @@ 'use strict'; /*:: -import type {PackageJson} from '../utils/monorepo'; +import type {PackageJson} from '../shared/monorepoUtils'; */ const { getPackages, getWorkspaceRoot, updatePackageJson, -} = require('../utils/monorepo'); +} = require('../shared/monorepoUtils'); const {updateReactNativeArtifacts} = require('./set-rn-artifacts-version'); const {parseArgs} = require('util'); diff --git a/scripts/__tests__/npm-utils-test.js b/scripts/releases/utils/__tests__/npm-utils-test.js similarity index 64% rename from scripts/__tests__/npm-utils-test.js rename to scripts/releases/utils/__tests__/npm-utils-test.js index ce52428a159..c0de672328b 100644 --- a/scripts/__tests__/npm-utils-test.js +++ b/scripts/releases/utils/__tests__/npm-utils-test.js @@ -8,7 +8,7 @@ * @format */ -const {getNpmInfo, getVersionsBySpec, publishPackage} = require('../npm-utils'); +const {getNpmInfo, publishPackage} = require('../npm-utils'); const execMock = jest.fn(); const getCurrentCommitMock = jest.fn(); @@ -98,49 +98,4 @@ describe('npm-utils', () => { }); }); }); - - describe('getVersionsBySpec', () => { - it('should return array when single version returned', () => { - execMock.mockImplementationOnce(() => ({code: 0, stdout: '"0.72.0" \n'})); - - const versions = getVersionsBySpec('mypackage', '^0.72.0'); - expect(versions).toEqual(['0.72.0']); - }); - - it('should return array of versions', () => { - execMock.mockImplementationOnce(() => ({ - code: 0, - stdout: '[\n"0.73.0",\n"0.73.1"\n]\n', - })); - - const versions = getVersionsBySpec('mypackage', '^0.73.0'); - expect(versions).toEqual(['0.73.0', '0.73.1']); - }); - - it('should return error summary if E404', () => { - const error = - `npm ERR! code E404\n` + - `npm ERR! 404 No match found for version ^0.72.0\n` + - `npm ERR! 404\n` + - `npm ERR! 404 '@react-native/community-cli-plugin@^0.72.0' is not in this registry.\n` + - `npm ERR! 404\n` + - `npm ERR! 404 Note that you can also install from a\n` + - `npm ERR! 404 tarball, folder, http url, or git url.\n` + - `{\n` + - ` "error": {\n` + - ` "code": "E404",\n` + - ` "summary": "No match found for version ^0.72.0",\n` + - ` "detail": "\n '@react-native/community-cli-plugin@^0.72.0' is not in this registry.\n\nNote that you can also install from a\ntarball, folder, http url, or git url."\n` + - ` }\n` + - `}\n`; - execMock.mockImplementationOnce(() => ({ - code: 1, - stderr: error, - })); - - expect(() => { - getVersionsBySpec('mypackage', '^0.72.0'); - }).toThrow('No match found for version ^0.72.0'); - }); - }); }); diff --git a/scripts/__tests__/scm-utils-test.js b/scripts/releases/utils/__tests__/scm-utils-test.js similarity index 100% rename from scripts/__tests__/scm-utils-test.js rename to scripts/releases/utils/__tests__/scm-utils-test.js diff --git a/scripts/npm-utils.js b/scripts/releases/utils/npm-utils.js similarity index 73% rename from scripts/npm-utils.js rename to scripts/releases/utils/npm-utils.js index 5b460513b3d..9a127b395c3 100644 --- a/scripts/npm-utils.js +++ b/scripts/releases/utils/npm-utils.js @@ -10,12 +10,12 @@ 'use strict'; -const {parseVersion} = require('./releases/utils/version-utils'); const { exitIfNotOnGit, getCurrentCommit, isTaggedLatest, } = require('./scm-utils'); +const {parseVersion} = require('./version-utils'); const {exec} = require('shelljs'); /*:: @@ -173,55 +173,7 @@ function getPackageVersionStrByTag( return result.stdout.trim(); } -/** - * `packageName`: name of npm package - * `spec`: spec range ex. '^0.72.0' - * - * Return an array of versions of the specified spec range or throw an error - */ -function getVersionsBySpec( - packageName /*: string */, - spec /*: string */, -) /*: Array */ { - const npmString = `npm view ${packageName}@'${spec}' version --json`; - const result = exec(npmString, {silent: true}); - - if (result.code) { - // Special handling if no such package spec exists - if (result.stderr.includes('npm ERR! code E404')) { - /** - * npm ERR! code E404 - * npm ERR! 404 No match found for version ^0.72.0 - * npm ERR! 404 - * npm ERR! 404 '@react-native/community-cli-plugin@^0.72.0' is not in this registry. - * npm ERR! 404 - * npm ERR! 404 Note that you can also install from a - * npm ERR! 404 tarball, folder, http url, or git url. - * { - * "error": { - * "code": "E404", - * "summary": "No match found for version ^0.72.0", - * "detail": "\n '@react-native/community-cli-plugin@^0.72.0' is not in this registry.\n\nNote that you can also install from a\ntarball, folder, http url, or git url." - * } - * } - */ - const error = JSON.parse( - result.stderr - .split('\n') - .filter(line => !line.includes('npm ERR')) - .join(''), - ).error; - throw new Error(error.summary); - } else { - throw new Error(`Failed: ${npmString}`); - } - } - const versions = JSON.parse(result.stdout.trim()); - return !Array.isArray(versions) ? [versions] : versions; -} - module.exports = { getNpmInfo, - getVersionsBySpec, publishPackage, }; diff --git a/scripts/scm-utils.js b/scripts/releases/utils/scm-utils.js similarity index 100% rename from scripts/scm-utils.js rename to scripts/releases/utils/scm-utils.js diff --git a/scripts/babel-register.js b/scripts/shared/babelRegister.js similarity index 91% rename from scripts/babel-register.js rename to scripts/shared/babelRegister.js index 64439cd6355..876cb367fe2 100644 --- a/scripts/babel-register.js +++ b/scripts/shared/babelRegister.js @@ -28,7 +28,7 @@ let isRegisteredForMonorepo = false; * ```js * // Place in a package entry point * if (!process.env.BUILD_EXCLUDE_BABEL_REGISTER) { - * require('../../../scripts/babel-register').registerForMonorepo(); + * require('../../../scripts/shared/babelRegister').registerForMonorepo(); * } * ``` */ @@ -57,7 +57,7 @@ function registerForMonorepo() { * * ```js * // Place in a script entry point - * require('../babel-register').registerForScript(); + * require('../shared/babelRegister').registerForScript(); * ``` */ function registerForScript() { diff --git a/scripts/consts.js b/scripts/shared/consts.js similarity index 95% rename from scripts/consts.js rename to scripts/shared/consts.js index a9a07faedff..09801410ef5 100644 --- a/scripts/consts.js +++ b/scripts/shared/consts.js @@ -13,7 +13,7 @@ const path = require('path'); /** * The absolute path to the repo root. */ -const REPO_ROOT /*: string */ = path.resolve(__dirname, '..'); +const REPO_ROOT /*: string */ = path.resolve(__dirname, '../..'); /** * The absolute path to the packages directory (note: this directory alone may diff --git a/scripts/shared/isGitRepo.js b/scripts/shared/isGitRepo.js new file mode 100644 index 00000000000..0e4f6d2ade1 --- /dev/null +++ b/scripts/shared/isGitRepo.js @@ -0,0 +1,35 @@ +/** + * 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 + */ + +'use strict'; + +const childProcess = require('child_process'); + +/*:: +type Commit = string; +*/ + +function isGitRepo() /*: boolean */ { + try { + const result = childProcess.execSync( + 'git rev-parse --is-inside-work-tree', + { + encoding: 'utf8', + stdio: ['ignore', 'pipe', 'ignore'], + }, + ); + return result.trim() === 'true'; + } catch (error) { + console.error(`Failed to check git repository status: ${error}`); + } + return false; +} + +module.exports = isGitRepo; diff --git a/scripts/utils/monorepo.js b/scripts/shared/monorepoUtils.js similarity index 98% rename from scripts/utils/monorepo.js rename to scripts/shared/monorepoUtils.js index 266e13594e9..6fc9a9d9e1e 100644 --- a/scripts/utils/monorepo.js +++ b/scripts/shared/monorepoUtils.js @@ -8,7 +8,7 @@ * @format */ -const {REPO_ROOT} = require('../consts'); +const {REPO_ROOT} = require('./consts'); const {promises: fs} = require('fs'); const glob = require('glob'); const path = require('path');