Skip to content

Commit bb9b69f

Browse files
Transpile to ES6
Add support for ES6 transpilation. Change dependency injection in order to match the `class` and `constructor` keywords. Update istanbul to latest version as its previous versions do not support ES6. Remove bluebird.d.ts as it conflicts with Promises from TypeScript's ES6 d.ts. Remove bluebird as dependency - we will use native Promises from now on. Add support for Node.js 4 and 5 - both versions have limited support for ES6 features, so in order to support them we have to: - pass --harmony flag to Node.js - do not use some features like default values of method arguments. - do not use spread operator - it's implementation in Node.js 4 is limited only to arrays. Even this implementation has issues. So use `.apply` instead of using spread operator. Remove all other usages of spread operator (for objects). Add tests scripts for istanbul and mocha, as they both will need the `--harmony` flag as well, so we cannot execute them directly with Node.js 4 and 5. Instead we'll execute a custom script, that will start a new node process. Introduce `test-scripts` directory with JavaScript executables to start the tests. Set them in package.json. They are required as when Node 4 or Node 5 is used, the tests will need `--harmony` flag as well. Exclude `test-scripts` directory from npm package (add it to `.npmignore`) - users do not need these files. Exclude `.vscode/launch.json` from `.gitignore` and add predefined launch configurations. They can be used for debugging CLI process and unit tests.
1 parent c960701 commit bb9b69f

20 files changed

+246
-50
lines changed

.gitignore

+5-2
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ results
2929
scratch/
3030
.idea/
3131
.settings/
32-
.vscode/
32+
.vscode/**
33+
!.vscode/launch.json
3334
test-reports.xml
3435

3536
npm-debug.log
3637
node_modules
37-
docs/html
38+
docs/html
39+
40+
!test-scripts/*.js

.npmignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ lib/**/*.ts
2121
lib/**/*.js.map
2222

2323
test/
24+
test-scripts/
2425
.vscode
2526
lib/common/test/
2627
coverage/
2728
scratch/
2829
*.suo
2930
.travis.yml
3031
docs/html/
31-
dev/
32+
dev/

.vscode/launch.json

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
// Use IntelliSense to learn about possible Node.js debug attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "node",
9+
"request": "launch",
10+
"name": "Launch Program (Node 6+)",
11+
"program": "${workspaceRoot}/lib/nativescript-cli.js",
12+
"cwd": "${workspaceRoot}",
13+
"sourceMaps": true,
14+
// define the arguments that you would like to pass to CLI, for example
15+
// "args": [ "build", "android", "--justlaunch" ]
16+
"args": [
17+
18+
]
19+
},
20+
21+
{
22+
// in case you want to debug a single test, modify it's code to be `it.only(...` instead of `it(...`
23+
"type": "node",
24+
"request": "launch",
25+
"name": "Launch Tests (Node 6+)",
26+
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
27+
"cwd": "${workspaceRoot}",
28+
"sourceMaps": true
29+
},
30+
31+
{
32+
"type": "node",
33+
"runtimeArgs": [
34+
"--harmony"
35+
],
36+
"request": "launch",
37+
"name": "Launch Program (Node 4, Node 5)",
38+
"program": "${workspaceRoot}/lib/nativescript-cli.js",
39+
"cwd": "${workspaceRoot}",
40+
"sourceMaps": true,
41+
// define the arguments that you would like to pass to CLI, for example
42+
// "args": [ "build", "android", "--justlaunch" ]
43+
"args": [
44+
45+
]
46+
},
47+
48+
{
49+
// in case you want to debug a single test, modify it's code to be `it.only(...` instead of `it(...`
50+
"type": "node",
51+
"runtimeArgs": [
52+
"--harmony"
53+
],
54+
"request": "launch",
55+
"name": "Launch Tests (Node 4, Node 5)",
56+
"program": "${workspaceRoot}/node_modules/mocha/bin/_mocha",
57+
"cwd": "${workspaceRoot}",
58+
"sourceMaps": true
59+
},
60+
61+
{
62+
"type": "node",
63+
"request": "attach",
64+
"name": "Attach to Process",
65+
"port": 5858,
66+
"sourceMaps": true
67+
}
68+
69+
]
70+
}

Gruntfile.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,7 @@ module.exports = function(grunt) {
2828

2929
pkg: grunt.file.readJSON("package.json"),
3030
ts: {
31-
options: {
32-
target: 'es5',
33-
module: 'commonjs',
34-
sourceMap: true,
35-
declaration: false,
36-
removeComments: false,
37-
noImplicitAny: true,
38-
experimentalDecorators: true
39-
},
31+
options: grunt.file.readJSON("tsconfig.json").compilerOptions,
4032

4133
devlib: {
4234
src: ["lib/**/*.ts", "!lib/common/node_modules/**/*.ts"],
@@ -131,7 +123,18 @@ module.exports = function(grunt) {
131123
},
132124

133125
clean: {
134-
src: ["test/**/*.js*", "lib/**/*.js*", "!lib/common/vendor/*.js", "!lib/common/**/*.json", "!lib/common/Gruntfile.js", "!lib/common/node_modules/**/*", "!lib/common/hooks/**/*.js", "!lib/common/bin/*.js", "*.tgz"]
126+
src: ["test/**/*.js*",
127+
"lib/**/*.js*",
128+
"!test-scripts/**/*",
129+
"!lib/common/vendor/*.js",
130+
"!lib/common/**/*.json",
131+
"!lib/common/Gruntfile.js",
132+
"!lib/common/node_modules/**/*",
133+
"!lib/common/hooks/**/*.js",
134+
"!lib/common/bin/*.js",
135+
"!lib/common/test-scripts/**/*",
136+
"!lib/common/scripts/**/*",
137+
"*.tgz"]
135138
}
136139
});
137140

bin/nativescript

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
#!/bin/sh
22

33
AB_DIR="`dirname \"$0\"`"
4-
node "$AB_DIR/nativescript.js" "$@"
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

bin/nativescript.cmd

+18-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
@node %~dp0\nativescript.js %*
1+
@for /F "delims=" %%i IN ('@node --version') DO @set node_ver=%%i
2+
3+
@echo %node_ver% | @findstr /b /c:"v4."
4+
@set is_node_4=%errorlevel%
5+
6+
@echo %node_ver% | @findstr /b /c:"v5."
7+
@set is_node_5=%errorlevel%
8+
9+
@set use_harmony_flag=0
10+
11+
@if %is_node_4% == 0 @set use_harmony_flag=1
12+
@if %is_node_5% == 0 @set use_harmony_flag=1
13+
14+
@if %use_harmony_flag% == 1 (
15+
return @node --harmony %~dp0\nativescript.js %*
16+
) else (
17+
@node %~dp0\nativescript.js %*
18+
)

bin/tns

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
11
#!/bin/sh
22

33
AB_DIR="`dirname \"$0\"`"
4-
node "$AB_DIR/nativescript.js" "$@"
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

bin/tns.cmd

+18-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,18 @@
1-
@node %~dp0\nativescript.js %*
1+
@for /F "delims=" %%i IN ('@node --version') DO @set node_ver=%%i
2+
3+
@echo %node_ver% | @findstr /b /c:"v4."
4+
@set is_node_4=%errorlevel%
5+
6+
@echo %node_ver% | @findstr /b /c:"v5."
7+
@set is_node_5=%errorlevel%
8+
9+
@set use_harmony_flag=0
10+
11+
@if %is_node_4% == 0 @set use_harmony_flag=1
12+
@if %is_node_5% == 0 @set use_harmony_flag=1
13+
14+
@if %use_harmony_flag% == 1 (
15+
return @node --harmony %~dp0\nativescript.js %*
16+
) else (
17+
@node %~dp0\nativescript.js %*
18+
)

lib/device-sockets/ios/socket-request-executor.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ export class IOSSocketRequestExecutor implements IiOSSocketRequestExecutor {
1313
return (() => {
1414
let npc = new iOSProxyServices.NotificationProxyClient(device, this.$injector);
1515

16-
let [alreadyConnected, readyForAttach, attachAvailable] = [this.$iOSNotification.alreadyConnected, this.$iOSNotification.readyForAttach, this.$iOSNotification.attachAvailable]
17-
.map((notification) => this.$iOSNotificationService.awaitNotification(npc, notification, timeout));
16+
let data = [this.$iOSNotification.alreadyConnected, this.$iOSNotification.readyForAttach, this.$iOSNotification.attachAvailable]
17+
.map((notification) => this.$iOSNotificationService.awaitNotification(npc, notification, timeout)),
18+
alreadyConnected = data[0],
19+
readyForAttach = data[1],
20+
attachAvailable = data[2];
1821

1922
npc.postNotificationAndAttachForData(this.$iOSNotification.attachAvailabilityQuery);
2023

lib/services/platform-service.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export class PlatformService implements IPlatformService {
5353

5454
private addPlatform(platformParam: string): IFuture<void> {
5555
return (() => {
56-
let [platform, version] = platformParam.split("@");
56+
let data = platformParam.split("@"),
57+
platform = data[0],
58+
version = data[1];
5759

5860
this.validatePlatform(platform);
5961

@@ -427,7 +429,10 @@ export class PlatformService implements IPlatformService {
427429
public updatePlatforms(platforms: string[]): IFuture<void> {
428430
return (() => {
429431
_.each(platforms, platformParam => {
430-
let [platform, version] = platformParam.split("@");
432+
let data = platformParam.split("@"),
433+
platform = data[0],
434+
version = data[1];
435+
431436
if (this.isPlatformInstalled(platform).wait()) {
432437
this.updatePlatform(platform, version).wait();
433438
} else {

lib/services/plugins-service.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ export class PluginsService implements IPluginsService {
163163
.filter(dependencyName => _.startsWith(dependencyName, "@"))
164164
.each(scopedDependencyDir => {
165165
let contents = this.$fs.readDirectory(path.join(this.nodeModulesPath, scopedDependencyDir)).wait();
166-
installedDependencies = installedDependencies.concat(...contents.map(dependencyName => `${scopedDependencyDir}/${dependencyName}`));
166+
installedDependencies = installedDependencies.concat(contents.map(dependencyName => `${scopedDependencyDir}/${dependencyName}`));
167167
});
168168

169169
let packageJsonContent = this.$fs.readJson(this.getPackageJsonFilePath()).wait();

lib/services/project-templates-service.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export class ProjectTemplatesService implements IProjectTemplatesService {
1818
let templateName = originalTemplateName.toLowerCase();
1919

2020
// support <reserved_name>@<version> syntax
21-
let [name, version] = templateName.split("@");
21+
let data = templateName.split("@"),
22+
name = data[0],
23+
version = data[1];
24+
2225
if(constants.RESERVED_TEMPLATE_NAMES[name]) {
2326
realTemplatePath = this.prepareNativeScriptTemplate(constants.RESERVED_TEMPLATE_NAMES[name], version, projectDir).wait();
2427
} else {

package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
},
1111
"main": "./lib/nativescript-cli.js",
1212
"scripts": {
13-
"test": "node_modules/.bin/istanbul cover node_modules/mocha/bin/_mocha",
13+
"test": "node test-scripts/istanbul.js",
1414
"postinstall": "node postinstall.js",
1515
"preuninstall": "node preuninstall.js",
16-
"mocha": "mocha",
16+
"mocha": "node test-scripts/mocha.js",
1717
"tsc": "tsc",
1818
"test-watch": "node ./dev/tsc-to-mocha-watch.js"
1919
},
@@ -27,7 +27,6 @@
2727
"mobile"
2828
],
2929
"dependencies": {
30-
"bluebird": "2.9.34",
3130
"bplist-parser": "0.1.0",
3231
"bufferpack": "0.0.6",
3332
"bufferutil": "https://github.com/telerik/bufferutil/tarball/v1.0.1.4",
@@ -90,7 +89,7 @@
9089
"grunt-ts": "6.0.0-beta.3",
9190
"grunt-tslint": "3.3.0",
9291
"istanbul": "0.4.5",
93-
"mocha": "2.5.3",
92+
"mocha": "3.1.2",
9493
"mocha-fibers": "https://github.com/NativeScript/mocha-fibers.git",
9594
"mocha-typescript": "^1.0.4",
9695
"should": "7.0.2",

postinstall.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
"use strict";
2+
23
var child_process = require("child_process");
3-
child_process.spawn(process.argv[0], ["bin/nativescript.js", "post-install-cli"], {stdio: "inherit"});
4+
var commandArgs = ["bin/nativescript.js", "post-install-cli"];
5+
var path = require("path");
6+
var nodeArgs = require(path.join(__dirname, "lib", "common", "scripts", "node-args")).getNodeArgs();
7+
8+
child_process.spawn(process.argv[0], nodeArgs.concat(commandArgs), {stdio: "inherit"});

preuninstall.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
"use strict";
2+
13
var child_process = require("child_process");
4+
var commandArgs = ["bin/nativescript.js", "dev-preuninstall"];
5+
var path = require("path");
6+
var nodeArgs = require(path.join(__dirname, "lib", "common", "scripts", "node-args")).getNodeArgs();
27

3-
var child = child_process.exec("node bin/nativescript.js dev-preuninstall", function (error) {
8+
var child = child_process.exec("node " + nodeArgs.concat(commandArgs).join(" "), function (error) {
49
if (error) {
510
// Some npm versions (3.0, 3.5.1, 3.7.3) remove the NativeScript node_modules before the preuninstall script is executed and the script can't find them (the preuninstall script is like postinstall script).
611
// The issue is described in the npm repository https://github.com/npm/npm/issues/8806 and it is not fixed in version 3.1.1 as commented in the conversation.
7-
console.error("Failed to complete all pre-uninstall steps. ");
12+
console.error("Failed to complete all pre-uninstall steps.");
813
}
914
});
1015

test-scripts/istanbul.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"use strict";
2+
3+
const childProcess = require("child_process");
4+
const path = require("path");
5+
const pathToNodeModules = path.join(__dirname, "..", "node_modules");
6+
const pathToIstanbul = path.join(pathToNodeModules, "istanbul", "lib", "cli.js");
7+
const pathToMocha = path.join(pathToNodeModules, "mocha", "bin", "_mocha");
8+
9+
const istanbulArgs = [ pathToIstanbul, "cover", pathToMocha ];
10+
11+
const nodeArgs = require("../lib/common/scripts/node-args").getNodeArgs();
12+
13+
const args = nodeArgs.concat(istanbulArgs);
14+
15+
const nodeProcess = childProcess.spawn("node", args, { stdio: "inherit" });
16+
17+
nodeProcess.on("close", (code) => {
18+
// We need this handler so if any test fails, we'll exit with same exit code as istanbul.
19+
process.exit(code);
20+
});

test-scripts/mocha.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
const childProcess = require("child_process");
4+
const path = require("path");
5+
const pathToMocha = path.join(__dirname, "..", "node_modules", "mocha", "bin", "_mocha");
6+
7+
const nodeArgs = require("../lib/common/scripts/node-args").getNodeArgs();
8+
9+
const args = nodeArgs.concat(pathToMocha);
10+
11+
const nodeProcess = childProcess.spawn("node", args, { stdio: "inherit" });
12+
nodeProcess.on("close", (code) => {
13+
// We need this handler so if any test fails, we'll exit with same exit code as mocha.
14+
process.exit(code);
15+
});

0 commit comments

Comments
 (0)