Skip to content

Commit 4990f91

Browse files
Add after-prepare hook to change the entry-point of application
Add after-prepare hook that will change the entry point of application in case `test` command is executed. Use `nativescript-hook` dependency in order to install the hook.
1 parent 937e458 commit 4990f91

File tree

5 files changed

+124
-4
lines changed

5 files changed

+124
-4
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,5 @@ test-reports.xml
2929

3030
npm-debug.log
3131
node_modules
32+
!preuninstall.js
33+
!postinstall.js

lib/after-prepare.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as path from "path";
2+
import * as fs from "fs";
3+
4+
module.exports = function ($platformsData, $testExecutionService) {
5+
if($testExecutionService && $testExecutionService.platform) {
6+
let platformData = $platformsData.getPlatformData($testExecutionService.platform),
7+
projectFilesPath = path.join(platformData.appDestinationDirectoryPath, "app"),
8+
packageJsonPath = path.join(projectFilesPath, 'package.json'),
9+
packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
10+
11+
// When test command is used in ns-cli, we should change the entry point of the application
12+
packageJson.main = "./tns_modules/nativescript-unit-test-runner/app.js";
13+
fs.writeFileSync(packageJsonPath, JSON.stringify(packageJson));
14+
}
15+
}

package.json

+19-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"description": "NativeScript unit test runner component.",
55
"main": "app.js",
66
"scripts": {
7-
"test": "exit 0"
7+
"test": "exit 0",
8+
"preuninstall": "node preuninstall.js",
9+
"postinstall": "node postinstall.js"
810
},
911
"repository": {
1012
"type": "git",
@@ -14,12 +16,25 @@
1416
"license": "Apache-2.0",
1517
"devDependencies": {
1618
"grunt": "^0.4.5",
19+
"grunt-contrib-clean": "^1.0.0",
20+
"grunt-contrib-watch": "^0.6.1",
21+
"grunt-shell": "^1.1.2",
1722
"grunt-ts": "^5.0.1"
1823
},
1924
"nativescript": {
2025
"platforms": {
21-
"android": "1.6.0",
22-
"ios": "1.6.0"
23-
}
26+
"android": "1.6.0",
27+
"ios": "1.6.0"
28+
},
29+
"hooks": [
30+
{
31+
"type": "after-prepare",
32+
"script": "lib/after-prepare.js",
33+
"inject": true
34+
}
35+
]
36+
},
37+
"dependencies": {
38+
"nativescript-hook": "^0.2.1"
2439
}
2540
}

postinstall.js

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
var hook = require('nativescript-hook')(__dirname);
2+
hook.postinstall();
3+
4+
var fs = require('fs');
5+
var path = require('path');
6+
7+
var projectDir = hook.findProjectDir();
8+
if (projectDir) {
9+
createTsconfig();
10+
createReferenceFile();
11+
installTypescript();
12+
}
13+
14+
function createReferenceFile() {
15+
var referenceFilePath = path.join(projectDir, 'references.d.ts'),
16+
content = '/// <reference path="./node_modules/tns-core-modules/tns-core-modules.d.ts" /> Needed for autocompletion and compilation.';
17+
18+
if (!fs.existsSync(referenceFilePath)) {
19+
fs.appendFileSync(referenceFilePath, content);
20+
}
21+
}
22+
23+
function createTsconfig() {
24+
var tsconfigPath = path.join(projectDir, 'tsconfig.json');
25+
var tsconfig = {};
26+
27+
if (fs.existsSync(tsconfigPath)) {
28+
try {
29+
tsconfig = JSON.parse(fs.readFileSync(tsconfigPath))
30+
} catch (err) {
31+
console.warn('tsconfig.json: ' + err.toString());
32+
}
33+
}
34+
35+
tsconfig.compilerOptions = tsconfig.compilerOptions || {
36+
module: "commonjs",
37+
target: "es5",
38+
sourceMap: true,
39+
experimentalDecorators: true,
40+
noEmitHelpers: true,
41+
};
42+
43+
tsconfig.exclude = ['node_modules', 'platforms'];
44+
45+
var coreModulesPath = 'node_modules/tns-core-modules/';
46+
var coreModulesTypingsPath = 'node_modules/tns-core-modules/tns-core-modules.d.ts';
47+
48+
try {
49+
var coreModulesPackageJson = JSON.parse(fs.readFileSync(path.join(projectDir, coreModulesPath, 'package.json')));
50+
if (coreModulesPackageJson.typings) {
51+
coreModulesTypingsPath = coreModulesPath + coreModulesPackageJson.typings;
52+
}
53+
} catch (err) {
54+
console.warn('tns-core-modules/package.json: ' + err.toString());
55+
}
56+
57+
fs.writeFileSync(tsconfigPath, JSON.stringify(tsconfig, null, 4));
58+
}
59+
60+
function getProjectTypeScriptVersion() {
61+
try {
62+
var packageJsonPath = path.join(projectDir, "package.json"),
63+
packageJsonContent = fs.readFileSync(packageJsonPath, "utf8"),
64+
jsonContent = JSON.parse(packageJsonContent);
65+
66+
return (jsonContent.dependencies && jsonContent.dependencies.typescript)
67+
|| (jsonContent.devDependencies && jsonContent.devDependencies.typescript);
68+
} catch(err) {
69+
console.error(err);
70+
return null;
71+
}
72+
}
73+
74+
function installTypescript() {
75+
var installedTypeScriptVersion = getProjectTypeScriptVersion();
76+
if(installedTypeScriptVersion) {
77+
console.log("Project already targets TypeScript " + installedTypeScriptVersion);
78+
} else {
79+
require('child_process').exec('npm install --save-dev typescript', { cwd: projectDir }, function (err, stdout, stderr) {
80+
if (err) {
81+
console.warn('npm: ' + err.toString());
82+
}
83+
process.stdout.write(stdout);
84+
process.stderr.write(stderr);
85+
});
86+
}
87+
}

preuninstall.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('nativescript-hook')(__dirname).preuninstall();

0 commit comments

Comments
 (0)