Skip to content

Commit

Permalink
[CLI] Make react-native init check your Node version
Browse files Browse the repository at this point in the history
We get a bunch of bugs because people are running old versions of Node that don't support modern JS. We have "engines" entries in the package.json files to catch this earlier but printing an explicit error message will also make this clear.

Test Plan: Changed react-native's package.json to require Node >= 5 and got an error message when running the CLI with Node 4.
  • Loading branch information
ide committed Oct 21, 2015
1 parent 5227b10 commit 64c8093
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
28 changes: 28 additions & 0 deletions react-native-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ var fs = require('fs');
var path = require('path');
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var chalk = require('chalk');
var prompt = require('prompt');
var semver = require('semver');

var CLI_MODULE_PATH = function() {
return path.resolve(
Expand All @@ -50,6 +52,15 @@ var CLI_MODULE_PATH = function() {
);
};

var REACT_NATIVE_PACKAGE_JSON_PATH = function() {
return path.resolve(
process.cwd(),
'node_modules',
'react-native',
'package.json'
);
};

checkForVersionArgument();

var cli;
Expand Down Expand Up @@ -185,6 +196,8 @@ function run(root, projectName) {
process.exit(1);
}

checkNodeVersion();

var cli = require(CLI_MODULE_PATH());
cli.init(root, projectName);
});
Expand All @@ -203,6 +216,21 @@ function runVerbose(root, projectName) {
});
}

function checkNodeVersion() {
var packageJson = require(REACT_NATIVE_PACKAGE_JSON_PATH());
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 React Native requires %s. ' +
'Please use a supported version of Node.'
),
process.version,
packageJson.engines.node);
}
}

function checkForVersionArgument() {
if (process.argv.indexOf('-v') >= 0 || process.argv.indexOf('--version') >= 0) {
var pjson = require('./package.json');
Expand Down
4 changes: 3 additions & 1 deletion react-native-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"react-native": "index.js"
},
"dependencies": {
"prompt": "^0.2.14"
"chalk": "^1.1.1",
"prompt": "^0.2.14",
"semver": "^5.0.3"
}
}

0 comments on commit 64c8093

Please sign in to comment.