Skip to content

Commit a319002

Browse files
voxsimgaearon
authored andcommitted
Use offline cached version with yarn when it's possible (#1423)
* add --offline flag when we are using yarn and we are offline * Revert changes to init script We only run these commands for backward compat mode, in which we wouldn't receive the offline flag anyway * Don't pass isOnline to init script because it doesn't need it * Don't ping the Yarn registry if user doesn't have Yarn * Remove unused/wrong arguments * Move logs to error handler * Fix error handling * Report to the user that they're offline
1 parent 8591902 commit a319002

File tree

1 file changed

+83
-40
lines changed

1 file changed

+83
-40
lines changed

packages/create-react-app/index.js

+83-40
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ var path = require('path');
5959
var execSync = require('child_process').execSync;
6060
var spawn = require('cross-spawn');
6161
var semver = require('semver');
62+
var dns = require('dns');
6263

6364
var projectName;
6465

@@ -154,25 +155,44 @@ function shouldUseYarn() {
154155
}
155156
}
156157

157-
function install(dependencies, verbose, callback) {
158-
var command;
159-
var args;
160-
if (shouldUseYarn()) {
161-
command = 'yarnpkg';
162-
args = [ 'add', '--exact'].concat(dependencies);
163-
} else {
164-
checkNpmVersion();
165-
command = 'npm';
166-
args = ['install', '--save', '--save-exact'].concat(dependencies);
167-
}
158+
function install(useYarn, dependencies, verbose, isOnline) {
159+
return new Promise(function(resolve, reject) {
160+
var command;
161+
var args;
162+
if (useYarn) {
163+
command = 'yarnpkg';
164+
args = [
165+
'add',
166+
'--exact',
167+
isOnline === false && '--offline'
168+
].concat(dependencies);
169+
170+
if (!isOnline) {
171+
console.log(chalk.yellow('You appear to be offline.'));
172+
console.log(chalk.yellow('Falling back to the local Yarn cache.'));
173+
console.log();
174+
}
168175

169-
if (verbose) {
170-
args.push('--verbose');
171-
}
176+
} else {
177+
checkNpmVersion();
178+
command = 'npm';
179+
args = ['install', '--save', '--save-exact'].concat(dependencies);
180+
}
172181

173-
var child = spawn(command, args, {stdio: 'inherit'});
174-
child.on('close', function(code) {
175-
callback(code, command, args);
182+
if (verbose) {
183+
args.push('--verbose');
184+
}
185+
186+
var child = spawn(command, args, {stdio: 'inherit'});
187+
child.on('close', function(code) {
188+
if (code !== 0) {
189+
reject({
190+
command: command + ' ' + args.join(' ')
191+
});
192+
return;
193+
}
194+
resolve();
195+
});
176196
});
177197
}
178198

@@ -188,11 +208,38 @@ function run(root, appName, version, verbose, originalDirectory, template) {
188208
', and ' + chalk.cyan(packageName) + '...'
189209
);
190210
console.log();
191-
192-
install(allDependencies, verbose, function(code, command, args) {
193-
if (code !== 0) {
211+
212+
var useYarn = shouldUseYarn();
213+
checkIfOnline(useYarn)
214+
.then(function(isOnline) {
215+
return install(useYarn, allDependencies, verbose, isOnline);
216+
})
217+
.then(function() {
218+
checkNodeVersion(packageName);
219+
220+
// Since react-scripts has been installed with --save
221+
// we need to move it into devDependencies and rewrite package.json
222+
// also ensure react dependencies have caret version range
223+
fixDependencies(packageName);
224+
225+
var scriptsPath = path.resolve(
226+
process.cwd(),
227+
'node_modules',
228+
packageName,
229+
'scripts',
230+
'init.js'
231+
);
232+
var init = require(scriptsPath);
233+
init(root, appName, verbose, originalDirectory, template);
234+
})
235+
.catch(function(reason) {
194236
console.log();
195-
console.error('Aborting installation.', chalk.cyan(command + ' ' + args.join(' ')), 'has failed.');
237+
console.log('Aborting installation.');
238+
if (reason.command) {
239+
console.log(' ' + chalk.cyan(reason.command), 'has failed.')
240+
}
241+
console.log();
242+
196243
// On 'exit' we will delete these files from target directory.
197244
var knownGeneratedFiles = [
198245
'package.json', 'npm-debug.log', 'yarn-error.log', 'yarn-debug.log', 'node_modules'
@@ -217,25 +264,7 @@ function run(root, appName, version, verbose, originalDirectory, template) {
217264
}
218265
console.log('Done.');
219266
process.exit(1);
220-
}
221-
222-
checkNodeVersion(packageName);
223-
224-
// Since react-scripts has been installed with --save
225-
// we need to move it into devDependencies and rewrite package.json
226-
// also ensure react dependencies have caret version range
227-
fixDependencies(packageName);
228-
229-
var scriptsPath = path.resolve(
230-
process.cwd(),
231-
'node_modules',
232-
packageName,
233-
'scripts',
234-
'init.js'
235-
);
236-
var init = require(scriptsPath);
237-
init(root, appName, verbose, originalDirectory, template);
238-
});
267+
});
239268
}
240269

241270
function getInstallPackage(version) {
@@ -407,3 +436,17 @@ function isSafeToCreateProjectIn(root) {
407436
return validFiles.indexOf(file) >= 0;
408437
});
409438
}
439+
440+
function checkIfOnline(useYarn) {
441+
if (!useYarn) {
442+
// Don't ping the Yarn registry.
443+
// We'll just assume the best case.
444+
return Promise.resolve(true);
445+
}
446+
447+
return new Promise(function(resolve) {
448+
dns.resolve('registry.yarnpkg.com', function(err) {
449+
resolve(err === null);
450+
});
451+
});
452+
}

0 commit comments

Comments
 (0)