react-native-cli
Advanced tools
Comparing version 1.3.0 to 2.0.0
116
index.js
#!/usr/bin/env node | ||
'use strict'; | ||
/** | ||
@@ -38,2 +36,4 @@ * Copyright (c) 2015-present, Facebook, Inc. | ||
'use strict'; | ||
var fs = require('fs'); | ||
@@ -43,3 +43,2 @@ var path = require('path'); | ||
var execSync = require('child_process').execSync; | ||
var spawn = require('child_process').spawn; | ||
var chalk = require('chalk'); | ||
@@ -60,4 +59,6 @@ var prompt = require('prompt'); | ||
*/ | ||
var argv = require('minimist')(process.argv.slice(2)); | ||
var options = require('minimist')(process.argv.slice(2)); | ||
checkForVersionArgument(options); | ||
var CLI_MODULE_PATH = function() { | ||
@@ -108,4 +109,2 @@ return path.resolve( | ||
checkForVersionArgument(); | ||
var cli; | ||
@@ -117,8 +116,7 @@ var cliPath = CLI_MODULE_PATH(); | ||
// minimist api | ||
var commands = argv._; | ||
var commands = options._; | ||
if (cli) { | ||
cli.run(); | ||
} else { | ||
if (argv._.length === 0 && (argv.h || argv.help)) { | ||
if (options._.length === 0 && (options.h || options.help)) { | ||
console.log([ | ||
@@ -157,4 +155,3 @@ '', | ||
} else { | ||
const rnPackage = argv.version; | ||
init(commands[1], argv.verbose, rnPackage, argv.npm); | ||
init(commands[1], options); | ||
} | ||
@@ -195,18 +192,18 @@ break; | ||
* @param name Project name, e.g. 'AwesomeApp'. | ||
* @param verbose If true, will run 'npm install' in verbose mode (for debugging). | ||
* @param rnPackage Version of React Native to install, e.g. '0.38.0'. | ||
* @param forceNpmClient If true, always use the npm command line client, | ||
* @param options.verbose If true, will run 'npm install' in verbose mode (for debugging). | ||
* @param options.version Version of React Native to install, e.g. '0.38.0'. | ||
* @param options.npm If true, always use the npm command line client, | ||
* don't use yarn even if available. | ||
*/ | ||
function init(name, verbose, rnPackage, forceNpmClient) { | ||
function init(name, options) { | ||
validateProjectName(name); | ||
if (fs.existsSync(name)) { | ||
createAfterConfirmation(name, verbose, rnPackage, forceNpmClient); | ||
createAfterConfirmation(name, options); | ||
} else { | ||
createProject(name, verbose, rnPackage, forceNpmClient); | ||
createProject(name, options); | ||
} | ||
} | ||
function createAfterConfirmation(name, verbose, rnPackage, forceNpmClient) { | ||
function createAfterConfirmation(name, options) { | ||
prompt.start(); | ||
@@ -224,3 +221,3 @@ | ||
if (result.yesno[0] === 'y') { | ||
createProject(name, verbose, rnPackage, forceNpmClient); | ||
createProject(name, options); | ||
} else { | ||
@@ -233,3 +230,3 @@ console.log('Project initialization canceled'); | ||
function createProject(name, verbose, rnPackage, forceNpmClient) { | ||
function createProject(name, options) { | ||
var root = path.resolve(name); | ||
@@ -258,7 +255,3 @@ var projectName = path.basename(root); | ||
if (verbose) { | ||
runVerbose(root, projectName, rnPackage, forceNpmClient); | ||
} else { | ||
run(root, projectName, rnPackage, forceNpmClient); | ||
} | ||
run(root, projectName, options); | ||
} | ||
@@ -278,44 +271,43 @@ | ||
function run(root, projectName, rnPackage, forceNpmClient) { | ||
function run(root, projectName, options) { | ||
// E.g. '0.38' or '/path/to/archive.tgz' | ||
const rnPackage = options.version; | ||
const forceNpmClient = options.npm; | ||
const yarnVersion = (!forceNpmClient) && getYarnVersionIfAvailable(); | ||
var installCommand; | ||
if (yarnVersion) { | ||
console.log('Using yarn v' + yarnVersion); | ||
console.log('Installing ' + getInstallPackage(rnPackage) + '...'); | ||
installCommand = 'yarn add ' + getInstallPackage(rnPackage) + ' --exact'; | ||
if (options.installCommand) { | ||
// In CI environments it can be useful to provide a custom command, | ||
// to set up and use an offline mirror for installing dependencies, for example. | ||
installCommand = options.installCommand; | ||
} else { | ||
console.log('Installing ' + getInstallPackage(rnPackage) + '...'); | ||
if (!forceNpmClient) { | ||
console.log('Consider installing yarn to make this faster: https://yarnpkg.com'); | ||
if (yarnVersion) { | ||
console.log('Using yarn v' + yarnVersion); | ||
console.log('Installing ' + getInstallPackage(rnPackage) + '...'); | ||
installCommand = 'yarn add ' + getInstallPackage(rnPackage) + ' --exact'; | ||
if (options.verbose) { | ||
installCommand += ' --verbose'; | ||
} | ||
} else { | ||
console.log('Installing ' + getInstallPackage(rnPackage) + '...'); | ||
if (!forceNpmClient) { | ||
console.log('Consider installing yarn to make this faster: https://yarnpkg.com'); | ||
} | ||
installCommand = 'npm install --save --save-exact ' + getInstallPackage(rnPackage); | ||
if (options.verbose) { | ||
installCommand += ' --verbose'; | ||
} | ||
} | ||
installCommand = 'npm install --save --save-exact ' + getInstallPackage(rnPackage); | ||
} | ||
exec(installCommand, function(err, stdout, stderr) { | ||
if (err) { | ||
console.log(stdout); | ||
console.error(stderr); | ||
console.error('Command `' + installCommand + '` failed.'); | ||
process.exit(1); | ||
} | ||
checkNodeVersion(); | ||
cli = require(CLI_MODULE_PATH()); | ||
cli.init(root, projectName); | ||
}); | ||
try { | ||
execSync(installCommand, {stdio: 'inherit'}); | ||
} catch (err) { | ||
console.error(err); | ||
console.error('Command `' + installCommand + '` failed.'); | ||
process.exit(1); | ||
} | ||
checkNodeVersion(); | ||
cli = require(CLI_MODULE_PATH()); | ||
cli.init(root, projectName); | ||
} | ||
function runVerbose(root, projectName, rnPackage, forceNpmClient) { | ||
// Use npm client, yarn doesn't support --verbose yet | ||
console.log('Installing ' + getInstallPackage(rnPackage) + ' from npm. This might take a while...'); | ||
var proc = spawn(/^win/.test(process.platform) ? 'npm.cmd' : 'npm', ['install', '--verbose', '--save', '--save-exact', getInstallPackage(rnPackage)], {stdio: 'inherit'}); | ||
proc.on('close', function (code) { | ||
if (code !== 0) { | ||
console.error('`npm install --save --save-exact react-native` failed'); | ||
return; | ||
} | ||
cli = require(CLI_MODULE_PATH()); | ||
cli.init(root, projectName); | ||
}); | ||
} | ||
function checkNodeVersion() { | ||
@@ -337,4 +329,4 @@ var packageJson = require(REACT_NATIVE_PACKAGE_JSON_PATH()); | ||
function checkForVersionArgument() { | ||
if (argv._.length === 0 && (argv.v || argv.version)) { | ||
function checkForVersionArgument(options) { | ||
if (options._.length === 0 && (options.v || options.version)) { | ||
console.log('react-native-cli: ' + require('./package.json').version); | ||
@@ -341,0 +333,0 @@ try { |
{ | ||
"name": "react-native-cli", | ||
"version": "1.3.0", | ||
"version": "2.0.0", | ||
"license": "BSD-3-Clause", | ||
@@ -5,0 +5,0 @@ "description": "The React Native CLI tools", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5
2
14767
300