Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion react-native-cli/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var spawn = require('child_process').spawn;
var chalk = require('chalk');
var prompt = require('prompt');
var semver = require('semver');
var os = require('os');

var CLI_MODULE_PATH = function() {
return path.resolve(
Expand Down Expand Up @@ -198,7 +199,14 @@ function run(root, projectName, logLevel) {
if (logLevel === 'debug' || logLevel === 'verbose') {
spawnArgs = {stdio: 'inherit'};
}
var proc = spawn('npm', args, spawnArgs);
var proc;
if (os.platform() === 'win32'){
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we use mostly process.platform throughout the codebase.

args.unshift('npm');
args.unshift('/c');
proc = spawn('cmd', args, spawnArgs);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this cmd /c npm install --save react-native? I've never seen that syntax, can you add some comment explaining what it does?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmd /c is just same as bash -c on *nix
spawn cannot run a .cmd file(like .sh on *nix) on windows, but we can invoke it through cmd.exe

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

npm is actually npm.cmd on windows, not a executable file.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To run things via shell, there is https://nodejs.org/dist/latest-v4.x/docs/api/child_process.html#child_process_child_process_exec_command_options_callback. Wouldn't it be safer to use that instead of hardcoding cmd /c?

} else {
proc = spawn('npm', args, spawnArgs);
}
proc.on('close', function (code) {
if (code !== 0) {
console.error('`npm install --save react-native` failed');
Expand Down