{"id":15018462,"url":"https://github.com/nativescript/nativescript-hook","last_synced_at":"2025-10-19T16:30:57.608Z","repository":{"id":1878028,"uuid":"45173579","full_name":"NativeScript/nativescript-hook","owner":"NativeScript","description":"Helper module for installing hooks into NativeScript projects","archived":false,"fork":false,"pushed_at":"2023-12-01T09:06:57.000Z","size":22,"stargazers_count":15,"open_issues_count":8,"forks_count":9,"subscribers_count":27,"default_branch":"master","last_synced_at":"2024-10-29T15:51:52.185Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/NativeScript.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":"NativeScript","open_collective":"nativescript"}},"created_at":"2015-10-29T09:37:27.000Z","updated_at":"2023-12-06T22:00:16.000Z","dependencies_parsed_at":"2023-12-16T17:44:30.738Z","dependency_job_id":null,"html_url":"https://github.com/NativeScript/nativescript-hook","commit_stats":{"total_commits":15,"total_committers":10,"mean_commits":1.5,"dds":0.7333333333333334,"last_synced_commit":"7d3b4715fa3e46afefa49ae5e2cedcb5acb0cc90"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fnativescript-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fnativescript-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fnativescript-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/NativeScript%2Fnativescript-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/NativeScript","download_url":"https://codeload.github.com/NativeScript/nativescript-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":237163217,"owners_count":19265228,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T19:51:59.770Z","updated_at":"2025-10-19T16:30:57.601Z","avatar_url":"https://github.com/NativeScript.png","language":"JavaScript","readme":"@nativescript/hook\n=======================================\n\nAn easier way to install hooks into NativeScript projects when using the `ns install \u003cmodule\u003e` command. A project hook is a script that is configured to be executed when the NativeScript CLI executes some action.\n\nHooks 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.\n\nThis simplifies the process of installing the scripts into the right project folder.\n\nHow to use\n----------\n\n### Describe the hooks\n\nFirst, add a description of your hooks to the module's `package.json`. Here's an example:\n```json\n{\n  \"nativescript\": {\n    \"hooks\": [\n      {\n        \"type\": \"before-prepare\",\n        \"script\": \"lib/before-prepare.js\"\n      }\n    ]\n  },\n}\n```\nThe 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.\n\n### Install the hooks\n\nAdd a post-install and pre-uninstall script to your `package.json`, if you haven't done so already:\n\n```ts\n  \"scripts\": {\n    ...\n    \"postinstall\": \"node postinstall.js\",\n    \"preuninstall\": \"node preuninstall.js\"\n  },\n```\n\nThe post-install script (`postinstall.js` in the example) should contain the following line:\n\n```javascript\nimport path from 'path';\nimport hook from '@nativescript/hook';\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\nhook(__dirname).postinstall();\n```\n\nThe pre-uninstall script (`preuninstall.js` in the example) should contain the following line:\n\n```javascript\nimport path from 'path';\nimport hook from '@nativescript/hook';\nimport { fileURLToPath } from \"url\";\n\nconst __filename = fileURLToPath(import.meta.url);\nconst __dirname = path.dirname(__filename);\nhook(__dirname).preuninstall();\n```\n\nThese two hooks will take care of installing and removing the hooks from the NativeScript project, when your module is installed or uninstalled.\n\n`ns install \u003cmodule\u003e`\n----------------------\nNativeScript modules that install hooks are intended to be installed using the `ns install \u003cmodule\u003e` 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 \u003cmodule\u003e`:\n* `TNS_HOOKS_DIR` - the directory where the hooks should be installed. It may or may not exist.\n* `TNS_PROJECT_DIR` - the current project directory.\n\nModules installed this way can be uninstalled simply by running `npm rm --save-dev`.\n\nIn-process hooks\n----------------\nBy 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.\n\nTo 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:\n\n```javascript\nexport default function($logger) {\n};\n```\nThen, the hook script will be required by the CLI and the exported function will be called through the injector.\n\nHooks can take a special injected argument named `hookArgs`:\n\n```javascript\nexport default function(hookArgs) {\n};\n```\n\n`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.\n\nIf you execute asynchronous code in your hook, you need to return a promise, otherwise execution will continue before your hook completes:\n\n```javascript\nimport mkdirp from 'mkdirp';\n\nexport default function($logger) {\n  return new Promise(function(resolve, reject) {\n      mkdirp('somedir', function(err) {\n          if (err) {\n            reject(err);\n          else {\n            resolve();\n          }\n        })\n    });\n}\n```\n\nAnd finally, when installing in-process hooks through this module, you need to explicitly specify that using the `inject` property of the script descriptor in the `package.json`:\n```json\n{\n  \"nativescript\": {\n    \"hooks\": [\n      {\n        \"type\": \"after-prepare\",\n        \"script\": \"lib/after-prepare.js\",\n        \"inject\": true\n      }\n    ]\n  },\n}\n```\n\nYou have the ability to define a custom name to your hook in `package.json`, this attribute is optional and defaults to the plugin package name:\n```json\n{\n  \"nativescript\": {\n    \"hooks\": [\n      {\n        \"type\": \"after-prepare\",\n        \"script\": \"lib/after-prepare.js\",\n        \"name\": \"my-custom-hook\"\n      }\n    ]\n  },\n}\n```\n\n\n","funding_links":["https://github.com/sponsors/NativeScript","https://opencollective.com/nativescript"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnativescript%2Fnativescript-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnativescript%2Fnativescript-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnativescript%2Fnativescript-hook/lists"}