Skip to content

Use offline cached version with yarn when it's possible #1423

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Next Next commit
add --offline flag when we are using yarn and we are offline
  • Loading branch information
voxsim committed Feb 27, 2017
commit efb8eeae77db1aa140fa00a2216f974d603a0e8b
116 changes: 69 additions & 47 deletions packages/create-react-app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ var path = require('path');
var execSync = require('child_process').execSync;
var spawn = require('cross-spawn');
var semver = require('semver');
var dns = require('dns');

var projectName;

Expand Down Expand Up @@ -145,25 +146,37 @@ function shouldUseYarn() {
}
}

function install(dependencies, verbose, callback) {
var command;
var args;
if (shouldUseYarn()) {
command = 'yarnpkg';
args = [ 'add', '--exact'].concat(dependencies);
} else {
checkNpmVersion();
command = 'npm';
args = ['install', '--save', '--save-exact'].concat(dependencies);
}
function install(dependencies, verbose, isOnline) {
return new Promise(function(resolve, reject) {
var command;
var args;
if (shouldUseYarn()) {
command = 'yarnpkg';
args = [
'add',
'--exact',
isOnline === false && '--offline'
].concat(dependencies);
} else {
checkNpmVersion();
command = 'npm';
args = ['install', '--save', '--save-exact'].concat(dependencies);
}

if (verbose) {
args.push('--verbose');
}
if (verbose) {
args.push('--verbose');
}

var child = spawn(command, args, {stdio: 'inherit'});
child.on('close', function(code) {
if (code !== 0) {
console.log();
console.error('Aborting installation.', chalk.cyan(command + ' ' + args.join(' ')), 'has failed.');
reject();
}

var child = spawn(command, args, {stdio: 'inherit'});
child.on('close', function(code) {
callback(code, command, args);
resolve(isOnline);
});
});
}

Expand All @@ -180,35 +193,11 @@ function run(root, appName, version, verbose, originalDirectory, template) {
);
console.log();

install(allDependencies, verbose, function(code, command, args) {
if (code !== 0) {
console.log();
console.error('Aborting installation.', chalk.cyan(command + ' ' + args.join(' ')), 'has failed.');
// On 'exit' we will delete these files from target directory.
var knownGeneratedFiles = [
'package.json', 'npm-debug.log', 'yarn-error.log', 'yarn-debug.log', 'node_modules'
];
var currentFiles = fs.readdirSync(path.join(root));
currentFiles.forEach(function (file) {
knownGeneratedFiles.forEach(function (fileToMatch) {
// This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
// and the rest of knownGeneratedFiles.
if ((fileToMatch.match(/.log/g) && file.indexOf(fileToMatch) === 0) || file === fileToMatch) {
console.log('Deleting generated file...', chalk.cyan(file));
fs.removeSync(path.join(root, file));
}
});
});
var remainingFiles = fs.readdirSync(path.join(root));
if (!remainingFiles.length) {
// Delete target folder if empty
console.log('Deleting', chalk.cyan(appName + '/'), 'from', chalk.cyan(path.resolve(root, '..')));
fs.removeSync(path.join(root));
}
console.log('Done.');
process.exit(1);
}

checkIfOnline()
.then(function(isOnline) {
return install(allDependencies, verbose, isOnline);
})
.then(function(isOnline) {
checkNodeVersion(packageName);

// Since react-scripts has been installed with --save
Expand All @@ -223,7 +212,32 @@ function run(root, appName, version, verbose, originalDirectory, template) {
'init.js'
);
var init = require(scriptsPath);
init(root, appName, verbose, originalDirectory, template);
init(root, appName, verbose, originalDirectory, template, isOnline);
})
.catch(function(command, args) {
// On 'exit' we will delete these files from target directory.
var knownGeneratedFiles = [
'package.json', 'npm-debug.log', 'yarn-error.log', 'yarn-debug.log', 'node_modules'
];
var currentFiles = fs.readdirSync(path.join(root));
currentFiles.forEach(function (file) {
knownGeneratedFiles.forEach(function (fileToMatch) {
// This will catch `(npm-debug|yarn-error|yarn-debug).log*` files
// and the rest of knownGeneratedFiles.
if ((fileToMatch.match(/.log/g) && file.indexOf(fileToMatch) === 0) || file === fileToMatch) {
console.log('Deleting generated file...', chalk.cyan(file));
fs.removeSync(path.join(root, file));
}
});
});
var remainingFiles = fs.readdirSync(path.join(root));
if (!remainingFiles.length) {
// Delete target folder if empty
console.log('Deleting', chalk.cyan(appName + '/'), 'from', chalk.cyan(path.resolve(root, '..')));
fs.removeSync(path.join(root));
}
console.log('Done.');
process.exit(1);
});
}

Expand Down Expand Up @@ -364,3 +378,11 @@ function isSafeToCreateProjectIn(root) {
return validFiles.indexOf(file) >= 0;
});
}

function checkIfOnline() {
return new Promise(function(resolve) {
dns.resolve('registry.yarnpkg.com', function(err) {
resolve(err === null);
});
});
}
7 changes: 5 additions & 2 deletions packages/react-scripts/scripts/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var path = require('path');
var spawn = require('cross-spawn');
var chalk = require('chalk');

module.exports = function(appPath, appName, verbose, originalDirectory, template) {
module.exports = function(appPath, appName, verbose, originalDirectory, template, isOnline) {
var ownPackageName = require(path.join(__dirname, '..', 'package.json')).name;
var ownPath = path.join(appPath, 'node_modules', ownPackageName);
var appPackage = require(path.join(appPath, 'package.json'));
Expand Down Expand Up @@ -69,7 +69,10 @@ module.exports = function(appPath, appName, verbose, originalDirectory, template

if (useYarn) {
command = 'yarnpkg';
args = ['add'];
args = [
'add',
isOnline === false && '--offline'
];
} else {
command = 'npm';
args = [
Expand Down