@@ -20,13 +20,15 @@ const argv = minimist(process.argv.slice(2));
2020 * --version - to print current version
2121 * --verbose - to print npm logs during init
2222 * --scripts-version <alternative package>
23+ * --package-manager <package manager name or path>
2324 * Example of valid values:
2425 * - a specific npm version: "0.22.0-rc1"
2526 * - a .tgz archive from npm: "https://registry.npmjs.org/react-native-scripts/-/react-native-scripts-0.20.0.tgz"
2627 * - a package from `tasks/clean_pack.sh`: "/home/adam/create-react-native-app/react-native-scripts-0.22.0.tgz"
2728 */
2829const commands = argv . _ ;
2930const cwd = process . cwd ( ) ;
31+ const packageManager = argv [ 'package-manager' ] ;
3032
3133if ( commands . length === 0 ) {
3234 if ( argv . version ) {
@@ -40,8 +42,7 @@ if (commands.length === 0) {
4042
4143createApp ( commands [ 0 ] , ! ! argv . verbose , argv [ 'scripts-version' ] ) . then ( ( ) => { } ) ;
4244
43- // use yarn if it's available, otherwise use npm
44- function shouldUseYarn ( ) {
45+ function isRespondYarn ( ) {
4546 try {
4647 const result = spawn . sync ( 'yarnpkg' , [ '--version' ] , { stdio : 'ignore' } ) ;
4748 if ( result . error || result . status !== 0 ) {
@@ -53,6 +54,31 @@ function shouldUseYarn() {
5354 }
5455}
5556
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+
5682async function createApp ( name : string , verbose : boolean , version : ?string ) : Promise < void > {
5783 const root = path . resolve ( name ) ;
5884 const appName = path . basename ( root ) ;
@@ -79,6 +105,7 @@ async function createApp(name: string, verbose: boolean, version: ?string): Prom
79105 await fse . writeFile ( path . join ( root , 'package.json' ) , JSON . stringify ( packageJson , null , 2 ) ) ;
80106 process . chdir ( root ) ;
81107
108+ console . log ( `Using package manager as ${ packageManagerCmd ( ) } with ${ packageManagerType ( ) } interface.` )
82109 console . log ( 'Installing packages. This might take a couple minutes.' ) ;
83110 console . log ( 'Installing react-native-scripts...' ) ;
84111 console . log ( ) ;
@@ -91,11 +118,11 @@ function install(
91118 verbose : boolean ,
92119 callback : ( code : number , command : string , args : Array < string > ) => Promise < void >
93120) : void {
94- const useYarn = shouldUseYarn ( ) ;
95- let args , cmd, result ;
121+ const type = packageManagerType ( ) ;
122+ let args , result ;
123+ let cmd = packageManagerCmd ( ) ;
96124
97- if ( useYarn ) {
98- cmd = 'yarnpkg' ;
125+ if ( type === 'yarn' ) {
99126 args = [ 'add' ] ;
100127
101128 if ( verbose ) {
@@ -110,7 +137,6 @@ function install(
110137 if ( verbose ) {
111138 args . push ( '--verbose' ) ;
112139 }
113- cmd = 'npm ';
114140 args = args . concat ( [ '--save-dev' , '--save-exact' , packageToInstall ] ) ;
115141
116142 result = spawn . sync ( cmd , args , { stdio : 'inherit' } ) ;
0 commit comments