11
11
12
12
'use strict' ;
13
13
14
+ /*:: import type {ProjectInfo} from '../utils/monorepo'; */
15
+
14
16
const { retry} = require ( '../circleci/retry' ) ;
15
17
const { REPO_ROOT } = require ( '../consts' ) ;
16
18
const { getPackages} = require ( '../utils/monorepo' ) ;
@@ -22,12 +24,12 @@ const {
22
24
const { parseArgs} = require ( '@pkgjs/parseargs' ) ;
23
25
const chalk = require ( 'chalk' ) ;
24
26
const { execSync} = require ( 'child_process' ) ;
27
+ const fs = require ( 'fs' ) ;
25
28
const path = require ( 'path' ) ;
26
29
27
30
const config = {
28
31
options : {
29
32
projectName : { type : 'string' } ,
30
- templatePath : { type : 'string' } ,
31
33
directory : { type : 'string' } ,
32
34
verbose : { type : 'boolean' , default : false } ,
33
35
help : { type : 'boolean' } ,
@@ -56,10 +58,10 @@ async function main() {
56
58
should not be committed.
57
59
58
60
Options:
59
- --projectName The name of the new React Native project.
60
- --templatePath The absolute path to the folder containing the template .
61
- --directory The absolute path to the target project directory .
62
- --verbose Print additional output. Default: false.
61
+ --projectName The name of the new React Native project.
62
+ --directory The absolute path to the target project directory .
63
+ --pathToLocalReactNative The absolute path to the local react-native package .
64
+ --verbose Print additional output. Default: false.
63
65
` ) ;
64
66
return ;
65
67
}
@@ -74,10 +76,10 @@ async function main() {
74
76
async function initNewProjectFromSource (
75
77
{
76
78
projectName,
77
- templatePath,
78
79
directory,
80
+ pathToLocalReactNative = null ,
79
81
verbose = false ,
80
- } /*: {projectName: string, templatePath : string, directory: string, verbose?: boolean} */ ,
82
+ } /*: {projectName: string, directory : string, pathToLocalReactNative?: ? string, verbose?: boolean} */ ,
81
83
) {
82
84
console . log ( 'Starting local npm proxy (Verdaccio)' ) ;
83
85
const verdaccioPid = setupVerdaccio ( ) ;
@@ -117,9 +119,9 @@ async function initNewProjectFromSource(
117
119
118
120
console . log ( 'Running react-native init without install' ) ;
119
121
execSync (
120
- `node ./packages/ react-native/cli.js init ${ projectName } \
122
+ `npx @ react-native-community /cli@next init ${ projectName } \
121
123
--directory ${ directory } \
122
- --template ${ templatePath } \
124
+ --version 0.75.0-rc.2 \
123
125
--verbose \
124
126
--pm npm \
125
127
--skip-install` ,
@@ -131,6 +133,9 @@ async function initNewProjectFromSource(
131
133
) ;
132
134
console . log ( '\nDone ✅' ) ;
133
135
136
+ _updateScopedPackages ( packages , directory ) ;
137
+ _updateReactNativeInTemplateIfNeeded ( pathToLocalReactNative , directory ) ;
138
+
134
139
console . log ( 'Installing project dependencies' ) ;
135
140
await installProjectUsingProxy ( directory ) ;
136
141
console . log ( 'Done ✅' ) ;
@@ -169,6 +174,61 @@ async function installProjectUsingProxy(cwd /*: string */) {
169
174
}
170
175
}
171
176
177
+ function _updateScopedPackages (
178
+ packages /*: ProjectInfo */ ,
179
+ directory /*: string */ ,
180
+ ) {
181
+ console . log (
182
+ 'Updating the scoped packagesto match the version published in Verdaccio' ,
183
+ ) ;
184
+
185
+ // Packages are updated in a lockstep and all with the same version.
186
+ // Pick the version from the first package
187
+ const version = packages [ Object . keys ( packages ) [ 0 ] ] . packageJson . version ;
188
+
189
+ // Update scoped packages which starts with @react -native
190
+ const appPackageJsonPath = path . join ( directory , 'package.json' ) ;
191
+ const appPackageJson = JSON . parse (
192
+ fs . readFileSync ( appPackageJsonPath , 'utf8' ) ,
193
+ ) ;
194
+
195
+ for ( const [ key , _ ] of Object . entries ( appPackageJson . dependencies ) ) {
196
+ if ( key . startsWith ( '@react-native' ) ) {
197
+ appPackageJson . dependencies [ key ] = version ;
198
+ }
199
+ }
200
+ for ( const [ key , _ ] of Object . entries ( appPackageJson . devDependencies ) ) {
201
+ if ( key . startsWith ( '@react-native' ) ) {
202
+ appPackageJson . devDependencies [ key ] = version ;
203
+ }
204
+ }
205
+
206
+ fs . writeFileSync ( appPackageJsonPath , JSON . stringify ( appPackageJson , null , 2 ) ) ;
207
+
208
+ console . log ( 'Done ✅' ) ;
209
+ }
210
+
211
+ function _updateReactNativeInTemplateIfNeeded (
212
+ pathToLocalReactNative /*: ?string */ ,
213
+ directory /*: string */ ,
214
+ ) {
215
+ if ( pathToLocalReactNative != null ) {
216
+ console . log ( 'Updating the template version to local react-native' ) ;
217
+ // Update template version.
218
+ const appPackageJsonPath = path . join ( directory , 'package.json' ) ;
219
+ const appPackageJson = JSON . parse (
220
+ fs . readFileSync ( appPackageJsonPath , 'utf8' ) ,
221
+ ) ;
222
+ appPackageJson . dependencies [ 'react-native' ] =
223
+ `file:${ pathToLocalReactNative } ` ;
224
+ fs . writeFileSync (
225
+ appPackageJsonPath ,
226
+ JSON . stringify ( appPackageJson , null , 2 ) ,
227
+ ) ;
228
+ console . log ( 'Done ✅' ) ;
229
+ }
230
+ }
231
+
172
232
module . exports = {
173
233
initNewProjectFromSource,
174
234
} ;
0 commit comments