From 7d3b4715fa3e46afefa49ae5e2cedcb5acb0cc90 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Thu, 2 Jul 2020 17:45:03 -0700 Subject: [PATCH] feat: support scoped packages (#19) --- README.md | 6 +- index.js | 177 ++++++++++++++++++++++++++------------------------- package.json | 8 +-- 3 files changed, 96 insertions(+), 95 deletions(-) diff --git a/README.md b/README.md index 64ff8d4..6a36853 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -nativescript-hook +@nativescript/hook ======================================= This module gives you an easier way to install hooks into NativeScript projects when using the `tns install ` 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 The post-install script (`postinstall.js` in the example) should contain the following line: ``` -require('nativescript-hook')(__dirname).postinstall(); +require('@nativescript/hook')(__dirname).postinstall(); ``` The pre-uninstall script (`preuninstall.js` in the example) should contain the following line: ``` -require('nativescript-hook')(__dirname).preuninstall(); +require('@nativescript/hook')(__dirname).preuninstall(); ``` These two hooks will take care of installing and removing the hooks from the NativeScript project, when your module is installed or uninstalled. diff --git a/index.js b/index.js index e46082d..6cccf7e 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,15 @@ module.exports = function (__dirname) { - return { - findProjectDir: function () { - return findProjectDir(__dirname); - }, - postinstall: function () { - return postinstall(__dirname); - }, - preuninstall: function () { - return preuninstall(__dirname); - } - }; + return { + findProjectDir: function () { + return findProjectDir(__dirname); + }, + postinstall: function () { + return postinstall(__dirname); + }, + preuninstall: function () { + return preuninstall(__dirname); + } + }; }; var fs = require('fs'); @@ -20,104 +20,105 @@ var mkdirp = require('mkdirp'); var glob = require('glob'); function generateHookName(pkg, hook) { - return (hook.name || pkg.name) + '.js'; + // flatten scoped packages to names + return ((hook.name || pkg.name).replace(/@/ig, '').replace(/\//ig, '-')) + '.js'; } function findProjectDir(pkgdir) { - if (process.env.INIT_CWD && _isNativeScriptAppRoot(process.env.INIT_CWD)) { - return process.env.INIT_CWD; - } + if (process.env.INIT_CWD && _isNativeScriptAppRoot(process.env.INIT_CWD)) { + return process.env.INIT_CWD; + } - var candidateDir = pkgdir; - var oldCandidateDir = null; + var candidateDir = pkgdir; + var oldCandidateDir = null; - while (true) { - candidateDir = path.dirname(candidateDir); - if (oldCandidateDir === candidateDir) { - return; - } + while (true) { + candidateDir = path.dirname(candidateDir); + if (oldCandidateDir === candidateDir) { + return; + } - if (path.basename(candidateDir) === 'node_modules') { - continue; - } + if (path.basename(candidateDir) === 'node_modules') { + continue; + } - if (_isNativeScriptAppRoot(candidateDir)) { - return candidateDir; - } + if (_isNativeScriptAppRoot(candidateDir)) { + return candidateDir; + } - oldCandidateDir = candidateDir; - } + oldCandidateDir = candidateDir; + } } function _isNativeScriptAppRoot(dir) { - var isNativeScriptAppRoot = false; - var packageJsonFile = path.join(dir, 'package.json'); - if (fs.existsSync(packageJsonFile)) { - var packageJsonContent = require(packageJsonFile); - isNativeScriptAppRoot = !!packageJsonContent.nativescript && !!packageJsonContent.nativescript.id; - } - - return isNativeScriptAppRoot; + var isNativeScriptAppRoot = false; + var packageJsonFile = path.join(dir, 'package.json'); + if (fs.existsSync(packageJsonFile)) { + var packageJsonContent = require(packageJsonFile); + isNativeScriptAppRoot = !!packageJsonContent.nativescript && !!packageJsonContent.nativescript.id; + } + + return isNativeScriptAppRoot; } function forEachHook(pkgdir, callback) { - var pkg = require(path.join(pkgdir, 'package.json')); - var ns = pkg.nativescript; - if (!ns) { - throw Error('Not a NativeScript development module.'); - } - - var projectDir = findProjectDir(pkgdir); - if (!projectDir) { - return; - } - var hooksDir = path.join(projectDir, 'hooks'); - - if (ns.hooks) { - ns.hooks.forEach(function (hook) { - callback(hooksDir, pkg, hook); - }); - } + var pkg = require(path.join(pkgdir, 'package.json')); + var ns = pkg.nativescript; + if (!ns) { + throw Error('Not a NativeScript development module.'); + } + + var projectDir = findProjectDir(pkgdir); + if (!projectDir) { + return; + } + var hooksDir = path.join(projectDir, 'hooks'); + + if (ns.hooks) { + ns.hooks.forEach(function (hook) { + callback(hooksDir, pkg, hook); + }); + } } function hookInstalled(hookDir, pkg, hook) { - var hookBaseName = pkg.name; - var hookGlob = path.join(hookDir, "*" + hookBaseName + "*"); - var files = glob.sync(hookGlob); - return files.length > 0; + var hookBaseName = pkg.name; + var hookGlob = path.join(hookDir, "*" + hookBaseName + "*"); + var files = glob.sync(hookGlob); + return files.length > 0; } function postinstall(pkgdir) { - forEachHook(pkgdir, function (hooksDir, pkg, hook) { - var hookDir = path.join(hooksDir, hook.type); - if (!fs.existsSync(hookDir)) { - mkdirp.sync(hookDir); - } - if (hookInstalled(hookDir, pkg, hook)) { - console.log(`Hook already installed: ${pkg.name} at location: ${hookDir}`); - return; - } - var hookFileName = generateHookName(pkg, hook); - var hookPath = path.join(hookDir, hookFileName); - - var trampoline = util.format('%srequire("%s/%s");', hook.inject ? 'module.exports = ' : '', pkg.name, hook.script); - - fs.writeFileSync(hookPath, trampoline + os.EOL); - }); + forEachHook(pkgdir, function (hooksDir, pkg, hook) { + var hookDir = path.join(hooksDir, hook.type); + if (!fs.existsSync(hookDir)) { + mkdirp.sync(hookDir); + } + if (hookInstalled(hookDir, pkg, hook)) { + console.log(`Hook already installed: ${pkg.name} at location: ${hookDir}`); + return; + } + var hookFileName = generateHookName(pkg, hook); + var hookPath = path.join(hookDir, hookFileName); + + var trampoline = util.format('%srequire("%s/%s");', hook.inject ? 'module.exports = ' : '', pkg.name, hook.script); + + fs.writeFileSync(hookPath, trampoline + os.EOL); + }); } function preuninstall(pkgdir) { - forEachHook(pkgdir, function (hooksDir, pkg, hook) { - var hookDir = path.join(hooksDir, hook.type); - var hookFileName = generateHookName(pkg, hook); - var hookPath = path.join(hookDir, hookFileName); - - try { - if (fs.existsSync(hookPath)) { - fs.unlinkSync(hookPath); - } - } catch (err) { - console.warn('nativescript-hook: ' + err.toString()); - } - }); + forEachHook(pkgdir, function (hooksDir, pkg, hook) { + var hookDir = path.join(hooksDir, hook.type); + var hookFileName = generateHookName(pkg, hook); + var hookPath = path.join(hookDir, hookFileName); + + try { + if (fs.existsSync(hookPath)) { + fs.unlinkSync(hookPath); + } + } catch (err) { + console.warn('@nativescript/hook: ' + err.toString()); + } + }); } diff --git a/package.json b/package.json index 1687c9a..0376224 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "nativescript-hook", - "version": "0.2.5", + "name": "@nativescript/hook", + "version": "1.0.0", "description": "Helper module for installing hooks into NativeScript projects", "main": "index.js", "scripts": { @@ -13,7 +13,7 @@ "url": "https://github.com/NativeScript/nativescript-hook.git" }, "dependencies": { - "glob": "^6.0.1", - "mkdirp": "^0.5.1" + "glob": "^7.1.0", + "mkdirp": "^1.0.4" } }