Add git-style hooks to your node project.
npm install hook-scripts
var hooks = require('hook-scripts')();
hooks('hook-name', function (hook) {
if (!hook) return console.log('No hook named hook-name found');
hook.on('close', function (code) {
console.log('Script exited with code', code);
});
hook.stdout.pipe(process.stdout);
hook.stderr.pipe(process.stderr);
});
By default, hook-scripts will look for a hooks
folder in the current
working directory (process.cwd()
). You can override this behavior by
parsing in a path to a different directory upon initialization:
var hooks = require('hook-scripts')('/path/to/hooks');
It's expected that the hook names map to filenames inside the hooks directory.
You can parse in arguments to the hook script using an optional 2nd argument when registering the hook:
hooks('hook-name', ['arg1', 'arg2'], function (hook) {...});
And you can even set custom environment variables:
hooks('hook-name', [], { env: { NODE_ENV: 'production' } }, function (hook) {...});
An initializer is returned when requireing hook-scripts
. It takes one
optional argument which can either be a string or an options hash.
require('hook-scripts')([dir || options]);
The string is just a shorthand for parsing in { dir: string }
.
The full list of options are:
dir
- The directory in which to look for the hook scriptscwd
- The working directory in which to run the hook scripts (defaults toprocess.cwd()
)env
- Environment key-value pairsoverrides
- An optional hash of hook script overrides (see the Advanced section below)
The hooks can be written in any language that you prefer. Just make sure they are executable and include a hashbang in the top of the script.
Here are some examples in various languages:
Bash
#!/usr/bin/env bash
echo 'Hello World'
Node.js
#!/usr/bin/env node
console.log('Hello World');
Ruby
#!/usr/bin/env ruby
puts 'Hello World'
Using the initialization option overrides
it's possible to override
individual hooks:
var hooks = require('hook-scripts')({ overrides: { foo: 'echo bar' } });
// will run `echo bar` instead of looking for a script named foo
hooks('foo', function (hook) {
hook.stdout.pipe(process.stdout); // pipes `bar\n` to STDOUT
});
MIT