Skip to content

Commit aeee4d5

Browse files
authored
feat: esm support (#24)
1 parent 96380ea commit aeee4d5

File tree

3 files changed

+44
-20
lines changed

3 files changed

+44
-20
lines changed

README.md

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
@nativescript/hook
22
=======================================
33

4-
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.
4+
An easier way to install hooks into NativeScript projects when using the `ns install <module>` command. A project hook is a script that is configured to be executed when the NativeScript CLI executes some action.
55

6-
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.
6+
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.
77

8-
This module simplifies the process of installing the scripts into the right project folder.
8+
This simplifies the process of installing the scripts into the right project folder.
99

1010
How to use
1111
----------
1212

1313
### Describe the hooks
14+
1415
First, add a description of your hooks to the module's `package.json`. Here's an example:
1516
```json
1617
{
@@ -24,11 +25,13 @@ First, add a description of your hooks to the module's `package.json`. Here's an
2425
},
2526
}
2627
```
27-
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.
28+
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.
2829

2930
### Install the hooks
31+
3032
Add a post-install and pre-uninstall script to your `package.json`, if you haven't done so already:
31-
```
33+
34+
```ts
3235
"scripts": {
3336
...
3437
"postinstall": "node postinstall.js",
@@ -37,20 +40,34 @@ Add a post-install and pre-uninstall script to your `package.json`, if you haven
3740
```
3841

3942
The post-install script (`postinstall.js` in the example) should contain the following line:
40-
```
41-
require('@nativescript/hook')(__dirname).postinstall();
43+
44+
```javascript
45+
import path from 'path';
46+
import hook from '@nativescript/hook';
47+
import { fileURLToPath } from "url";
48+
49+
const __filename = fileURLToPath(import.meta.url);
50+
const __dirname = path.dirname(__filename);
51+
hook(__dirname).postinstall();
4252
```
4353
4454
The pre-uninstall script (`preuninstall.js` in the example) should contain the following line:
45-
```
46-
require('@nativescript/hook')(__dirname).preuninstall();
55+
56+
```javascript
57+
import path from 'path';
58+
import hook from '@nativescript/hook';
59+
import { fileURLToPath } from "url";
60+
61+
const __filename = fileURLToPath(import.meta.url);
62+
const __dirname = path.dirname(__filename);
63+
hook(__dirname).preuninstall();
4764
```
4865
4966
These two hooks will take care of installing and removing the hooks from the NativeScript project, when your module is installed or uninstalled.
5067
51-
`tns install <module>`
68+
`ns install <module>`
5269
----------------------
53-
NativeScript modules that install hooks are intended to be installed using the `tns install <module>` 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 <module>`:
70+
NativeScript modules that install hooks are intended to be installed using the `ns install <module>` 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 <module>`:
5471
* `TNS_HOOKS_DIR` - the directory where the hooks should be installed. It may or may not exist.
5572
* `TNS_PROJECT_DIR` - the current project directory.
5673
@@ -60,24 +77,29 @@ In-process hooks
6077
----------------
6178
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.
6279
63-
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:
80+
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:
81+
6482
```javascript
65-
module.exports = function($logger) {
83+
export default function($logger) {
6684
};
6785
```
68-
Then, the hook script will be require'd by the CLI and the exported function will be called through the injector.
86+
Then, the hook script will be required by the CLI and the exported function will be called through the injector.
6987
7088
Hooks can take a special injected argument named `hookArgs`:
89+
7190
```javascript
72-
module.exports = function(hookArgs) {
91+
export default function(hookArgs) {
7392
};
7493
```
94+
7595
`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.
7696
7797
If you execute asynchronous code in your hook, you need to return a promise, otherwise execution will continue before your hook completes:
98+
7899
```javascript
79-
var mkdirp = require('mkdirp');
80-
module.exports = function($logger) {
100+
import mkdirp from 'mkdirp';
101+
102+
export default function($logger) {
81103
return new Promise(function(resolve, reject) {
82104
mkdirp('somedir', function(err) {
83105
if (err) {

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ function postinstall(pkgdir) {
100100
var hookFileName = generateHookName(pkg, hook);
101101
var hookPath = path.join(hookDir, hookFileName);
102102

103-
var trampoline = util.format('%srequire("%s/%s");', hook.inject ? 'module.exports = ' : '', pkg.name, hook.script);
103+
var trampoline = `import hooks from "${pkg.name}/${hook.script}";
104+
105+
export default hooks;`
104106

105107
fs.writeFileSync(hookPath, trampoline + os.EOL);
106108
});

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"url": "https://github.com/NativeScript/nativescript-hook.git"
1414
},
1515
"dependencies": {
16-
"glob": "^7.1.0",
17-
"mkdirp": "^1.0.4"
16+
"glob": "^11.0.0",
17+
"mkdirp": "^3.0.1"
1818
}
1919
}

0 commit comments

Comments
 (0)