Skip to content

Commit 7d3b471

Browse files
authored
feat: support scoped packages (#19)
1 parent b381cbc commit 7d3b471

File tree

3 files changed

+96
-95
lines changed

3 files changed

+96
-95
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
nativescript-hook
1+
@nativescript/hook
22
=======================================
33

44
This module gives you an easier way to install hooks into NativeScript projects when using the `tns install <module>` command. A project hook is some sort of script that is configured to be executed when the NativeScript CLI executes some action.
@@ -38,12 +38,12 @@ Add a post-install and pre-uninstall script to your `package.json`, if you haven
3838

3939
The post-install script (`postinstall.js` in the example) should contain the following line:
4040
```
41-
require('nativescript-hook')(__dirname).postinstall();
41+
require('@nativescript/hook')(__dirname).postinstall();
4242
```
4343

4444
The pre-uninstall script (`preuninstall.js` in the example) should contain the following line:
4545
```
46-
require('nativescript-hook')(__dirname).preuninstall();
46+
require('@nativescript/hook')(__dirname).preuninstall();
4747
```
4848

4949
These two hooks will take care of installing and removing the hooks from the NativeScript project, when your module is installed or uninstalled.

index.js

Lines changed: 89 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
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-
};
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+
};
1313
};
1414

1515
var fs = require('fs');
@@ -20,104 +20,105 @@ var mkdirp = require('mkdirp');
2020
var glob = require('glob');
2121

2222
function generateHookName(pkg, hook) {
23-
return (hook.name || pkg.name) + '.js';
23+
// flatten scoped packages to names
24+
return ((hook.name || pkg.name).replace(/@/ig, '').replace(/\//ig, '-')) + '.js';
2425
}
2526

2627
function findProjectDir(pkgdir) {
27-
if (process.env.INIT_CWD && _isNativeScriptAppRoot(process.env.INIT_CWD)) {
28-
return process.env.INIT_CWD;
29-
}
28+
if (process.env.INIT_CWD && _isNativeScriptAppRoot(process.env.INIT_CWD)) {
29+
return process.env.INIT_CWD;
30+
}
3031

31-
var candidateDir = pkgdir;
32-
var oldCandidateDir = null;
32+
var candidateDir = pkgdir;
33+
var oldCandidateDir = null;
3334

34-
while (true) {
35-
candidateDir = path.dirname(candidateDir);
36-
if (oldCandidateDir === candidateDir) {
37-
return;
38-
}
35+
while (true) {
36+
candidateDir = path.dirname(candidateDir);
37+
if (oldCandidateDir === candidateDir) {
38+
return;
39+
}
3940

40-
if (path.basename(candidateDir) === 'node_modules') {
41-
continue;
42-
}
41+
if (path.basename(candidateDir) === 'node_modules') {
42+
continue;
43+
}
4344

44-
if (_isNativeScriptAppRoot(candidateDir)) {
45-
return candidateDir;
46-
}
45+
if (_isNativeScriptAppRoot(candidateDir)) {
46+
return candidateDir;
47+
}
4748

48-
oldCandidateDir = candidateDir;
49-
}
49+
oldCandidateDir = candidateDir;
50+
}
5051
}
5152

5253
function _isNativeScriptAppRoot(dir) {
53-
var isNativeScriptAppRoot = false;
54-
var packageJsonFile = path.join(dir, 'package.json');
55-
if (fs.existsSync(packageJsonFile)) {
56-
var packageJsonContent = require(packageJsonFile);
57-
isNativeScriptAppRoot = !!packageJsonContent.nativescript && !!packageJsonContent.nativescript.id;
58-
}
59-
60-
return isNativeScriptAppRoot;
54+
var isNativeScriptAppRoot = false;
55+
var packageJsonFile = path.join(dir, 'package.json');
56+
if (fs.existsSync(packageJsonFile)) {
57+
var packageJsonContent = require(packageJsonFile);
58+
isNativeScriptAppRoot = !!packageJsonContent.nativescript && !!packageJsonContent.nativescript.id;
59+
}
60+
61+
return isNativeScriptAppRoot;
6162
}
6263

6364
function forEachHook(pkgdir, callback) {
64-
var pkg = require(path.join(pkgdir, 'package.json'));
65-
var ns = pkg.nativescript;
66-
if (!ns) {
67-
throw Error('Not a NativeScript development module.');
68-
}
69-
70-
var projectDir = findProjectDir(pkgdir);
71-
if (!projectDir) {
72-
return;
73-
}
74-
var hooksDir = path.join(projectDir, 'hooks');
75-
76-
if (ns.hooks) {
77-
ns.hooks.forEach(function (hook) {
78-
callback(hooksDir, pkg, hook);
79-
});
80-
}
65+
var pkg = require(path.join(pkgdir, 'package.json'));
66+
var ns = pkg.nativescript;
67+
if (!ns) {
68+
throw Error('Not a NativeScript development module.');
69+
}
70+
71+
var projectDir = findProjectDir(pkgdir);
72+
if (!projectDir) {
73+
return;
74+
}
75+
var hooksDir = path.join(projectDir, 'hooks');
76+
77+
if (ns.hooks) {
78+
ns.hooks.forEach(function (hook) {
79+
callback(hooksDir, pkg, hook);
80+
});
81+
}
8182
}
8283

8384
function hookInstalled(hookDir, pkg, hook) {
84-
var hookBaseName = pkg.name;
85-
var hookGlob = path.join(hookDir, "*" + hookBaseName + "*");
86-
var files = glob.sync(hookGlob);
87-
return files.length > 0;
85+
var hookBaseName = pkg.name;
86+
var hookGlob = path.join(hookDir, "*" + hookBaseName + "*");
87+
var files = glob.sync(hookGlob);
88+
return files.length > 0;
8889
}
8990

9091
function postinstall(pkgdir) {
91-
forEachHook(pkgdir, function (hooksDir, pkg, hook) {
92-
var hookDir = path.join(hooksDir, hook.type);
93-
if (!fs.existsSync(hookDir)) {
94-
mkdirp.sync(hookDir);
95-
}
96-
if (hookInstalled(hookDir, pkg, hook)) {
97-
console.log(`Hook already installed: ${pkg.name} at location: ${hookDir}`);
98-
return;
99-
}
100-
var hookFileName = generateHookName(pkg, hook);
101-
var hookPath = path.join(hookDir, hookFileName);
102-
103-
var trampoline = util.format('%srequire("%s/%s");', hook.inject ? 'module.exports = ' : '', pkg.name, hook.script);
104-
105-
fs.writeFileSync(hookPath, trampoline + os.EOL);
106-
});
92+
forEachHook(pkgdir, function (hooksDir, pkg, hook) {
93+
var hookDir = path.join(hooksDir, hook.type);
94+
if (!fs.existsSync(hookDir)) {
95+
mkdirp.sync(hookDir);
96+
}
97+
if (hookInstalled(hookDir, pkg, hook)) {
98+
console.log(`Hook already installed: ${pkg.name} at location: ${hookDir}`);
99+
return;
100+
}
101+
var hookFileName = generateHookName(pkg, hook);
102+
var hookPath = path.join(hookDir, hookFileName);
103+
104+
var trampoline = util.format('%srequire("%s/%s");', hook.inject ? 'module.exports = ' : '', pkg.name, hook.script);
105+
106+
fs.writeFileSync(hookPath, trampoline + os.EOL);
107+
});
107108
}
108109

109110
function preuninstall(pkgdir) {
110-
forEachHook(pkgdir, function (hooksDir, pkg, hook) {
111-
var hookDir = path.join(hooksDir, hook.type);
112-
var hookFileName = generateHookName(pkg, hook);
113-
var hookPath = path.join(hookDir, hookFileName);
114-
115-
try {
116-
if (fs.existsSync(hookPath)) {
117-
fs.unlinkSync(hookPath);
118-
}
119-
} catch (err) {
120-
console.warn('nativescript-hook: ' + err.toString());
121-
}
122-
});
111+
forEachHook(pkgdir, function (hooksDir, pkg, hook) {
112+
var hookDir = path.join(hooksDir, hook.type);
113+
var hookFileName = generateHookName(pkg, hook);
114+
var hookPath = path.join(hookDir, hookFileName);
115+
116+
try {
117+
if (fs.existsSync(hookPath)) {
118+
fs.unlinkSync(hookPath);
119+
}
120+
} catch (err) {
121+
console.warn('@nativescript/hook: ' + err.toString());
122+
}
123+
});
123124
}

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "nativescript-hook",
3-
"version": "0.2.5",
2+
"name": "@nativescript/hook",
3+
"version": "1.0.0",
44
"description": "Helper module for installing hooks into NativeScript projects",
55
"main": "index.js",
66
"scripts": {
@@ -13,7 +13,7 @@
1313
"url": "https://github.com/NativeScript/nativescript-hook.git"
1414
},
1515
"dependencies": {
16-
"glob": "^6.0.1",
17-
"mkdirp": "^0.5.1"
16+
"glob": "^7.1.0",
17+
"mkdirp": "^1.0.4"
1818
}
1919
}

0 commit comments

Comments
 (0)