Skip to content

Commit 6782e56

Browse files
committed
Merge pull request #1 from NativeScript/hdeshev/check-if-hook-exists
Check if hook exists and don't reinstall.
2 parents 90f31f7 + 508e135 commit 6782e56

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ module.exports = function (__dirname) {
1010
return preuninstall(__dirname);
1111
}
1212
};
13-
}
13+
};
1414

1515
var fs = require('fs');
1616
var os = require('os');
1717
var path = require('path');
1818
var util = require('util');
1919
var mkdirp = require('mkdirp');
20+
var glob = require('glob');
2021

2122
function generateHookName(pkg, hook) {
2223
return pkg.name + '.js';
@@ -57,17 +58,28 @@ function forEachHook(pkgdir, callback) {
5758

5859
if (ns.hooks) {
5960
ns.hooks.forEach(function (hook) {
60-
callback(hooksDir, pkg, hook)
61+
callback(hooksDir, pkg, hook);
6162
});
6263
}
6364
}
6465

66+
function hookInstalled(hookDir, pkg, hook) {
67+
var hookBaseName = pkg.name;
68+
var hookGlob = path.join(hookDir, "*" + hookBaseName + "*");
69+
var files = glob.sync(hookGlob);
70+
return files.length > 0;
71+
}
72+
6573
function postinstall(pkgdir) {
6674
forEachHook(pkgdir, function (hooksDir, pkg, hook) {
6775
var hookDir = path.join(hooksDir, hook.type);
6876
if (!fs.existsSync(hookDir)) {
6977
mkdirp.sync(hookDir);
7078
}
79+
if (hookInstalled(hookDir, pkg, hook)) {
80+
console.log('Hook already installed: ' + pkg.name);
81+
return;
82+
}
7183
var hookFileName = generateHookName(pkg, hook);
7284
var hookPath = path.join(hookDir, hookFileName);
7385

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-hook",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "Helper module for installing hooks into NativeScript projects",
55
"main": "index.js",
66
"scripts": {
@@ -13,6 +13,7 @@
1313
"url": "https://github.com/NativeScript/nativescript-hook.git"
1414
},
1515
"dependencies": {
16+
"glob": "^6.0.1",
1617
"mkdirp": "^0.5.1"
1718
}
1819
}

0 commit comments

Comments
 (0)