@@ -20,13 +20,15 @@ const argv = minimist(process.argv.slice(2));
20
20
* --version - to print current version
21
21
* --verbose - to print npm logs during init
22
22
* --scripts-version <alternative package>
23
+ * --package-manager <package manager name or path>
23
24
* Example of valid values:
24
25
* - a specific npm version: "0.22.0-rc1"
25
26
* - a .tgz archive from npm: "https://registry.npmjs.org/react-native-scripts/-/react-native-scripts-0.20.0.tgz"
26
27
* - a package from `tasks/clean_pack.sh`: "/home/adam/create-react-native-app/react-native-scripts-0.22.0.tgz"
27
28
*/
28
29
const commands = argv . _ ;
29
30
const cwd = process . cwd ( ) ;
31
+ const packageManager = argv [ 'package-manager' ] ;
30
32
31
33
if ( commands . length === 0 ) {
32
34
if ( argv . version ) {
@@ -40,8 +42,7 @@ if (commands.length === 0) {
40
42
41
43
createApp ( commands [ 0 ] , ! ! argv . verbose , argv [ 'scripts-version' ] ) . then ( ( ) => { } ) ;
42
44
43
- // use yarn if it's available, otherwise use npm
44
- function shouldUseYarn ( ) {
45
+ function isRespondYarn ( ) {
45
46
try {
46
47
const result = spawn . sync ( 'yarnpkg' , [ '--version' ] , { stdio : 'ignore' } ) ;
47
48
if ( result . error || result . status !== 0 ) {
@@ -53,6 +54,31 @@ function shouldUseYarn() {
53
54
}
54
55
}
55
56
57
+ // This decides the 'interface' of the package managing command.
58
+ // Ex: If it guesses the type of package manager as 'yarn',
59
+ // then it executes '(yarn) add' command instead of '(npm) install'.
60
+ function packageManagerType ( ) {
61
+ const defaultType = 'npm' ;
62
+ const supportedTypes = [ 'yarn' , 'npm' ] ;
63
+
64
+ if ( packageManager ) {
65
+ let t = supportedTypes . find ( type => {
66
+ return ( packageManager . indexOf ( type ) > - 1 ) ;
67
+ } )
68
+ return t ? t : defaultType ;
69
+ }
70
+
71
+ return isRespondYarn ( ) ? 'yarn' : defaultType ;
72
+ }
73
+
74
+ function packageManagerCmd ( ) {
75
+ if ( packageManager ) {
76
+ return packageManager ;
77
+ } else {
78
+ return packageManagerType ( ) === 'yarn' ? 'yarnpkg' : 'npm' ;
79
+ }
80
+ }
81
+
56
82
async function createApp ( name : string , verbose : boolean , version : ?string ) : Promise < void > {
57
83
const root = path . resolve ( name ) ;
58
84
const appName = path . basename ( root ) ;
@@ -79,6 +105,7 @@ async function createApp(name: string, verbose: boolean, version: ?string): Prom
79
105
await fse . writeFile ( path . join ( root , 'package.json' ) , JSON . stringify ( packageJson , null , 2 ) ) ;
80
106
process . chdir ( root ) ;
81
107
108
+ console . log ( `Using package manager as ${ packageManagerCmd ( ) } with ${ packageManagerType ( ) } interface.` )
82
109
console . log ( 'Installing packages. This might take a couple minutes.' ) ;
83
110
console . log ( 'Installing react-native-scripts...' ) ;
84
111
console . log ( ) ;
@@ -91,11 +118,11 @@ function install(
91
118
verbose : boolean ,
92
119
callback : ( code : number , command : string , args : Array < string > ) => Promise < void >
93
120
) : void {
94
- const useYarn = shouldUseYarn ( ) ;
95
- let args , cmd, result ;
121
+ const type = packageManagerType ( ) ;
122
+ let args , result ;
123
+ let cmd = packageManagerCmd ( ) ;
96
124
97
- if ( useYarn ) {
98
- cmd = 'yarnpkg' ;
125
+ if ( type === 'yarn' ) {
99
126
args = [ 'add' ] ;
100
127
101
128
if ( verbose ) {
@@ -110,7 +137,6 @@ function install(
110
137
if ( verbose ) {
111
138
args . push ( '--verbose' ) ;
112
139
}
113
- cmd = 'npm ';
114
140
args = args . concat ( [ '--save-dev' , '--save-exact' , packageToInstall ] ) ;
115
141
116
142
result = spawn . sync ( cmd , args , { stdio : 'inherit' } ) ;
0 commit comments