Skip to content

Commit 6ddc1f7

Browse files
Fix CLI usage with Node.js 4
When a package has to be used globally, npm automatically creates required executable based on the `bin` key in `package.json`. In our case the executable, that's created, is a shell script (`.cmd` on Windows) that starts a node process and launches `<cli dir>/bin/nativescript.js`. This makes it impossible for us to modify the arguments passed to Node.js. But for Node.js 4.x.x, we want to pass `--harmony` flag. In order to fix this, modify nativescript.js to checks current node version and if we want to pass additional arguments to Node.js process, start a new Node process inside it. In case the version of Node is 6.x.x or later, we'll use the old code, where the operations are in the same process. For easier development, we've created several shell scripts (`.cmd` on Windows) inside CLI's bin directory. Developers of CLI add the bin dir to their `$PATH` variable instead of installing the CLI globally. In order to allow usage of `tns` directly from the terminal for them, we've created shell scripts that start the `bin/nativescript.js` executable. However the `bin/nativescript.js` has shebang header and it can be executed directly. So in order to simplify the bin directory, rename `bin/nativescript.js` to `tns` and remove all other files. This way when CLI developers add bin to their $PATH environment variable, they'll be able to call `tns` directly. Set the executable name in `package.json`, so when the CLI is installed globally, everything will work as before.
1 parent 6b81845 commit 6ddc1f7

File tree

6 files changed

+40
-83
lines changed

6 files changed

+40
-83
lines changed

bin/nativescript

+2-18
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,3 @@
1-
#!/bin/sh
1+
#!/usr/bin/env node
22

3-
AB_DIR="`dirname \"$0\"`"
4-
NODE_VERSION=`node --version`
5-
NODE4_VERSION_PREFIX="v4."
6-
NODE5_VERSION_PREFIX="v5."
7-
8-
# check if Node.js version is 4.x.x or 5.x.x - both of them do not support some of required features
9-
# so we have to pass --harmony flag for them in order to enable spread opearator usage
10-
# Use POSIX substring parameter expansion, so the code will work on all shells.
11-
12-
if [ "${NODE_VERSION#$NODE4_VERSION_PREFIX*}" != "$NODE_VERSION" -o "${NODE_VERSION#$NODE5_VERSION_PREFIX*}" != "$NODE_VERSION" ]
13-
then
14-
# Node is 4.x.x or 5.x.x
15-
node --harmony "$AB_DIR/nativescript.js" "$@"
16-
else
17-
# Node is NOT 4.x.x or 5.x.x
18-
node "$AB_DIR/nativescript.js" "$@"
19-
fi
3+
require("./tns");

bin/nativescript.cmd

-18
This file was deleted.

bin/nativescript.js

-8
This file was deleted.

bin/tns

+36-19
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
1-
#!/bin/sh
2-
3-
AB_DIR="`dirname \"$0\"`"
4-
NODE_VERSION=`node --version`
5-
NODE4_VERSION_PREFIX="v4."
6-
NODE5_VERSION_PREFIX="v5."
7-
8-
# check if Node.js version is 4.x.x or 5.x.x - both of them do not support some of required features
9-
# so we have to pass --harmony flag for them in order to enable spread opearator usage
10-
# Use POSIX substring parameter expansion, so the code will work on all shells.
11-
12-
if [ "${NODE_VERSION#$NODE4_VERSION_PREFIX*}" != "$NODE_VERSION" -o "${NODE_VERSION#$NODE5_VERSION_PREFIX*}" != "$NODE_VERSION" ]
13-
then
14-
# Node is 4.x.x or 5.x.x
15-
node --harmony "$AB_DIR/nativescript.js" "$@"
16-
else
17-
# Node is NOT 4.x.x or 5.x.x
18-
node "$AB_DIR/nativescript.js" "$@"
19-
fi
1+
#!/usr/bin/env node
2+
3+
"use strict";
4+
var path = require("path"),
5+
node = require("../package.json").engines.node,
6+
pathToLib = path.join(__dirname, "..", "lib"),
7+
pathToCommon = path.join(pathToLib, "common");
8+
9+
require(path.join(pathToCommon, "verify-node-version")).verifyNodeVersion(node, "NativeScript");
10+
11+
var pathToCliExecutable = path.join(pathToLib, "nativescript-cli.js");
12+
13+
var nodeArgs = require(path.join(pathToCommon, "scripts", "node-args")).getNodeArgs();
14+
15+
if (nodeArgs.length) {
16+
// We need custom args for Node process, so pass them here.
17+
var childProcess = require("child_process");
18+
var args = process.argv;
19+
20+
// Remove `node` and `nativescript` from the arguments.
21+
args.shift();
22+
args.shift();
23+
24+
args.unshift(pathToCliExecutable);
25+
26+
args = nodeArgs.concat(args);
27+
28+
var nodeProcess = childProcess.spawn(process.execPath, args, { stdio: "inherit" });
29+
30+
nodeProcess.on("close", function(code) {
31+
// We need this handler so if command fails, we'll exit with same exit code as CLI.
32+
process.exit(code);
33+
});
34+
} else {
35+
require(pathToCliExecutable);
36+
}

bin/tns.cmd

-18
This file was deleted.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
"author": "Telerik <[email protected]>",
66
"description": "Command-line interface for building NativeScript projects",
77
"bin": {
8-
"tns": "./bin/nativescript.js",
9-
"nativescript": "./bin/nativescript.js"
8+
"tns": "./bin/tns",
9+
"nativescript": "./bin/tns"
1010
},
1111
"main": "./lib/nativescript-cli.js",
1212
"scripts": {

0 commit comments

Comments
 (0)