From ca106043fc655a1c51332aedf9b001a512269550 Mon Sep 17 00:00:00 2001 From: Sean Ballew Date: Mon, 4 Dec 2017 11:12:53 -0800 Subject: [PATCH] Fix local-cli's installedGlobally check to work on Windows platforms Summary: The react-native local-cli does a check to see if it is being run from a global install or not. If running from a global install, an error is printed and the CLI exits. This check for a global install does not work on Windows. The check of `process.argv` does not contain the expected `node_modules/.bin/react-native`. It instead contains a direct path to the `wrong-react-native.js` file, as determined by the `node_modules/.bin/react-native.cmd` entry point. This update will, on Windows platforms, do a global check by instead looking for the existence of a package.json above the node_modules. If not found, we assume a global install and print the error. In a react-native project, I originally tried running the local react-native cli: ``` > yarn react-native --version yarn run v1.3.2 $ E:\myproject\node_modules\.bin\react-native --version Looks like you installed react-native globally, maybe you meant react-native-cli? To fix the issue, run: npm uninstall -g react-native npm install -g react-native-cli error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. ``` I replaced the `wrong-react-native.js` with the modified version and reran the command: ``` > yarn react-native --version yarn run v1.3.2 $ E:\myproject\node_modules\.bin\react-native --version Scanning folders for symlinks in E:\myproject\node_modules (93ms) 0.50.3 Done in 1.86s. ``` [CLI] [BUGFIX] [local-cli/wrong-react-native.js] - Updated local-cli on Windows to check for the absence of a package.json file to determine if being run from a global installation or not. Closes https://github.com/facebook/react-native/pull/17036 Differential Revision: D6471925 Pulled By: TheSavior fbshipit-source-id: cc5560d1c102d05f378e5ae537f13d31b5343045 --- local-cli/wrong-react-native.js | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/local-cli/wrong-react-native.js b/local-cli/wrong-react-native.js index 7489aa3a6..2555fa045 100755 --- a/local-cli/wrong-react-native.js +++ b/local-cli/wrong-react-native.js @@ -9,8 +9,20 @@ * of patent rights can be found in the PATENTS file in the same directory. */ -var script = process.argv[1]; -var installedGlobally = script.indexOf('node_modules/.bin/react-native') === -1; +const isWindows = process.platform === 'win32'; + +var installedGlobally; +if (isWindows) { + const fs = require('fs'); + const path = require('path'); + // On Windows, assume we are installed globally if we can't find a package.json above node_modules. + installedGlobally = !(fs.existsSync(path.join(__dirname, '../../../package.json'))); +} else { + // On non-windows, assume we are installed globally if we are called from outside of the node_mobules/.bin/react-native executable. + var script = process.argv[1]; + installedGlobally = script.indexOf('node_modules/.bin/react-native') === -1; +} + if (installedGlobally) { const chalk = require('chalk');