Skip to content

Commit 7f9fb29

Browse files
EnoahNetzachgaearon
authored andcommitted
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
1 parent 9d42ffa commit 7f9fb29

File tree

4 files changed

+48
-29
lines changed

4 files changed

+48
-29
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
language: node_js
33
node_js:
4+
- 0.10
45
- 4
56
- 6
67
cache:

packages/create-react-app/index.js

+31-22
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,47 @@
3838

3939
'use strict';
4040

41+
var chalk = require('chalk');
42+
43+
var currentNodeVersion = process.versions.node
44+
if (currentNodeVersion.split('.')[0] < 4) {
45+
console.error(chalk.red('You are currently running Node v' + currentNodeVersion +
46+
' but create-react-app requires >=4. Please use a supported version of Node.\n'));
47+
process.exit(1);
48+
}
49+
4150
var fs = require('fs');
4251
var path = require('path');
4352
var execSync = require('child_process').execSync;
4453
var spawn = require('cross-spawn');
45-
var chalk = require('chalk');
4654
var semver = require('semver');
47-
var argv = require('minimist')(process.argv.slice(2));
4855
var pathExists = require('path-exists');
4956

50-
/**
51-
* Arguments:
52-
* --version - to print current version
53-
* --verbose - to print logs while init
54-
* --scripts-version <alternative package>
55-
* Example of valid values:
56-
* - a specific npm version: "0.22.0-rc1"
57-
* - a .tgz archive from any npm repo: "https://registry.npmjs.org/react-scripts/-/react-scripts-0.20.0.tgz"
58-
* - a package prepared with `tasks/clean_pack.sh`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz"
59-
*/
60-
var commands = argv._;
61-
if (commands.length === 0) {
62-
if (argv.version) {
63-
console.log('create-react-app version: ' + require('./package.json').version);
64-
process.exit();
65-
}
66-
console.error(
67-
'Usage: create-react-app <project-directory> [--verbose]'
68-
);
57+
var projectName;
58+
59+
var program = require('commander')
60+
.version(require('./package.json').version)
61+
.arguments('<name>')
62+
.action(function (name) {
63+
projectName = name;
64+
})
65+
.option('-v, --verbose', 'print logs while init')
66+
.option('-s, --scripts-version <alternative package>', 'select a react script variant')
67+
.on('--help', function () {
68+
console.log('Example of valid script version values:')
69+
console.log(' - a specific npm version: "0.22.0-rc1"')
70+
console.log(' - a .tgz archive from any npm repo: "https://registry.npmjs.org/react-scripts/-/react-scripts-0.20.0.tgz"')
71+
console.log(' - a package prepared with `tasks/clean_pack.sh`: "/Users/home/vjeux/create-react-app/react-scripts-0.22.0.tgz"')
72+
})
73+
.parse(process.argv)
74+
75+
if (typeof projectName === 'undefined') {
76+
console.error('Error: no name given!');
77+
console.log('Usage: ' + program.name() + ' ' + program.usage());
6978
process.exit(1);
7079
}
7180

72-
createApp(commands[0], argv.verbose, argv['scripts-version']);
81+
createApp(projectName, program.verbose, program.scriptsVersion);
7382

7483
function createApp(name, verbose, version) {
7584
var root = path.resolve(name);

packages/create-react-app/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
},
2222
"dependencies": {
2323
"chalk": "^1.1.1",
24+
"commander": "^2.9.0",
2425
"cross-spawn": "^4.0.0",
25-
"minimist": "^1.2.0",
2626
"path-exists": "^2.1.0",
2727
"semver": "^5.0.3"
2828
}

tasks/e2e.sh

+15-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
# Start in tasks/ even if run from root directory
1515
cd "$(dirname "$0")"
1616

17+
# CLI and app temporary locations
18+
# http://unix.stackexchange.com/a/84980
19+
temp_cli_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_cli_path'`
20+
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`
21+
1722
function cleanup {
1823
echo 'Cleaning up.'
1924
cd $root_path
@@ -53,15 +58,23 @@ set -x
5358
cd ..
5459
root_path=$PWD
5560

61+
npm install
62+
63+
# If the node version is < 4, the script should just give an error.
64+
if [ `node --version | sed -e 's/^v//' -e 's/\..\+//g'` -lt 4 ]
65+
then
66+
cd $temp_app_path
67+
err_output=`node "$root_path"/packages/create-react-app/index.js test-node-version 2>&1 > /dev/null || echo ''`
68+
[[ $err_output =~ You\ are\ currently\ running\ Node\ v.+\ but\ create-react-app\ requires\ \>=4\. ]] && exit 0 || exit 1
69+
fi
70+
5671
if [ "$USE_YARN" = "yes" ]
5772
then
5873
# Install Yarn so that the test can use it to install packages.
5974
npm install -g yarn@0.17.10 # TODO: remove version when https://github.com/yarnpkg/yarn/issues/2142 is fixed.
6075
yarn cache clean
6176
fi
6277

63-
npm install
64-
6578
# Lint own code
6679
./node_modules/.bin/eslint --ignore-path .gitignore ./
6780

@@ -117,13 +130,10 @@ mv package.json.orig package.json
117130
# ******************************************************************************
118131

119132
# Install the CLI in a temporary location
120-
# http://unix.stackexchange.com/a/84980
121-
temp_cli_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_cli_path'`
122133
cd $temp_cli_path
123134
npm install $cli_path
124135

125136
# Install the app in a temporary location
126-
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`
127137
cd $temp_app_path
128138
create_react_app --scripts-version=$scripts_path test-app
129139

@@ -185,7 +195,6 @@ npm test -- --watch=no
185195
# Test the server
186196
npm start -- --smoke-test
187197

188-
189198
# ******************************************************************************
190199
# Test --scripts-version with a version number
191200
# ******************************************************************************

0 commit comments

Comments
 (0)