1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 *
7 * @format
8 */
9
10'use strict';
11
12const yargs = require('yargs');
13const {execSync, spawnSync} = require('child_process');
14
15const forEachPackage = require('../monorepo/for-each-package');
16const setupVerdaccio = require('../setup-verdaccio');
17
18const {argv} = yargs
19  .option('r', {
20    alias: 'reactNativeRootPath',
21    describe: 'Path to root folder of react-native',
22    required: true,
23  })
24  .option('n', {
25    alias: 'templateName',
26    describe: 'Template App name',
27    required: true,
28  })
29  .option('tcp', {
30    alias: 'templateConfigPath',
31    describe: 'Path to folder containing template config',
32    required: true,
33  })
34  .option('d', {
35    alias: 'directory',
36    describe: 'Path to template application folder',
37    required: true,
38  })
39  .strict();
40
41const {reactNativeRootPath, templateName, templateConfigPath, directory} = argv;
42
43const VERDACCIO_CONFIG_PATH = `${reactNativeRootPath}/.circleci/verdaccio.yml`;
44
45function install() {
46  const VERDACCIO_PID = setupVerdaccio(
47    reactNativeRootPath,
48    VERDACCIO_CONFIG_PATH,
49  );
50  process.stdout.write('Bootstrapped Verdaccio \u2705\n');
51
52  process.stdout.write('Starting to publish every package...\n');
53  forEachPackage(
54    (packageAbsolutePath, packageRelativePathFromRoot, packageManifest) => {
55      if (packageManifest.private) {
56        return;
57      }
58
59      execSync('npm publish --registry http://localhost:4873 --access public', {
60        cwd: packageAbsolutePath,
61        stdio: [process.stdin, process.stdout, process.stderr],
62      });
63
64      process.stdout.write(
65        `Published ${packageManifest.name} to proxy \u2705\n`,
66      );
67    },
68  );
69
70  process.stdout.write('Published every package \u2705\n');
71
72  execSync(
73    `node cli.js init ${templateName} --directory ${directory} --template ${templateConfigPath} --verbose --skip-install`,
74    {
75      cwd: reactNativeRootPath,
76      stdio: [process.stdin, process.stdout, process.stderr],
77    },
78  );
79  process.stdout.write('Completed initialization of template app \u2705\n');
80
81  process.stdout.write('Installing dependencies in template app folder...\n');
82  spawnSync('yarn', ['install'], {
83    cwd: directory,
84    stdio: [process.stdin, process.stdout, process.stderr],
85  });
86  process.stdout.write('Installed dependencies via Yarn \u2705\n');
87
88  process.stdout.write(`Killing verdaccio. PID — ${VERDACCIO_PID}...\n`);
89  execSync(`kill -9 ${VERDACCIO_PID}`);
90  process.stdout.write('Killed Verdaccio process \u2705\n');
91
92  process.exit();
93}
94
95install();
96