Files
react-native/scripts/template/initialize.js
T
Alex Hunt 24f7bd7445 Remove .npmrc write from setupVerdaccio util (#42941)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/42941

I noticed that programatically running `npm set registry <value>` would fail within the repo root dir (intended run location) (`node` version `18.18.2`).

```
npm ERR! This command does not support workspaces.
```

It turns out this is no longer supported from npm 9.x: https://github.com/npm/cli/issues/6099. **Note**: The workaround discussed in this thread is incompatible/nontrivial with `npx`, so I've opted to remove this behaviour.

**Changes**

- Remove `npm set registry http://localhost:4873` call.
    - This is non-breaking due to the [explicit `--registry` arg already present in `run-e2e-ci-tests.js`](https://github.com/facebook/react-native/blob/b366b4b42e0f91eb2b1850c404fadd0f0322fc61/scripts/run-ci-e2e-tests.js#L102). The previous `.npmrc` config value is unnecessary, and probably was being ignored (will be validated for this PR in CircleCI run).
- Add comment against remaining `.npmrc` write, convert to `fs` call.
- Remove unused params on `setupVerdaccio` (moved to constants which will be exported and referenced in the next diff).

Changelog: [Internal]

Reviewed By: cipolleschi

Differential Revision: D53609308

fbshipit-source-id: 77d3666b42963cd61f6d3fd0be00cdc19bbb1ec8
2024-02-15 09:06:44 -08:00

150 lines
4.3 KiB
JavaScript

/**
* 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
* @format
* @oncall react_native
*/
'use strict';
const {retry} = require('../circleci/retry');
const forEachPackage = require('../monorepo/for-each-package');
const setupVerdaccio = require('./setup-verdaccio');
const {parseArgs} = require('@pkgjs/parseargs');
const {execSync} = require('child_process');
const path = require('path');
const REPO_ROOT = path.resolve(__dirname, '../..');
const NPM_REGISTRY_SERVER = 'http://localhost:4873';
const config = {
options: {
projectName: {type: 'string'},
templatePath: {type: 'string'},
directory: {type: 'string'},
help: {type: 'boolean'},
},
};
async function main() {
const {
values: {help, projectName, templatePath, directory},
} = parseArgs(config);
if (help) {
console.log(`
Usage: node ./scripts/template/initialize.js [OPTIONS]
Bootstraps and runs \`react-native init\`, using the currently checked out
repository as the source of truth for the react-native package and
dependencies.
- Configures and starts a local npm proxy (Verdaccio).
- Builds and publishes all in-repo dependencies to the local npm proxy.
- Runs \`react-native init\` with the local npm proxy configured.
- Does NOT install CocoaPods dependencies.
Note: This script will mutate the contents of some package files, which
should not be committed.
Options:
--projectName The name of the new React Native project.
--templatePath The absolute path to the folder containing the template.
--directory The absolute path to the target project directory.
`);
return;
}
const VERDACCIO_PID = setupVerdaccio();
try {
process.stdout.write('Bootstrapped Verdaccio \u2705\n');
process.stdout.write('Building packages...\n');
execSync('node ./scripts/build/build.js', {
cwd: REPO_ROOT,
stdio: [process.stdin, process.stdout, process.stderr],
});
process.stdout.write('Starting to publish every package...\n');
forEachPackage(
(packageAbsolutePath, packageRelativePathFromRoot, packageManifest) => {
if (packageManifest.private) {
return;
}
execSync(
`npm publish --registry ${NPM_REGISTRY_SERVER} --access public`,
{
cwd: packageAbsolutePath,
stdio: [process.stdin, process.stdout, process.stderr],
},
);
process.stdout.write(
`Published ${packageManifest.name} to proxy \u2705\n`,
);
},
);
process.stdout.write('Published every package \u2705\n');
execSync(
`node cli.js init ${projectName} \
--directory ${directory} \
--template ${templatePath} \
--verbose \
--skip-install \
--yarn-config-options npmRegistryServer="${NPM_REGISTRY_SERVER}"`,
{
cwd: `${REPO_ROOT}/packages/react-native`,
stdio: [process.stdin, process.stdout, process.stderr],
},
);
process.stdout.write('Completed initialization of template app \u2705\n');
process.stdout.write('Installing dependencies in template app folder...\n');
const options = {
cwd: directory,
stdio: [process.stdin, process.stdout, process.stderr],
};
execSync(
`yarn config set npmRegistryServer "${NPM_REGISTRY_SERVER}"`,
options,
);
execSync(
'yarn config set unsafeHttpWhitelist --json \'["localhost"]\'',
options,
);
const success = await retry('yarn', options, 3, 500, ['install']);
if (!success) {
process.stdout.write(
'Failed to install dependencies in template app folder.',
);
throw new Error('Failed to install dependencies in template app folder.');
}
process.stdout.write('Installed dependencies via Yarn \u2705\n');
} finally {
process.stdout.write(`Killing verdaccio. PID — ${VERDACCIO_PID}...\n`);
execSync(`kill -9 ${VERDACCIO_PID}`);
process.stdout.write('Killed Verdaccio process \u2705\n');
// TODO(huntie): Fix memory leak from `spawn` in `setupVerdaccio` (above
// kill command does not wait for kill success).
process.exit(0);
}
}
if (require.main === module) {
// eslint-disable-next-line no-void
void main();
}