Files
react-native/scripts/template/initialize.js
T
Alex Hunt 3c943bbe3a Integrate dev-middleware into start command (#39059)
Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/39059

## Context

RFC: Decoupling Flipper from React Native core: https://github.com/react-native-community/discussions-and-proposals/pull/641

## Changes

This change:
- Links the new `react-native/dev-middleware` endpoints into the recently migrated `react-native start` command.
- Adds `react-native/community-cli-plugin` (the migrated [`cli-plugin-metro`](https://github.com/react-native-community/cli/tree/main/packages/cli-plugin-metro)) as a dependency of `react-native`, and hooks in these versions of the `start`, `bundle`, and `ram-bundle` commands via `react-native.config.js`.

Functionally, this means that the new `/open-debugger` endpoint is available on the dev server started by `react-native start` (not yet linked into any UI).

After this PR is merged, the new `community-cli-plugin` package is "linked" and we can remove `cli-plugin-metro` from `react-native-community/cli`: https://github.com/react-native-community/cli/pull/2055.

Changelog: [Internal]

Reviewed By: motiz88

Differential Revision: D47226421

fbshipit-source-id: 123039961f93bd8183a32a2d3f30c447f7c0f132
2023-08-22 08:08:15 -07:00

120 lines
3.4 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.
*
* @format
*/
'use strict';
const yargs = require('yargs');
const {execSync} = require('child_process');
const path = require('path');
const forEachPackage = require('../monorepo/for-each-package');
const setupVerdaccio = require('../setup-verdaccio');
const {retry} = require('../circleci/retry');
const {argv} = yargs
.option('r', {
alias: 'reactNativeRootPath',
describe: 'Path to root folder of react-native',
required: true,
})
.option('n', {
alias: 'templateName',
describe: 'Template App name',
required: true,
})
.option('tcp', {
alias: 'templateConfigPath',
describe: 'Path to folder containing template config',
required: true,
})
.option('d', {
alias: 'directory',
describe: 'Path to template application folder',
required: true,
})
.strict();
const {reactNativeRootPath, templateName, templateConfigPath, directory} = argv;
const REPO_ROOT = path.resolve(__dirname, '../..');
const VERDACCIO_CONFIG_PATH = `${reactNativeRootPath}/.circleci/verdaccio.yml`;
async function install() {
const VERDACCIO_PID = setupVerdaccio(
reactNativeRootPath,
VERDACCIO_CONFIG_PATH,
);
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 http://localhost:4873 --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 ${templateName} --directory ${directory} --template ${templateConfigPath} --verbose --skip-install`,
{
cwd: `${reactNativeRootPath}/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],
};
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');
}
}
install().then(() => {
console.log('Done with preparing the project.');
process.exit();
});