diff --git a/package.json b/package.json index a041712b9abb..e9de8d7c7e19 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,6 @@ "lint": "eslint --max-warnings 0 .", "preinstall": "node ./scripts/try-set-nightly-hermes-compiler.js", "prettier": "prettier --write \"./**/*.{js,md,yml,ts,tsx}\"", - "print-packages": "node ./scripts/monorepo/print", "shellcheck": "./.github/workflow-scripts/analyze_scripts.sh", "start": "yarn --cwd packages/rn-tester start", "set-version": "node ./scripts/releases/set-version.js", diff --git a/scripts/monorepo/print/index.js b/scripts/monorepo/print/index.js deleted file mode 100644 index 189351393175..000000000000 --- a/scripts/monorepo/print/index.js +++ /dev/null @@ -1,132 +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. - * - * @format - * @noflow - */ - -const {getPackages} = require('../../shared/monorepoUtils'); -const {exit} = require('shelljs'); -const yargs = require('yargs'); - -const { - argv: {type, minor}, -} = yargs - .option('type', { - type: 'string', - describe: 'Choose which packages to list, default is all', - choices: ['all', 'public', 'private'], - default: 'all', - }) - .option('minor', { - type: 'number', - describe: - 'List latest version for specified minor. Ex. 72, 73. Note this will make a network request to npm', - default: 0, - }) - .check(argv => { - if (argv.minor > 0 && argv.minor < 70) { - throw new Error('Invalid minor. No monorepo packages before 70'); - } - return true; - }) - .strict(); - -function reversePatchComp(semverA, semverB) { - const patchA = parseInt(semverA.split('.')[2], 10); - const patchB = parseInt(semverB.split('.')[2], 10); - 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({ - includeReactNative: true, - includePrivate: true, - }); - - for (const {packageJson} of Object.values(packages)) { - const isPublic = !packageJson.private; - if ( - type === 'all' || - (type === 'private' && !isPublic) || - (type === 'public' && isPublic) - ) { - const packageInfo = { - 'Public?': isPublic ? '\u{2705}' : '\u{274C}', - Name: packageJson.name, - 'Version (main)': packageJson.version, - }; - - if (isPublic && minor !== 0) { - try { - const versions = getVersionsBySpec( - packageJson.name, - `^0.${minor}.0`, - ).sort(reversePatchComp); - packageInfo[`Version (${minor})`] = versions[0]; - } catch (e) { - packageInfo[`Version (${minor})`] = e.message; - } - } - data.push(packageInfo); - } - } - - console.table(data); - exit(0); -} - -if (require.main === module) { - void main(); -} diff --git a/scripts/releases/utils/__tests__/scm-utils-test.js b/scripts/releases/utils/__tests__/scm-utils-test.js index 64bbacf1597e..62b12717f3ca 100644 --- a/scripts/releases/utils/__tests__/scm-utils-test.js +++ b/scripts/releases/utils/__tests__/scm-utils-test.js @@ -8,11 +8,10 @@ * @format */ -const {isTaggedLatest, revertFiles, saveFiles} = require('../scm-utils'); +const {isTaggedLatest} = require('../scm-utils'); let execResult = null; -const cpMock = jest.fn(); -const mkdirSyncMock = jest.fn(); + jest .mock('shelljs', () => ({ exec: () => { @@ -26,11 +25,9 @@ jest exit: exitCode => { process.exit(exitCode); }, - cp: cpMock, })) .mock('fs', () => ({ existsSync: jest.fn().mockImplementation(_ => true), - mkdirSync: mkdirSyncMock, })) .mock('path', () => ({ dirname: jest @@ -58,41 +55,4 @@ describe('scm-utils', () => { expect(isTaggedLatest('6c19dc3266b8')).toBe(false); }); }); - - describe('saveFiles', () => { - it('it should save files in the temp folder', () => { - const tmpFolder = '/tmp'; - saveFiles(['package.json', 'android/package.json'], tmpFolder); - expect(mkdirSyncMock).toHaveBeenCalledWith(`${tmpFolder}/android`, { - recursive: true, - }); - expect(cpMock).toHaveBeenNthCalledWith( - 1, - 'package.json', - '/tmp/package.json', - ); - expect(cpMock).toHaveBeenNthCalledWith( - 2, - 'android/package.json', - `${tmpFolder}/android/package.json`, - ); - }); - }); - - describe('revertFiles', () => { - it('it should revert files from the temp folder', () => { - const tmpFolder = '/tmp'; - revertFiles(['package.json', 'android/package.json'], tmpFolder); - expect(cpMock).toHaveBeenNthCalledWith( - 1, - `${tmpFolder}/package.json`, - 'package.json', - ); - expect(cpMock).toHaveBeenNthCalledWith( - 2, - `${tmpFolder}/android/package.json`, - 'android/package.json', - ); - }); - }); }); diff --git a/scripts/releases/utils/scm-utils.js b/scripts/releases/utils/scm-utils.js index 986ae22453f6..0f1e59d196c3 100644 --- a/scripts/releases/utils/scm-utils.js +++ b/scripts/releases/utils/scm-utils.js @@ -10,28 +10,8 @@ 'use strict'; -const fs = require('fs'); -const path = require('path'); -const {cp, echo, exec, exit} = require('shelljs'); - -/*:: -type Commit = string; -*/ - -function isGitRepo() /*: boolean */ { - try { - return ( - exec('git rev-parse --is-inside-work-tree', { - silent: true, - }).stdout.trim() === 'true' - ); - } catch (error) { - echo( - `It wasn't possible to check if we are in a git repository. Details: ${error}`, - ); - } - return false; -} +const isGitRepo = require('../../shared/isGitRepo'); +const {echo, exec, exit} = require('shelljs'); function exitIfNotOnGit /*::*/( command /*: () => T */, @@ -47,7 +27,7 @@ function exitIfNotOnGit /*::*/( } } -function isTaggedLatest(commitSha /*: Commit */) /*: boolean */ { +function isTaggedLatest(commitSha /*: string */) /*: boolean */ { return ( exec(`git rev-list -1 latest | grep ${commitSha}`, { silent: true, @@ -61,7 +41,7 @@ function getBranchName() /*: string */ { }).stdout.trim(); } -function getCurrentCommit() /*: Commit */ { +function getCurrentCommit() /*: string */ { return isGitRepo() ? exec('git rev-parse HEAD', { silent: true, @@ -69,51 +49,9 @@ function getCurrentCommit() /*: Commit */ { : 'TEMP'; } -function saveFiles(filePaths /*: Array */, tmpFolder /*: string */) { - for (const filePath of filePaths) { - const dirName = path.dirname(filePath); - if (dirName !== '.') { - const destFolder = `${tmpFolder}/${dirName}`; - fs.mkdirSync(destFolder, {recursive: true}); - } - cp(filePath, `${tmpFolder}/${filePath}`); - } -} - -function revertFiles(filePaths /*: Array */, tmpFolder /*: string */) { - for (const filePath of filePaths) { - const absoluteTmpPath = `${tmpFolder}/${filePath}`; - if (fs.existsSync(absoluteTmpPath)) { - cp(absoluteTmpPath, filePath); - } else { - echo( - `It was not possible to revert ${filePath} since ${absoluteTmpPath} does not exist.`, - ); - exit(1); - } - } -} - -// git restore for local path -function restore(repoPath /*: string */) { - const result = exec('git restore .', { - cwd: repoPath, - }); - - if (result.code !== 0) { - throw new Error(result.stderr); - } - - return; -} - module.exports = { exitIfNotOnGit, getCurrentCommit, getBranchName, - isGitRepo, isTaggedLatest, - revertFiles, - saveFiles, - restore, }; diff --git a/scripts/shared/isGitRepo.js b/scripts/shared/isGitRepo.js index 0e4f6d2ade12..654ee51d2920 100644 --- a/scripts/shared/isGitRepo.js +++ b/scripts/shared/isGitRepo.js @@ -12,10 +12,6 @@ const childProcess = require('child_process'); -/*:: -type Commit = string; -*/ - function isGitRepo() /*: boolean */ { try { const result = childProcess.execSync(