-
Dan Abramov authored955ea330
#!/usr/bin/env node
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
// create-react-app is installed globally on people's computers. This means
// that it is extremely difficult to have them upgrade the version and
// because there's only one global version installed, it is very prone to
// breaking changes.
//
// The only job of create-react-app is to init the repository and then
// forward all the commands to the local version of create-react-app.
//
// If you need to add a new command, please add it to the scripts/ folder.
//
// The only reason to modify this file is to add more warnings and
// troubleshooting information for the `create-react-app` command.
//
// Do not make breaking changes! We absolutely don't want to have to
// tell people to update their global version of create-react-app.
//
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// /!\ DO NOT MODIFY THIS FILE /!\
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
'use strict';
var fs = require('fs');
var path = require('path');
var spawn = require('cross-spawn');
var chalk = require('chalk');
var semver = require('semver');
var argv = require('minimist')(process.argv.slice(2));
/**
* Arguments:
* --version - to print current version
* --verbose - to print logs while init
* --scripts-version <alternative package>
* Example of valid values:
* - a specific npm version: "0.22.0-rc1"
* - a .tgz archive from any npm repo: "https://registry.npmjs.org/react-scripts/-/react-scripts-0.20.0.tgz"
* - a package prepared with `npm pack`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz"
*/
var commands = argv._;
if (commands.length === 0) {
console.error(
'Usage: create-react-app <project-directory> [--verbose]'
);
process.exit(1);
}
if (argv.version) {
console.log('create-react-app version: ' + require('./package.json').version);
process.exit();
}
createApp(commands[0], argv.verbose, argv['scripts-version']);
function createApp(name, verbose, version) {
7172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
if (fs.existsSync(name)) {
console.log('The directory `' + name + '` already exists. Aborting.');
process.exit(1);
}
var root = path.resolve(name);
var appName = path.basename(root);
console.log(
'Creating a new React app in ' + root + '.'
);
console.log();
fs.mkdirSync(root);
var packageJson = {
name: appName,
version: '0.0.1',
private: true,
};
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
process.chdir(root);
console.log('Installing packages. This might take a couple minutes.');
console.log('Installing react-scripts from npm...');
run(root, appName, version, verbose);
}
function run(root, appName, version, verbose) {
var args = [
'install',
verbose && '--verbose',
'--save-dev',
'--save-exact',
getInstallPackage(version),
].filter(function(e) { return e; });
var proc = spawn('npm', args, {stdio: 'inherit'});
proc.on('close', function (code) {
if (code !== 0) {
console.error('`npm ' + args.join(' ') + '` failed');
return;
}
var scriptsPath = path.resolve(
process.cwd(),
'node_modules',
'react-scripts',
'scripts',
'init.js'
);
var init = require(scriptsPath);
init(root, appName, verbose);
});
}
function getInstallPackage(version) {
var packageToInstall = 'react-scripts';
var validSemver = semver.valid(version);
if (validSemver) {
packageToInstall += '@' + validSemver;
} else if (version) {
// for tar.gz or alternative paths
packageToInstall = version;
}
return packageToInstall;
}
function checkNodeVersion() {
var packageJsonPath = path.resolve(
141142143144145146147148149150151152153154155156157158159160161162
process.cwd(),
'node_modules',
'react-scripts',
'package.json'
);
var packageJson = require(packageJsonPath);
if (!packageJson.engines || !packageJson.engines.node) {
return;
}
if (!semver.satisfies(process.version, packageJson.engines.node)) {
console.error(
chalk.red(
'You are currently running Node %s but create-react-app requires %s.' +
' Please use a supported version of Node.\n'
),
process.version,
packageJson.engines.node
);
}
}