From 51eceec0395fc1e61ca47daf5278754a2d9584d0 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Sun, 6 Jul 2025 09:33:23 -0700 Subject: [PATCH 1/3] feat: esm support --- index.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index be8dc02..fe97c80 100644 --- a/index.js +++ b/index.js @@ -100,7 +100,9 @@ function postinstall(pkgdir) { 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); + var trampoline = `import hooks from "${pkg.name}/${hook.script}"; + +export default hooks;` fs.writeFileSync(hookPath, trampoline + os.EOL); }); From 07ac72ea2a7e0aa0f970afd5a5385b21bcecb666 Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Sun, 6 Jul 2025 09:33:44 -0700 Subject: [PATCH 2/3] chore: dep updates --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3356d54..81dd855 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "url": "https://github.com/NativeScript/nativescript-hook.git" }, "dependencies": { - "glob": "^7.1.0", - "mkdirp": "^1.0.4" + "glob": "^11.0.0", + "mkdirp": "^3.0.1" } } From 7afe91949133200316f239af80be68ffa7898ddc Mon Sep 17 00:00:00 2001 From: Nathan Walker Date: Sun, 6 Jul 2025 09:55:05 -0700 Subject: [PATCH 3/3] docs: readme --- README.md | 56 ++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 6a36853..20644bc 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,17 @@ @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. +An easier way to install hooks into NativeScript projects when using the `ns install ` command. A project hook is a script that is configured to be executed when the NativeScript CLI executes some action. -Hooks go into the `hooks/` folder of a project. For example, when `tns prepare ...` is executed, all script files in the `hooks/before-prepare/` and `hooks/after-prepare/` folders are executed, as well. +Hooks go into the `hooks/` folder of a project. For example, when `ns prepare ...` is executed, all script files in the `hooks/before-prepare/` and `hooks/after-prepare/` folders are executed as well. -This module simplifies the process of installing the scripts into the right project folder. +This simplifies the process of installing the scripts into the right project folder. How to use ---------- ### Describe the hooks + First, add a description of your hooks to the module's `package.json`. Here's an example: ```json { @@ -24,11 +25,13 @@ First, add a description of your hooks to the module's `package.json`. Here's an }, } ``` -The above specifies that the script `lib/before-prepare.js` should be executed when the `tns prepare ...` command is executed. the `"type"` property specifies the type of the hook to install. The `"script"` property specifies the path to the script to execute. You can add more hooks by extending the `"hooks"` array. +The above specifies that the script `lib/before-prepare.js` should be executed when the `ns prepare ...` command is executed. the `"type"` property specifies the type of the hook to install. The `"script"` property specifies the path to the script to execute. You can add more hooks by extending the `"hooks"` array. ### Install the hooks + Add a post-install and pre-uninstall script to your `package.json`, if you haven't done so already: -``` + +```ts "scripts": { ... "postinstall": "node postinstall.js", @@ -37,20 +40,34 @@ 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(); + +```javascript +import path from 'path'; +import hook from '@nativescript/hook'; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +hook(__dirname).postinstall(); ``` The pre-uninstall script (`preuninstall.js` in the example) should contain the following line: -``` -require('@nativescript/hook')(__dirname).preuninstall(); + +```javascript +import path from 'path'; +import hook from '@nativescript/hook'; +import { fileURLToPath } from "url"; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +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. -`tns install ` +`ns install ` ---------------------- -NativeScript modules that install hooks are intended to be installed using the `tns install ` command, not through npm directly. During module installation the NativeScript CLI provides information to the post-install script where to put the hooks. The following environment variables are defined when installing through `tns install `: +NativeScript modules that install hooks are intended to be installed using the `ns install ` command, not through npm directly. During module installation the NativeScript CLI provides information to the post-install script where to put the hooks. The following environment variables are defined when installing through `ns install `: * `TNS_HOOKS_DIR` - the directory where the hooks should be installed. It may or may not exist. * `TNS_PROJECT_DIR` - the current project directory. @@ -60,24 +77,29 @@ In-process hooks ---------------- By default, hooks are executed in a child process and execution continues when the child process dies. This gives you flexibility when writing your hooks, but doesn't allow you to use any of the services of the CLI. -To that end, in-process hooks allow you to execute your hooks in such a way so that you can use any of the services available from the injector. In-process hooks work only for JavaScript hooks. To enable in-process execution, all you need to have is a `module.exports = ...` statement in the hook. For example, if the hook script is: +To that end, in-process hooks allow you to execute your hooks in such a way so that you can use any of the services available from the injector. In-process hooks work only for JavaScript hooks. To enable in-process execution, all you need to have is a `export default function(...)` statement in the hook. For example, if the hook script is: + ```javascript -module.exports = function($logger) { +export default function($logger) { }; ``` -Then, the hook script will be require'd by the CLI and the exported function will be called through the injector. +Then, the hook script will be required by the CLI and the exported function will be called through the injector. Hooks can take a special injected argument named `hookArgs`: + ```javascript -module.exports = function(hookArgs) { +export default function(hookArgs) { }; ``` + `hookArgs` is a hash containing all the arguments passed to the hooked method. For example, the `prepare` hook is activated by the CLI method `preparePlatform(platform: string)`. Here, the hook will get the value of the `platform` argument in the `hookArgs.platform` property. If you execute asynchronous code in your hook, you need to return a promise, otherwise execution will continue before your hook completes: + ```javascript -var mkdirp = require('mkdirp'); -module.exports = function($logger) { +import mkdirp from 'mkdirp'; + +export default function($logger) { return new Promise(function(resolve, reject) { mkdirp('somedir', function(err) { if (err) {