From 64c809345fc61f8db5470956133e4f428bb6570f Mon Sep 17 00:00:00 2001 From: James Ide Date: Wed, 21 Oct 2015 12:47:38 -0700 Subject: [PATCH] [CLI] Make `react-native init` check your Node version 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. --- react-native-cli/index.js | 28 ++++++++++++++++++++++++++++ react-native-cli/package.json | 4 +++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/react-native-cli/index.js b/react-native-cli/index.js index 4bfd6e735..288323396 100755 --- a/react-native-cli/index.js +++ b/react-native-cli/index.js @@ -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( @@ -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; @@ -185,6 +196,8 @@ function run(root, projectName) { process.exit(1); } + checkNodeVersion(); + var cli = require(CLI_MODULE_PATH()); cli.init(root, projectName); }); @@ -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'); diff --git a/react-native-cli/package.json b/react-native-cli/package.json index 31fddd66b..78a807012 100644 --- a/react-native-cli/package.json +++ b/react-native-cli/package.json @@ -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" } }