Commit bf948bf2 authored by Simon Vocella's avatar Simon Vocella Committed by Dan Abramov
Browse files

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
parent 2b824d8b
Showing with 83 additions and 40 deletions
+83 -40
...@@ -59,6 +59,7 @@ var path = require('path'); ...@@ -59,6 +59,7 @@ var path = require('path');
var execSync = require('child_process').execSync; var execSync = require('child_process').execSync;
var spawn = require('cross-spawn'); var spawn = require('cross-spawn');
var semver = require('semver'); var semver = require('semver');
var dns = require('dns');
var projectName; var projectName;
...@@ -154,25 +155,44 @@ function shouldUseYarn() { ...@@ -154,25 +155,44 @@ function shouldUseYarn() {
} }
} }
function install(dependencies, verbose, callback) { function install(useYarn, dependencies, verbose, isOnline) {
var command; return new Promise(function(resolve, reject) {
var args; var command;
if (shouldUseYarn()) { var args;
command = 'yarnpkg'; if (useYarn) {
args = [ 'add', '--exact'].concat(dependencies); command = 'yarnpkg';
} else { args = [
checkNpmVersion(); 'add',
command = 'npm'; '--exact',
args = ['install', '--save', '--save-exact'].concat(dependencies); isOnline === false && '--offline'
} ].concat(dependencies);
if (!isOnline) {
console.log(chalk.yellow('You appear to be offline.'));
console.log(chalk.yellow('Falling back to the local Yarn cache.'));
console.log();
}
if (verbose) { } else {
args.push('--verbose'); checkNpmVersion();
} command = 'npm';
args = ['install', '--save', '--save-exact'].concat(dependencies);
}
var child = spawn(command, args, {stdio: 'inherit'}); if (verbose) {
child.on('close', function(code) { args.push('--verbose');
callback(code, command, args); }
var child = spawn(command, args, {stdio: 'inherit'});
child.on('close', function(code) {
if (code !== 0) {
reject({
command: command + ' ' + args.join(' ')
});
return;
}
resolve();
});
}); });
} }
...@@ -188,11 +208,38 @@ function run(root, appName, version, verbose, originalDirectory, template) { ...@@ -188,11 +208,38 @@ function run(root, appName, version, verbose, originalDirectory, template) {
', and ' + chalk.cyan(packageName) + '...' ', and ' + chalk.cyan(packageName) + '...'
); );
console.log(); console.log();
install(allDependencies, verbose, function(code, command, args) { var useYarn = shouldUseYarn();
if (code !== 0) { checkIfOnline(useYarn)
.then(function(isOnline) {
return install(useYarn, allDependencies, verbose, isOnline);
})
.then(function() {
checkNodeVersion(packageName);
// Since react-scripts has been installed with --save
// we need to move it into devDependencies and rewrite package.json
// also ensure react dependencies have caret version range
fixDependencies(packageName);
var scriptsPath = path.resolve(
process.cwd(),
'node_modules',
packageName,
'scripts',
'init.js'
);
var init = require(scriptsPath);
init(root, appName, verbose, originalDirectory, template);
})
.catch(function(reason) {
console.log(); console.log();
console.error('Aborting installation.', chalk.cyan(command + ' ' + args.join(' ')), 'has failed.'); console.log('Aborting installation.');
if (reason.command) {
console.log(' ' + chalk.cyan(reason.command), 'has failed.')
}
console.log();
// On 'exit' we will delete these files from target directory. // On 'exit' we will delete these files from target directory.
var knownGeneratedFiles = [ var knownGeneratedFiles = [
'package.json', 'npm-debug.log', 'yarn-error.log', 'yarn-debug.log', 'node_modules' '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) { ...@@ -217,25 +264,7 @@ function run(root, appName, version, verbose, originalDirectory, template) {
} }
console.log('Done.'); console.log('Done.');
process.exit(1); process.exit(1);
} });
checkNodeVersion(packageName);
// Since react-scripts has been installed with --save
// we need to move it into devDependencies and rewrite package.json
// also ensure react dependencies have caret version range
fixDependencies(packageName);
var scriptsPath = path.resolve(
process.cwd(),
'node_modules',
packageName,
'scripts',
'init.js'
);
var init = require(scriptsPath);
init(root, appName, verbose, originalDirectory, template);
});
} }
function getInstallPackage(version) { function getInstallPackage(version) {
...@@ -407,3 +436,17 @@ function isSafeToCreateProjectIn(root) { ...@@ -407,3 +436,17 @@ function isSafeToCreateProjectIn(root) {
return validFiles.indexOf(file) >= 0; return validFiles.indexOf(file) >= 0;
}); });
} }
function checkIfOnline(useYarn) {
if (!useYarn) {
// Don't ping the Yarn registry.
// We'll just assume the best case.
return Promise.resolve(true);
}
return new Promise(function(resolve) {
dns.resolve('registry.yarnpkg.com', function(err) {
resolve(err === null);
});
});
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment