@@ -64,8 +64,6 @@ module.exports = function(appPath, appName, verbose, originalDirectory, template
64
64
}
65
65
} ) ;
66
66
67
- // Run yarn or npm for react and react-dom
68
- // TODO: having to do two npm/yarn installs is bad, can we avoid it?
69
67
var command ;
70
68
var args ;
71
69
@@ -92,53 +90,65 @@ module.exports = function(appPath, appName, verbose, originalDirectory, template
92
90
fs . unlinkSync ( templateDependenciesPath ) ;
93
91
}
94
92
95
- console . log ( 'Installing react and react-dom using ' + command + '...' ) ;
96
- console . log ( ) ;
93
+ // Install react and react-dom for backward compatibility with old CRA cli
94
+ // which doesn't install react and react-dom along with react-scripts
95
+ // or template is presetend (via --internal-testing-template)
96
+ if ( ! isReactInstalled ( appPackage ) || template ) {
97
+ console . log ( 'Installing react and react-dom using ' + command + '...' ) ;
98
+ console . log ( ) ;
97
99
98
- var proc = spawn ( command , args , { stdio : 'inherit' } ) ;
99
- proc . on ( 'close' , function ( code ) {
100
- if ( code !== 0 ) {
100
+ var proc = spawn . sync ( command , args , { stdio : 'inherit' } ) ;
101
+ if ( proc . status !== 0 ) {
101
102
console . error ( '`' + command + ' ' + args . join ( ' ' ) + '` failed' ) ;
102
103
return ;
103
104
}
105
+ }
104
106
105
- // Display the most elegant way to cd.
106
- // This needs to handle an undefined originalDirectory for
107
- // backward compatibility with old global-cli's.
108
- var cdpath ;
109
- if ( originalDirectory &&
110
- path . join ( originalDirectory , appName ) === appPath ) {
111
- cdpath = appName ;
112
- } else {
113
- cdpath = appPath ;
114
- }
107
+ // Display the most elegant way to cd.
108
+ // This needs to handle an undefined originalDirectory for
109
+ // backward compatibility with old global-cli's.
110
+ var cdpath ;
111
+ if ( originalDirectory &&
112
+ path . join ( originalDirectory , appName ) === appPath ) {
113
+ cdpath = appName ;
114
+ } else {
115
+ cdpath = appPath ;
116
+ }
115
117
118
+ console . log ( ) ;
119
+ console . log ( 'Success! Created ' + appName + ' at ' + appPath ) ;
120
+ console . log ( 'Inside that directory, you can run several commands:' ) ;
121
+ console . log ( ) ;
122
+ console . log ( chalk . cyan ( ' ' + command + ' start' ) ) ;
123
+ console . log ( ' Starts the development server.' ) ;
124
+ console . log ( ) ;
125
+ console . log ( chalk . cyan ( ' ' + command + ' run build' ) ) ;
126
+ console . log ( ' Bundles the app into static files for production.' ) ;
127
+ console . log ( ) ;
128
+ console . log ( chalk . cyan ( ' ' + command + ' test' ) ) ;
129
+ console . log ( ' Starts the test runner.' ) ;
130
+ console . log ( ) ;
131
+ console . log ( chalk . cyan ( ' ' + command + ' run eject' ) ) ;
132
+ console . log ( ' Removes this tool and copies build dependencies, configuration files' ) ;
133
+ console . log ( ' and scripts into the app directory. If you do this, you can’t go back!' ) ;
134
+ console . log ( ) ;
135
+ console . log ( 'We suggest that you begin by typing:' ) ;
136
+ console . log ( ) ;
137
+ console . log ( chalk . cyan ( ' cd' ) , cdpath ) ;
138
+ console . log ( ' ' + chalk . cyan ( command + ' start' ) ) ;
139
+ if ( readmeExists ) {
116
140
console . log ( ) ;
117
- console . log ( 'Success! Created ' + appName + ' at ' + appPath ) ;
118
- console . log ( 'Inside that directory, you can run several commands:' ) ;
119
- console . log ( ) ;
120
- console . log ( chalk . cyan ( ' ' + command + ' start' ) ) ;
121
- console . log ( ' Starts the development server.' ) ;
122
- console . log ( ) ;
123
- console . log ( chalk . cyan ( ' ' + command + ' run build' ) ) ;
124
- console . log ( ' Bundles the app into static files for production.' ) ;
125
- console . log ( ) ;
126
- console . log ( chalk . cyan ( ' ' + command + ' test' ) ) ;
127
- console . log ( ' Starts the test runner.' ) ;
128
- console . log ( ) ;
129
- console . log ( chalk . cyan ( ' ' + command + ' run eject' ) ) ;
130
- console . log ( ' Removes this tool and copies build dependencies, configuration files' ) ;
131
- console . log ( ' and scripts into the app directory. If you do this, you can’t go back!' ) ;
132
- console . log ( ) ;
133
- console . log ( 'We suggest that you begin by typing:' ) ;
134
- console . log ( ) ;
135
- console . log ( chalk . cyan ( ' cd' ) , cdpath ) ;
136
- console . log ( ' ' + chalk . cyan ( command + ' start' ) ) ;
137
- if ( readmeExists ) {
138
- console . log ( ) ;
139
- console . log ( chalk . yellow ( 'You had a `README.md` file, we renamed it to `README.old.md`' ) ) ;
140
- }
141
- console . log ( ) ;
142
- console . log ( 'Happy hacking!' ) ;
143
- } ) ;
141
+ console . log ( chalk . yellow ( 'You had a `README.md` file, we renamed it to `README.old.md`' ) ) ;
142
+ }
143
+ console . log ( ) ;
144
+ console . log ( 'Happy hacking!' ) ;
144
145
} ;
146
+
147
+ function isReactInstalled ( appPackage ) {
148
+ var dependencies = appPackage . dependencies || { } ;
149
+
150
+ return (
151
+ typeof dependencies . react !== 'undefined' &&
152
+ typeof dependencies [ 'react-dom' ] !== 'undefined'
153
+ )
154
+ }
0 commit comments