From 80ec096e7ce37cb39bb61716037d5ee9959b3a74 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Fri, 16 Feb 2024 03:21:25 -0800 Subject: [PATCH] Create shared consts module for scripts Summary: Tidy up, removes risk of breakage when relocating inner modules. Changelog: [Internal] Reviewed By: lunaleaps Differential Revision: D53813025 fbshipit-source-id: 43dc4a57191363546d6858cff311e260d416192f --- scripts/build/build.js | 3 +- scripts/consts.js | 44 +++++++++++++++++++ scripts/debugger-frontend/sync-and-build.js | 4 +- scripts/e2e/init-template-e2e.js | 4 +- scripts/e2e/run-ci-e2e-tests.js | 7 +-- scripts/e2e/utils/verdaccio.js | 2 +- scripts/monorepo/for-each-package.js | 8 ++-- scripts/release-testing/test-e2e-local.js | 3 +- .../releases-ci/__tests__/publish-npm-test.js | 3 +- scripts/releases-ci/publish-npm.js | 3 +- .../releases/__tests__/set-rn-version-test.js | 3 +- scripts/releases/set-rn-version.js | 2 +- .../set-version/__tests__/set-version-test.js | 14 +++--- scripts/releases/utils/monorepo.js | 2 +- 14 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 scripts/consts.js diff --git a/scripts/build/build.js b/scripts/build/build.js index 52733861f0f..3fe18c98e4d 100644 --- a/scripts/build/build.js +++ b/scripts/build/build.js @@ -9,6 +9,7 @@ * @oncall react_native */ +const {PACKAGES_DIR} = require('../consts'); const { buildConfig, getBabelConfig, @@ -26,8 +27,6 @@ const path = require('path'); const prettier = require('prettier'); const ts = require('typescript'); -const REPO_ROOT = path.resolve(__dirname, '../..'); -const PACKAGES_DIR /*: string */ = path.join(REPO_ROOT, 'packages'); const SRC_DIR = 'src'; const BUILD_DIR = 'dist'; const JS_FILES_PATTERN = '**/*.js'; diff --git a/scripts/consts.js b/scripts/consts.js new file mode 100644 index 00000000000..a4528fea7dc --- /dev/null +++ b/scripts/consts.js @@ -0,0 +1,44 @@ +/** + * 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 + * @format + * @oncall react_native + */ + +const path = require('path'); + +/** + * The absolute path to the repo root. + */ +const REPO_ROOT /*: string */ = path.resolve(__dirname, '..'); + +/** + * The absolute path to the packages directory (note: this directory alone may + * not match the "workspaces" config in package.json). + */ +const PACKAGES_DIR /*: string */ = path.join(REPO_ROOT, 'packages'); + +/** + * The absolute path to the repo scripts directory. + */ +const SCRIPTS_DIR /*: string */ = path.join(REPO_ROOT, 'scripts'); + +/** + * The absolute path to the react-native package. + */ +const REACT_NATIVE_PACKAGE_DIR /*: string */ = path.join( + REPO_ROOT, + 'packages', + 'react-native', +); + +module.exports = { + PACKAGES_DIR, + REACT_NATIVE_PACKAGE_DIR, + REPO_ROOT, + SCRIPTS_DIR, +}; diff --git a/scripts/debugger-frontend/sync-and-build.js b/scripts/debugger-frontend/sync-and-build.js index 9c1866f285d..37a55c1c324 100644 --- a/scripts/debugger-frontend/sync-and-build.js +++ b/scripts/debugger-frontend/sync-and-build.js @@ -9,6 +9,7 @@ * @oncall react_native */ +const {PACKAGES_DIR} = require('../consts'); const {parseArgs} = require('@pkgjs/parseargs'); // $FlowFixMe[untyped-import]: TODO type ansi-styles const ansiStyles = require('ansi-styles'); @@ -27,9 +28,6 @@ const supportsColor = require('supports-color'); const DEVTOOLS_FRONTEND_REPO_URL = 'https://github.com/facebookexperimental/rn-chrome-devtools-frontend'; -const REPO_ROOT = path.resolve(__dirname, '../..'); -const PACKAGES_DIR /*: string */ = path.join(REPO_ROOT, 'packages'); - const config = { allowPositionals: true, options: { diff --git a/scripts/e2e/init-template-e2e.js b/scripts/e2e/init-template-e2e.js index 3a63656d5ce..49542fcb730 100644 --- a/scripts/e2e/init-template-e2e.js +++ b/scripts/e2e/init-template-e2e.js @@ -12,6 +12,7 @@ 'use strict'; const {retry} = require('../circleci/retry'); +const {REPO_ROOT} = require('../consts'); const forEachPackage = require('../monorepo/for-each-package'); const { VERDACCIO_SERVER_URL, @@ -21,9 +22,6 @@ const { const {parseArgs} = require('@pkgjs/parseargs'); const chalk = require('chalk'); const {execSync} = require('child_process'); -const path = require('path'); - -const REPO_ROOT = path.resolve(__dirname, '../..'); const config = { options: { diff --git a/scripts/e2e/run-ci-e2e-tests.js b/scripts/e2e/run-ci-e2e-tests.js index 435b3f12067..d76d7a1b667 100644 --- a/scripts/e2e/run-ci-e2e-tests.js +++ b/scripts/e2e/run-ci-e2e-tests.js @@ -21,6 +21,7 @@ * --retries [num] - how many times to retry possible flaky commands: yarn add and running tests, default 1 */ +const {REACT_NATIVE_PACKAGE_DIR, REPO_ROOT, SCRIPTS_DIR} = require('../consts'); const forEachPackage = require('../monorepo/for-each-package'); const tryExecNTimes = require('./utils/try-n-times'); const {setupVerdaccio} = require('./utils/verdaccio'); @@ -30,10 +31,6 @@ const path = require('path'); const {cd, cp, echo, exec, exit, mv} = require('shelljs'); const argv = require('yargs').argv; -const SCRIPTS = path.join(__dirname, '..'); -const REPO_ROOT = path.join(__dirname, '../..'); -const REACT_NATIVE_PACKAGE_DIR = path.join(REPO_ROOT, 'packages/react-native'); - const REACT_NATIVE_TEMP_DIR = exec( 'mktemp -d /tmp/react-native-XXXXXXXX', ).stdout.trim(); @@ -150,7 +147,7 @@ try { 'Failed to install appium. Most common reason is npm registry connectivity, try again.', ); } - cp(`${SCRIPTS}/android-e2e-test.js`, 'android-e2e-test.js'); + cp(`${SCRIPTS_DIR}/android-e2e-test.js`, 'android-e2e-test.js'); cd('android'); describe('Download Maven deps'); exec('./gradlew :app:copyDownloadableDepsToLibs'); diff --git a/scripts/e2e/utils/verdaccio.js b/scripts/e2e/utils/verdaccio.js index 3b2497e46a8..d520d206557 100644 --- a/scripts/e2e/utils/verdaccio.js +++ b/scripts/e2e/utils/verdaccio.js @@ -11,11 +11,11 @@ 'use strict'; +const {REPO_ROOT} = require('../../consts'); const {execSync, spawn} = require('child_process'); const fs = require('fs'); const path = require('path'); -const REPO_ROOT = path.join(__dirname, '../../..'); const NPM_CONFIG_PATH = path.join(REPO_ROOT, '.npmrc'); const VERDACCIO_CONFIG_PATH = path.join(__dirname, '..', 'verdaccio.yml'); const VERDACCIO_STORAGE_PATH = '/tmp/verdaccio'; diff --git a/scripts/monorepo/for-each-package.js b/scripts/monorepo/for-each-package.js index da6d9b3ead1..49d7c9eee3f 100644 --- a/scripts/monorepo/for-each-package.js +++ b/scripts/monorepo/for-each-package.js @@ -8,12 +8,10 @@ * @format */ +const {PACKAGES_DIR} = require('../consts'); const {readdirSync, readFileSync} = require('fs'); const path = require('path'); -const REPO_ROOT = path.join(path.dirname(__filename), '..', '..'); -const PACKAGES_LOCATION = path.join(REPO_ROOT, 'packages'); - const DEFAULT_OPTIONS /*: Options */ = {includeReactNative: false}; /*:: @@ -51,13 +49,13 @@ const forEachPackage = ( // We filter react-native package on purpose, so that no CI's script will be executed for this package in future // Unless includeReactNative options is provided - const packagesDirectories = getDirectories(PACKAGES_LOCATION).filter( + const packagesDirectories = getDirectories(PACKAGES_DIR).filter( directoryName => directoryName !== 'react-native' || includeReactNative === true, ); packagesDirectories.forEach(packageDirectory => { - const packageAbsolutePath = path.join(PACKAGES_LOCATION, packageDirectory); + const packageAbsolutePath = path.join(PACKAGES_DIR, packageDirectory); const packageRelativePathFromRoot = path.join('packages', packageDirectory); const packageManifest = JSON.parse( diff --git a/scripts/release-testing/test-e2e-local.js b/scripts/release-testing/test-e2e-local.js index 45823da7602..fbdd376f291 100644 --- a/scripts/release-testing/test-e2e-local.js +++ b/scripts/release-testing/test-e2e-local.js @@ -17,6 +17,7 @@ * and to make it more accessible for other devs to play around with. */ +const {REPO_ROOT} = require('../consts'); const {initNewProjectFromSource} = require('../e2e/init-template-e2e'); const updateTemplatePackage = require('../releases/update-template-package'); const { @@ -36,8 +37,6 @@ const yargs = require('yargs'); type Unwrap = T extends Promise ? U : T; */ -const REPO_ROOT = path.resolve(__dirname, '../..'); - const argv = yargs .option('t', { alias: 'target', diff --git a/scripts/releases-ci/__tests__/publish-npm-test.js b/scripts/releases-ci/__tests__/publish-npm-test.js index 27c6cb31b7f..c8852d37a50 100644 --- a/scripts/releases-ci/__tests__/publish-npm-test.js +++ b/scripts/releases-ci/__tests__/publish-npm-test.js @@ -22,11 +22,10 @@ const getNpmInfoMock = jest.fn(); const generateAndroidArtifactsMock = jest.fn(); const getPackagesMock = jest.fn(); +const {REPO_ROOT} = require('../../consts'); const {publishNpm} = require('../publish-npm'); const path = require('path'); -const REPO_ROOT = path.resolve(__filename, '../../../..'); - let consoleLog; describe('publish-npm', () => { diff --git a/scripts/releases-ci/publish-npm.js b/scripts/releases-ci/publish-npm.js index a50d4211148..ddb38248f96 100755 --- a/scripts/releases-ci/publish-npm.js +++ b/scripts/releases-ci/publish-npm.js @@ -15,6 +15,7 @@ import type {BuildType} from '../releases/utils/version-utils'; */ +const {REPO_ROOT} = require('../consts'); const {getNpmInfo, publishPackage} = require('../npm-utils'); const {removeNewArchFlags} = require('../releases/remove-new-arch-flags'); const {setReactNativeVersion} = require('../releases/set-rn-version'); @@ -27,8 +28,6 @@ const { const path = require('path'); const yargs = require('yargs'); -const REPO_ROOT = path.resolve(__dirname, '../..'); - /** * This script prepares a release version of react-native and may publish to NPM. * It is supposed to run in CI environment, not on a developer's machine. diff --git a/scripts/releases/__tests__/set-rn-version-test.js b/scripts/releases/__tests__/set-rn-version-test.js index 43dcc8b6a4c..26e9bfbee71 100644 --- a/scripts/releases/__tests__/set-rn-version-test.js +++ b/scripts/releases/__tests__/set-rn-version-test.js @@ -23,11 +23,10 @@ jest.mock('fs', () => ({ })); jest.mock('./../update-template-package', () => updateTemplatePackageMock); +const {REPO_ROOT} = require('../../consts'); const {setReactNativeVersion} = require('../set-rn-version'); const path = require('path'); -const REPO_ROOT = path.resolve(__filename, '../../../..'); - describe('setReactNativeVersion', () => { beforeAll(() => { readFileMock.mockImplementation(filePath => { diff --git a/scripts/releases/set-rn-version.js b/scripts/releases/set-rn-version.js index 4873ff34df8..f4dfb5f4c7a 100755 --- a/scripts/releases/set-rn-version.js +++ b/scripts/releases/set-rn-version.js @@ -13,6 +13,7 @@ import type {BuildType, Version} from './utils/version-utils'; */ +const {REPO_ROOT} = require('../consts'); const {applyPackageVersions} = require('../npm-utils'); const {getNpmInfo} = require('../npm-utils'); const updateTemplatePackage = require('./update-template-package'); @@ -21,7 +22,6 @@ const {parseArgs} = require('@pkgjs/parseargs'); const {promises: fs} = require('fs'); const path = require('path'); -const REPO_ROOT = path.join(path.dirname(__filename), '..', '..'); const GRADLE_FILE_PATH = path.join( REPO_ROOT, 'packages/react-native/ReactAndroid/gradle.properties', diff --git a/scripts/releases/set-version/__tests__/set-version-test.js b/scripts/releases/set-version/__tests__/set-version-test.js index 9cae9b2d6c7..9e7369ff947 100644 --- a/scripts/releases/set-version/__tests__/set-version-test.js +++ b/scripts/releases/set-version/__tests__/set-version-test.js @@ -11,6 +11,11 @@ const setVersion = require('../index'); const path = require('path'); +jest.mock('../../../consts', () => ({ + REPO_ROOT: path.join(__dirname, '__fixtures__'), + PACKAGES_DIR: path.join(__dirname, '__fixtures__', 'packages'), +})); + let customWriteFileExpect = null; const writeFileMock = jest.fn().mockImplementation((filePath, content) => { if (customWriteFileExpect != null) { @@ -32,15 +37,6 @@ describe('setVersion', () => { }); beforeAll(() => { - jest.mock('path', () => { - // $FlowIgnore[underconstrained-implicit-instantiation] - const originalPath = jest.requireActual('path'); - return { - ...originalPath, - dirname: () => originalPath.join(__dirname, '__fixtures__/two/levels'), - }; - }); - jest.mock('fs', () => { // $FlowIgnore[underconstrained-implicit-instantiation] const originalFs = jest.requireActual('fs'); diff --git a/scripts/releases/utils/monorepo.js b/scripts/releases/utils/monorepo.js index e33f0bc82d8..a488e15d06e 100644 --- a/scripts/releases/utils/monorepo.js +++ b/scripts/releases/utils/monorepo.js @@ -9,11 +9,11 @@ * @oncall react_native */ +const {REPO_ROOT} = require('../../consts'); const fs = require('fs'); const glob = require('glob'); const path = require('path'); -const REPO_ROOT = path.resolve(__dirname, '../../..'); const WORKSPACES_CONFIG = 'packages/*'; /*::