Skip to content

Commit

Permalink
Merge pull request facebook#3580 from exponentjs/cli-version-check
Browse files Browse the repository at this point in the history
[CLI] Make `react-native init` check your Node version
  • Loading branch information
Felix Oghină committed Oct 23, 2015
2 parents 7e53ee1 + 64c8093 commit 864fdd1
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 @@ -13,6 +13,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 864fdd1

Please sign in to comment.