Skip to content

Commit

Permalink
Create shared consts module for scripts
Browse files Browse the repository at this point in the history
Summary:
Tidy up, removes risk of breakage when relocating inner modules.

Changelog: [Internal]

Reviewed By: lunaleaps

Differential Revision: D53813025

fbshipit-source-id: 43dc4a57191363546d6858cff311e260d416192f
  • Loading branch information
huntie authored and facebook-github-bot committed Feb 16, 2024
1 parent a80674f commit 80ec096
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 38 deletions.
3 changes: 1 addition & 2 deletions scripts/build/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @oncall react_native
*/

const {PACKAGES_DIR} = require('../consts');
const {
buildConfig,
getBabelConfig,
Expand All @@ -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';
Expand Down
44 changes: 44 additions & 0 deletions scripts/consts.js
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 1 addition & 3 deletions scripts/debugger-frontend/sync-and-build.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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: {
Expand Down
4 changes: 1 addition & 3 deletions scripts/e2e/init-template-e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: {
Expand Down
7 changes: 2 additions & 5 deletions scripts/e2e/run-ci-e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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();
Expand Down Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion scripts/e2e/utils/verdaccio.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 3 additions & 5 deletions scripts/monorepo/for-each-package.js
Original file line number Diff line number Diff line change
Expand Up @@ -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};

/*::
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 1 addition & 2 deletions scripts/release-testing/test-e2e-local.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -36,8 +37,6 @@ const yargs = require('yargs');
type Unwrap<T> = T extends Promise<infer U> ? U : T;
*/

const REPO_ROOT = path.resolve(__dirname, '../..');

const argv = yargs
.option('t', {
alias: 'target',
Expand Down
3 changes: 1 addition & 2 deletions scripts/releases-ci/__tests__/publish-npm-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
3 changes: 1 addition & 2 deletions scripts/releases-ci/publish-npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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.
Expand Down
3 changes: 1 addition & 2 deletions scripts/releases/__tests__/set-rn-version-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
2 changes: 1 addition & 1 deletion scripts/releases/set-rn-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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',
Expand Down
14 changes: 5 additions & 9 deletions scripts/releases/set-version/__tests__/set-version-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion scripts/releases/utils/monorepo.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/*';

/*::
Expand Down

0 comments on commit 80ec096

Please sign in to comment.