Skip to content

Commit 90f31f7

Browse files
committed
remove dependency on environment variables coming from tns
1 parent 17434a7 commit 90f31f7

File tree

2 files changed

+53
-21
lines changed

2 files changed

+53
-21
lines changed

index.js

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
module.exports = function (__dirname) {
2+
return {
3+
findProjectDir: function () {
4+
return findProjectDir(__dirname);
5+
},
6+
postinstall: function () {
7+
return postinstall(__dirname);
8+
},
9+
preuninstall: function () {
10+
return preuninstall(__dirname);
11+
}
12+
};
13+
}
14+
115
var fs = require('fs');
216
var os = require('os');
317
var path = require('path');
@@ -8,18 +22,38 @@ function generateHookName(pkg, hook) {
822
return pkg.name + '.js';
923
}
1024

25+
function findProjectDir(pkgdir) {
26+
var candidateDir = pkgdir;
27+
28+
while (true) {
29+
var oldCandidateDir = candidateDir;
30+
candidateDir = path.dirname(candidateDir);
31+
if (path.basename(candidateDir) === 'node_modules') {
32+
continue;
33+
}
34+
var packageJsonFile = path.join(candidateDir, 'package.json');
35+
if (fs.existsSync(packageJsonFile)) {
36+
return candidateDir;
37+
}
38+
39+
if (oldCandidateDir === candidateDir) {
40+
return;
41+
}
42+
}
43+
}
44+
1145
function forEachHook(pkgdir, callback) {
1246
var pkg = require(path.join(pkgdir, 'package.json'));
1347
var ns = pkg.nativescript;
1448
if (!ns) {
1549
throw Error('Not a NativeScript development module.');
1650
}
1751

18-
var hooksDir = process.env['TNS_HOOKS_DIR'];
19-
if (!hooksDir) {
20-
console.warn('This module should be installed through the `tns install` command, not npm.');
21-
process.exit(1);
52+
var projectDir = findProjectDir(pkgdir);
53+
if (!projectDir) {
54+
return;
2255
}
56+
var hooksDir = path.join(projectDir, 'hooks');
2357

2458
if (ns.hooks) {
2559
ns.hooks.forEach(function (hook) {
@@ -28,8 +62,7 @@ function forEachHook(pkgdir, callback) {
2862
}
2963
}
3064

31-
exports.postinstall = function postinstall(pkgdir) {
32-
var hookFiles = [];
65+
function postinstall(pkgdir) {
3366
forEachHook(pkgdir, function (hooksDir, pkg, hook) {
3467
var hookDir = path.join(hooksDir, hook.type);
3568
if (!fs.existsSync(hookDir)) {
@@ -41,22 +74,21 @@ exports.postinstall = function postinstall(pkgdir) {
4174
var trampoline = util.format('%srequire("%s/%s");', hook.inject ? 'module.exports = ' : '', pkg.name, hook.script);
4275

4376
fs.writeFileSync(hookPath, trampoline + os.EOL);
44-
hookFiles.push(path.relative(pkgdir, hookPath));
4577
});
46-
47-
fs.writeFileSync(path.join(pkgdir, '_hooks.json'), JSON.stringify(hookFiles));
4878
}
4979

50-
exports.preuninstall = function preuninstall(pkgdir) {
51-
try {
52-
var hookFiles = JSON.parse(fs.readFileSync(path.join(pkgdir, '_hooks.json')));
53-
hookFiles.forEach(function (hookRelativePath) {
54-
var hookFileName = path.join(pkgdir, hookRelativePath);
55-
if (fs.existsSync(hookFileName)) {
56-
fs.unlinkSync(hookFileName);
80+
function preuninstall(pkgdir) {
81+
forEachHook(pkgdir, function (hooksDir, pkg, hook) {
82+
var hookDir = path.join(hooksDir, hook.type);
83+
var hookFileName = generateHookName(pkg, hook);
84+
var hookPath = path.join(hookDir, hookFileName);
85+
86+
try {
87+
if (fs.existsSync(hookPath)) {
88+
fs.unlinkSync(hookPath);
5789
}
58-
});
59-
} catch (err) {
60-
console.warn('pkgdir: ' + err.toString());
61-
}
90+
} catch (err) {
91+
console.warn('nativescript-hook: ' + err.toString());
92+
}
93+
});
6294
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-hook",
3-
"version": "0.1.1",
3+
"version": "0.2.0",
44
"description": "Helper module for installing hooks into NativeScript projects",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy