Skip to content

Commit

Permalink
feat(react-native/template): use verdaccio to publish local packages …
Browse files Browse the repository at this point in the history
…before testing template app (#35444)

Summary:
Pull Request resolved: #35444

Changelog:
[Internal][Changed] - now using Verdaccio to publish necessary packages for template app

- Adds script `/scripts/template/install-dependencies.js`, which incapsulates the logic of installing dependencies of template app
- The idea of the script is to run verdaccio and publish all necessary packages to node_modules, since these packages might not yet be present on npm
- This should also potentially resolve some template app test failures on CircleCI related to package-ifying Animated, VirtualizedList, FlatList modules

Reviewed By: cortinico

Differential Revision: D41498086

fbshipit-source-id: 48fbbb1c9334e7a9e7657e6275b7b04f9ce290b5
  • Loading branch information
hoxyq authored and Ruslan Lesiutin committed Jan 24, 2023
1 parent 1ca223f commit 12f65b5
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 19 deletions.
4 changes: 1 addition & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -936,9 +936,7 @@ jobs:
- run:
name: Install iOS dependencies - Configuration << parameters.flavor >>; New Architecture << parameters.architecture >>; JS Engine << parameters.jsengine>>; Flipper << parameters.flipper >>
command: |
cd /tmp/$PROJECT_NAME
yarn install
cd ios
cd /tmp/$PROJECT_NAME/ios
bundle install
Expand Down
5 changes: 4 additions & 1 deletion scripts/run-ci-e2e-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ const REACT_NATIVE_TEMP_DIR = exec(
).stdout.trim();
const REACT_NATIVE_APP_DIR = `${REACT_NATIVE_TEMP_DIR}/template`;
const numberOfRetries = argv.retries || 1;

const VERDACCIO_CONFIG_PATH = path.join(ROOT, '.circleci/verdaccio.yml');

let SERVER_PID;
let APPIUM_PID;
let VERDACCIO_PID;
Expand Down Expand Up @@ -75,7 +78,7 @@ try {
const REACT_NATIVE_PACKAGE = path.join(ROOT, 'react-native-*.tgz');

describe('Set up Verdaccio');
VERDACCIO_PID = setupVerdaccio();
VERDACCIO_PID = setupVerdaccio(ROOT, VERDACCIO_CONFIG_PATH);

describe('Publish packages');
forEachPackage(
Expand Down
49 changes: 34 additions & 15 deletions scripts/setup-verdaccio.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,41 @@

'use strict';

const {exec} = require('shelljs');
const spawn = require('child_process').spawn;

function setupVerdaccio() {
const verdaccioProcess = spawn('npx', [
'verdaccio@5.15.3',
'--config',
'.circleci/verdaccio.yml',
]);
const {execSync, spawn} = require('child_process');

function setupVerdaccio(
reactNativeRootPath, // Path to React Native root folder
verdaccioConfigPath, // Path to Verdaccio config file, which you want to use for bootstrapping Verdaccio
verdaccioStoragePath, // Path to Verdaccio storage, where it should keep packages. Optional. Default value will be decided by your Verdaccio config
) {
if (!reactNativeRootPath) {
throw new Error(
'Path to React Native repo root is not specified. You should provide it as a first argument',
);
}

if (!verdaccioConfigPath) {
throw new Error(
'Path to Verdaccio config is not specified. You should provide it as a second argument',
);
}

const verdaccioProcess = spawn(
'npx',
['verdaccio@5.16.3', '--config', verdaccioConfigPath],
{env: {...process.env, VERDACCIO_STORAGE_PATH: verdaccioStoragePath}},
);

const VERDACCIO_PID = verdaccioProcess.pid;
exec('npx wait-on@6.0.1 http://localhost:4873');
exec('npm set registry http://localhost:4873');
exec('echo "//localhost:4873/:_authToken=secretToken" > .npmrc');

execSync('npx wait-on@6.0.1 http://localhost:4873');

execSync('npm set registry http://localhost:4873');
execSync('echo "//localhost:4873/:_authToken=secretToken" > .npmrc', {
cwd: reactNativeRootPath,
});

return VERDACCIO_PID;
}

module.exports = {
setupVerdaccio: setupVerdaccio,
};
module.exports = setupVerdaccio;
30 changes: 30 additions & 0 deletions scripts/template/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
## Why?

The main purpose of `install-dependencies.js` is to bootstrap [Verdaccio](https://verdaccio.org/docs/what-is-verdaccio). It will host all the local packages, which are not yet present on npm registry. In the near future this should help us in keep template tests green, because once we move to [monorepo structure](https://github.com/react-native-community/discussions-and-proposals/pull/480), template app may use some versions of dependencies that are not yet present on npm registry.

## I have migrated some module to package, which is not yet published to npm, how to use it?

First of all, you need to modify [Verdaccio config](https://github.com/facebook/react-native/tree/main/scripts/template/verdaccio.yml):
```diff
packages:
+ '<my-migrated-package-name>':
+ access: $all
+ publish: $all
'@*/*':
access: $all
publish: $authenticated
proxy: npmjs
'**':
access: $all
publish: $all
proxy: npmjs
```

After that, you should modify [install-dependencies script](https://github.com/facebook/react-native/tree/main/scripts/template/install-dependencies.js) to include your package for publishing

```diff
const PACKAGES_TO_PUBLISH_PATHS = [
...
+ "packages/<your-package-folder-name>"
];
```
67 changes: 67 additions & 0 deletions scripts/template/install-dependencies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* 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, spawnSync} = require('child_process');
const setupVerdaccio = require('../setup-verdaccio');

const {argv} = yargs
.option('r', {
alias: 'reactNativeRootPath',
describe: 'Path to root folder of react-native',
required: true,
})
.option('c', {
alias: 'templatePath',
describe: 'Path to template application folder',
required: true,
})
.strict();

const {reactNativeRootPath, templatePath} = argv;

const VERDACCIO_CONFIG_PATH = `${reactNativeRootPath}/scripts/template/verdaccio.yml`;
const VERDACCIO_STORAGE_PATH = `${templatePath}/node_modules`;

const PACKAGES_TO_PUBLISH_PATHS = [];

function install() {
const VERDACCIO_PID = setupVerdaccio(
reactNativeRootPath,
VERDACCIO_CONFIG_PATH,
VERDACCIO_STORAGE_PATH,
);
process.stdout.write('Bootstrapped Verdaccio \u2705\n');

// Publish all necessary packages...
for (const packagePath of PACKAGES_TO_PUBLISH_PATHS) {
execSync('npm publish --registry http://localhost:4873 --access public', {
cwd: `${reactNativeRootPath}/${packagePath}`,
stdio: [process.stdin, process.stdout, process.stderr],
});

process.stdout.write(`Published /${packagePath} to proxy \u2705\n`);
}

spawnSync('yarn', ['install'], {
cwd: templatePath,
stdio: [process.stdin, process.stdout, process.stderr],
});
process.stdout.write('Installed dependencies via Yarn \u2705\n');

process.stdout.write(`Killing verdaccio. PID — ${VERDACCIO_PID}...\n`);
execSync(`kill -9 ${VERDACCIO_PID}`);
process.stdout.write('Killed Verdaccio process \u2705\n');

process.exit();
}

install();
27 changes: 27 additions & 0 deletions scripts/template/verdaccio.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
storage: ./storage
auth:
htpasswd:
file: ./htpasswd
uplinks:
npmjs:
url: https://registry.npmjs.org/
max_fails: 40
maxage: 30m
timeout: 60s
fail_timeout: 10m
cache: false
agent_options:
keepAlive: true
maxSockets: 40
maxFreeSockets: 10
packages:
'@*/*':
access: $all
publish: $authenticated
proxy: npmjs
'**':
access: $all
publish: $all
proxy: npmjs
logs:
- {type: file, path: verdaccio.log, format: json, level: warn}

0 comments on commit 12f65b5

Please sign in to comment.