Commit 7f9fb294 authored by Fabrizio Castellarin's avatar Fabrizio Castellarin Committed by Dan Abramov
Browse files

Use "commander" for cli argv handling (#1195)

* Use "commander" for  cli argv handling

* Handle different scripts version forms and exits without a name given

* Revert comment about min supported node version

* Check sooner for the minimal node version

* Add travis test for node <4

* Parse stderr in node versions <4
parent 9d42ffab
Showing with 48 additions and 29 deletions
+48 -29
--- ---
language: node_js language: node_js
node_js: node_js:
- 0.10
- 4 - 4
- 6 - 6
cache: cache:
......
...@@ -38,38 +38,47 @@ ...@@ -38,38 +38,47 @@
'use strict'; 'use strict';
var chalk = require('chalk');
var currentNodeVersion = process.versions.node
if (currentNodeVersion.split('.')[0] < 4) {
console.error(chalk.red('You are currently running Node v' + currentNodeVersion +
' but create-react-app requires >=4. Please use a supported version of Node.\n'));
process.exit(1);
}
var fs = require('fs'); var fs = require('fs');
var path = require('path'); 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 chalk = require('chalk');
var semver = require('semver'); var semver = require('semver');
var argv = require('minimist')(process.argv.slice(2));
var pathExists = require('path-exists'); var pathExists = require('path-exists');
/** var projectName;
* Arguments:
* --version - to print current version var program = require('commander')
* --verbose - to print logs while init .version(require('./package.json').version)
* --scripts-version <alternative package> .arguments('<name>')
* Example of valid values: .action(function (name) {
* - a specific npm version: "0.22.0-rc1" projectName = name;
* - a .tgz archive from any npm repo: "https://registry.npmjs.org/react-scripts/-/react-scripts-0.20.0.tgz" })
* - a package prepared with `tasks/clean_pack.sh`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz" .option('-v, --verbose', 'print logs while init')
*/ .option('-s, --scripts-version <alternative package>', 'select a react script variant')
var commands = argv._; .on('--help', function () {
if (commands.length === 0) { console.log('Example of valid script version values:')
if (argv.version) { console.log(' - a specific npm version: "0.22.0-rc1"')
console.log('create-react-app version: ' + require('./package.json').version); console.log(' - a .tgz archive from any npm repo: "https://registry.npmjs.org/react-scripts/-/react-scripts-0.20.0.tgz"')
process.exit(); console.log(' - a package prepared with `tasks/clean_pack.sh`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz"')
} })
console.error( .parse(process.argv)
'Usage: create-react-app <project-directory> [--verbose]'
); if (typeof projectName === 'undefined') {
console.error('Error: no name given!');
console.log('Usage: ' + program.name() + ' ' + program.usage());
process.exit(1); process.exit(1);
} }
createApp(commands[0], argv.verbose, argv['scripts-version']); createApp(projectName, program.verbose, program.scriptsVersion);
function createApp(name, verbose, version) { function createApp(name, verbose, version) {
var root = path.resolve(name); var root = path.resolve(name);
......
...@@ -21,8 +21,8 @@ ...@@ -21,8 +21,8 @@
}, },
"dependencies": { "dependencies": {
"chalk": "^1.1.1", "chalk": "^1.1.1",
"commander": "^2.9.0",
"cross-spawn": "^4.0.0", "cross-spawn": "^4.0.0",
"minimist": "^1.2.0",
"path-exists": "^2.1.0", "path-exists": "^2.1.0",
"semver": "^5.0.3" "semver": "^5.0.3"
} }
......
...@@ -14,6 +14,11 @@ ...@@ -14,6 +14,11 @@
# Start in tasks/ even if run from root directory # Start in tasks/ even if run from root directory
cd "$(dirname "$0")" cd "$(dirname "$0")"
# CLI and app temporary locations
# http://unix.stackexchange.com/a/84980
temp_cli_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_cli_path'`
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`
function cleanup { function cleanup {
echo 'Cleaning up.' echo 'Cleaning up.'
cd $root_path cd $root_path
...@@ -53,6 +58,16 @@ set -x ...@@ -53,6 +58,16 @@ set -x
cd .. cd ..
root_path=$PWD root_path=$PWD
npm install
# If the node version is < 4, the script should just give an error.
if [ `node --version | sed -e 's/^v//' -e 's/\..\+//g'` -lt 4 ]
then
cd $temp_app_path
err_output=`node "$root_path"/packages/create-react-app/index.js test-node-version 2>&1 > /dev/null || echo ''`
[[ $err_output =~ You\ are\ currently\ running\ Node\ v.+\ but\ create-react-app\ requires\ \>=4\. ]] && exit 0 || exit 1
fi
if [ "$USE_YARN" = "yes" ] if [ "$USE_YARN" = "yes" ]
then then
# Install Yarn so that the test can use it to install packages. # Install Yarn so that the test can use it to install packages.
...@@ -60,8 +75,6 @@ then ...@@ -60,8 +75,6 @@ then
yarn cache clean yarn cache clean
fi fi
npm install
# Lint own code # Lint own code
./node_modules/.bin/eslint --ignore-path .gitignore ./ ./node_modules/.bin/eslint --ignore-path .gitignore ./
...@@ -117,13 +130,10 @@ mv package.json.orig package.json ...@@ -117,13 +130,10 @@ mv package.json.orig package.json
# ****************************************************************************** # ******************************************************************************
# Install the CLI in a temporary location # Install the CLI in a temporary location
# http://unix.stackexchange.com/a/84980
temp_cli_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_cli_path'`
cd $temp_cli_path cd $temp_cli_path
npm install $cli_path npm install $cli_path
# Install the app in a temporary location # Install the app in a temporary location
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`
cd $temp_app_path cd $temp_app_path
create_react_app --scripts-version=$scripts_path test-app create_react_app --scripts-version=$scripts_path test-app
...@@ -185,7 +195,6 @@ npm test -- --watch=no ...@@ -185,7 +195,6 @@ npm test -- --watch=no
# Test the server # Test the server
npm start -- --smoke-test npm start -- --smoke-test
# ****************************************************************************** # ******************************************************************************
# Test --scripts-version with a version number # Test --scripts-version with a version number
# ****************************************************************************** # ******************************************************************************
......
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