Skip to content

Commit dfa2667

Browse files
Merge pull request #5 from NativeScript/vladimirov/add-hook
Ensure after-prepare hook is added to preoject
2 parents 27bf1fd + 738d95f commit dfa2667

File tree

6 files changed

+75
-5
lines changed

6 files changed

+75
-5
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,5 @@ test-reports.xml
2929

3030
npm-debug.log
3131
node_modules
32-
!preuninstall.js
33-
!postinstall.js
3432
!Gruntfile.js
3533
.d.ts

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ module.exports = function(grunt) {
8686
},
8787

8888
clean: {
89-
src: ["**/*.js*", "!**/*.json", "!postinstall.js", "!preuninstall.js", "!Gruntfile.js", "!node_modules/**/*", "*.tgz"]
89+
src: ["**/*.js*", "!**/*.json", "!Gruntfile.js", "!node_modules/**/*", "*.tgz"]
9090
}
9191
});
9292

lib/hook-helper.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"use strict";
2+
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
6+
export function findProjectDir(){
7+
// start from the root of ns-unit-test-runner
8+
let candidateDir = path.join(__dirname, "..");
9+
10+
while (true) {
11+
let oldCandidateDir = candidateDir;
12+
candidateDir = path.dirname(candidateDir);
13+
if (path.basename(candidateDir) === 'node_modules') {
14+
continue;
15+
}
16+
17+
let packageJsonFile = path.join(candidateDir, 'package.json');
18+
if (fs.existsSync(packageJsonFile)) {
19+
return candidateDir;
20+
}
21+
22+
if (oldCandidateDir === candidateDir) {
23+
return;
24+
}
25+
}
26+
}
27+
28+
export function getHooksDir() {
29+
return path.join(findProjectDir(), 'hooks')
30+
}
31+
32+
export function getAfterPrepareHookDir() {
33+
return path.join(getHooksDir(), "after-prepare");
34+
}
35+
36+
export function getHookFilePath() {
37+
return path.join(getAfterPrepareHookDir(), "nativescript-unit-test-runner.js");
38+
}

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"name": "nativescript-unit-test-runner",
3-
"version": "0.3.1",
3+
"version": "0.3.2",
44
"description": "NativeScript unit test runner component.",
55
"main": "app.js",
66
"scripts": {
7-
"test": "exit 0"
7+
"test": "exit 0",
8+
"postinstall": "node postinstall.js",
9+
"preuninstall": "node preuninstall.js"
810
},
911
"repository": {
1012
"type": "git",

postinstall.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
"use strict";
2+
3+
import * as fs from "fs";
4+
import * as path from "path";
5+
import { EOL } from "os";
6+
import * as hookHelper from "./lib/hook-helper";
7+
8+
let projectDir = hookHelper.findProjectDir();
9+
if(projectDir) {
10+
let hooksDir = hookHelper.getHooksDir(),
11+
afterPrepareHookDir = hookHelper.getAfterPrepareHookDir(),
12+
content = 'module.exports = require("nativescript-unit-test-runner/lib/after-prepare");';
13+
if(!fs.existsSync(hooksDir)) {
14+
fs.mkdirSync(hooksDir);
15+
}
16+
17+
if(!fs.existsSync(afterPrepareHookDir)) {
18+
fs.mkdirSync(afterPrepareHookDir);
19+
}
20+
21+
fs.writeFileSync(hookHelper.getHookFilePath(), content + EOL);
22+
}

preuninstall.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"use strict";
2+
3+
import * as fs from "fs";
4+
import * as hookHelper from "./lib/hook-helper";
5+
6+
let hookPath = hookHelper.getHookFilePath();
7+
8+
if (fs.existsSync(hookPath)) {
9+
fs.unlinkSync(hookPath);
10+
}

0 commit comments

Comments
 (0)