1313
1414const { retry} = require ( '../circleci/retry' ) ;
1515const forEachPackage = require ( '../monorepo/for-each-package' ) ;
16- const setupVerdaccio = require ( './setup-verdaccio' ) ;
16+ const {
17+ VERDACCIO_SERVER_URL ,
18+ VERDACCIO_STORAGE_PATH ,
19+ setupVerdaccio,
20+ } = require ( './setup-verdaccio' ) ;
1721const { parseArgs} = require ( '@pkgjs/parseargs' ) ;
22+ const chalk = require ( 'chalk' ) ;
1823const { execSync} = require ( 'child_process' ) ;
1924const path = require ( 'path' ) ;
2025
2126const REPO_ROOT = path . resolve ( __dirname , '../..' ) ;
22- const NPM_REGISTRY_SERVER = 'http://localhost:4873' ;
2327
2428const config = {
2529 options : {
2630 projectName : { type : 'string' } ,
2731 templatePath : { type : 'string' } ,
2832 directory : { type : 'string' } ,
33+ verbose : { type : 'boolean' , default : false } ,
2934 help : { type : 'boolean' } ,
3035 } ,
3136} ;
3237
3338async function main ( ) {
3439 const {
35- values : { help, projectName , templatePath , directory } ,
40+ values : { help, ... options } ,
3641 } = parseArgs ( config ) ;
3742
3843 if ( help ) {
@@ -55,94 +60,119 @@ async function main() {
5560 --projectName The name of the new React Native project.
5661 --templatePath The absolute path to the folder containing the template.
5762 --directory The absolute path to the target project directory.
63+ --verbose Print additional output. Default: false.
5864 ` ) ;
5965 return ;
6066 }
6167
62- const VERDACCIO_PID = setupVerdaccio ( ) ;
68+ await initNewProjectFromSource ( options ) ;
69+ }
6370
64- try {
65- process . stdout . write ( 'Bootstrapped Verdaccio \u2705\n' ) ;
71+ async function initNewProjectFromSource (
72+ {
73+ projectName,
74+ templatePath,
75+ directory,
76+ verbose = false ,
77+ } /*: {projectName: string, templatePath: string, directory: string, verbose?: boolean} */ ,
78+ ) {
79+ console . log ( 'Starting local npm proxy (Verdaccio)' ) ;
80+ const verdaccioPid = setupVerdaccio ( ) ;
81+ console . log ( 'Done ✅' ) ;
6682
67- process . stdout . write ( 'Building packages...\n' ) ;
83+ try {
6884 execSync ( 'node ./scripts/build/build.js' , {
6985 cwd : REPO_ROOT ,
70- stdio : [ process . stdin , process . stdout , process . stderr ] ,
86+ stdio : 'inherit' ,
7187 } ) ;
88+ console . log ( '\nDone ✅' ) ;
7289
73- process . stdout . write ( 'Starting to publish every package... \n') ;
90+ console . log ( 'Publishing packages to local npm proxy \n') ;
7491 forEachPackage (
7592 ( packageAbsolutePath , packageRelativePathFromRoot , packageManifest ) => {
7693 if ( packageManifest . private ) {
7794 return ;
7895 }
7996
97+ const desc = `${ packageManifest . name } (${ packageRelativePathFromRoot } )` ;
98+ process . stdout . write (
99+ `${ desc } ${ chalk . dim ( '.' ) . repeat ( Math . max ( 0 , 72 - desc . length ) ) } ` ,
100+ ) ;
80101 execSync (
81- `npm publish --registry ${ NPM_REGISTRY_SERVER } --access public` ,
102+ `npm publish --registry ${ VERDACCIO_SERVER_URL } --access public` ,
82103 {
83104 cwd : packageAbsolutePath ,
84- stdio : [ process . stdin , process . stdout , process . stderr ] ,
105+ stdio : verbose ? 'inherit' : [ process . stderr ] ,
85106 } ,
86107 ) ;
87-
88- process . stdout . write (
89- `Published ${ packageManifest . name } to proxy \u2705\n` ,
90- ) ;
108+ process . stdout . write ( chalk . reset . inverse . bold . green ( ' DONE ' ) + '\n' ) ;
91109 } ,
92110 ) ;
111+ console . log ( '\nDone ✅' ) ;
93112
94- process . stdout . write ( 'Published every package \u2705\n' ) ;
95-
113+ console . log ( 'Running react-native init without install' ) ;
96114 execSync (
97- `node cli.js init ${ projectName } \
115+ `node ./packages/react-native/ cli.js init ${ projectName } \
98116 --directory ${ directory } \
99117 --template ${ templatePath } \
100118 --verbose \
101119 --skip-install \
102- --yarn-config-options npmRegistryServer="${ NPM_REGISTRY_SERVER } "` ,
120+ --yarn-config-options npmRegistryServer="${ VERDACCIO_SERVER_URL } "` ,
103121 {
104- cwd : `${ REPO_ROOT } /packages/react-native` ,
105- stdio : [ process . stdin , process . stdout , process . stderr ] ,
122+ // Avoid loading packages/react-native/react-native.config.js
123+ cwd : REPO_ROOT ,
124+ stdio : verbose ? 'inherit' : [ process . stderr ] ,
106125 } ,
107126 ) ;
108- process . stdout . write ( 'Completed initialization of template app \u2705\n' ) ;
109-
110- process . stdout . write ( 'Installing dependencies in template app folder...\n' ) ;
111- const options = {
112- cwd : directory ,
113- stdio : [ process . stdin , process . stdout , process . stderr ] ,
114- } ;
115-
116- execSync (
117- `yarn config set npmRegistryServer "${ NPM_REGISTRY_SERVER } "` ,
118- options ,
119- ) ;
120-
121- execSync (
122- 'yarn config set unsafeHttpWhitelist --json \'["localhost"]\'' ,
123- options ,
124- ) ;
125-
126- const success = await retry ( 'yarn' , options , 3 , 500 , [ 'install' ] ) ;
127+ console . log ( '\nDone ✅' ) ;
128+
129+ console . log ( 'Installing project dependencies' ) ;
130+ await runYarnUsingProxy ( directory ) ;
131+ console . log ( 'Done ✅' ) ;
132+ } catch ( e ) {
133+ console . log ( 'Failed ❌' ) ;
134+ throw e ;
135+ } finally {
136+ console . log ( `Cleanup: Killing Verdaccio process (PID: ${ verdaccioPid } )` ) ;
137+ execSync ( `kill -9 ${ verdaccioPid } ` ) ;
138+ console . log ( 'Done ✅' ) ;
127139
128- if ( ! success ) {
129- process . stdout . write (
130- 'Failed to install dependencies in template app folder.' ,
131- ) ;
132- throw new Error ( 'Failed to install dependencies in template app folder.' ) ;
133- }
140+ console . log ( 'Cleanup: Removing Verdaccio storage directory' ) ;
141+ execSync ( `rm -rf ${ VERDACCIO_STORAGE_PATH } ` ) ;
142+ console . log ( 'Done ✅' ) ;
134143
135- process . stdout . write ( 'Installed dependencies via Yarn \u2705\n' ) ;
136- } finally {
137- process . stdout . write ( `Killing verdaccio. PID — ${ VERDACCIO_PID } ...\n` ) ;
138- execSync ( `kill -9 ${ VERDACCIO_PID } ` ) ;
139- process . stdout . write ( 'Killed Verdaccio process \u2705\n' ) ;
140144 // TODO(huntie): Fix memory leak from `spawn` in `setupVerdaccio` (above
141145 // kill command does not wait for kill success).
142146 process . exit ( 0 ) ;
143147 }
144148}
145149
150+ async function runYarnUsingProxy ( cwd /*: string */ ) {
151+ const execOptions = {
152+ cwd,
153+ stdio : 'inherit' ,
154+ } ;
155+ execSync (
156+ `yarn config set npmRegistryServer "${ VERDACCIO_SERVER_URL } "` ,
157+ execOptions ,
158+ ) ;
159+ execSync (
160+ 'yarn config set unsafeHttpWhitelist --json \'["localhost"]\'' ,
161+ execOptions ,
162+ ) ;
163+
164+ // TODO(huntie): Review pre-existing retry limit
165+ const success = await retry ( 'yarn' , execOptions , 3 , 500 , [ 'install' ] ) ;
166+
167+ if ( ! success ) {
168+ throw new Error ( 'Failed to install project dependencies' ) ;
169+ }
170+ }
171+
172+ module . exports = {
173+ initNewProjectFromSource,
174+ } ;
175+
146176if ( require . main === module ) {
147177 // eslint-disable-next-line no-void
148178 void main ( ) ;
0 commit comments