Socket
Book a DemoInstallSign in
Socket

hook-scripts

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hook-scripts

Add git-style hooks to your node project

npmnpm
Version
1.0.0
Version published
Weekly downloads
0
-100%
Maintainers
1
Weekly downloads
 
Created
Source

hook-scripts

Add git-style hooks to your node module.

build status

Install

npm install hook-scripts

Example usage

var hooks = require('hook-scripts')();

hooks('hook-name', function (hook) {
  hook.on('close', function (code) {
    console.log('Script exited with code', code);
  });
  hook.stdout.pipe(process.stdout);
  hook.stderr.pipe(process.stderr);
});

API

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) {...});

Initialize

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 scripts
  • cwd - The working directory in which to run the hook scripts (defaults to process.cwd())
  • overrides - An optional hash of hook script overrides (see the Advanced section below)

Example hooks

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'

Advanced

Overrides

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
});

License

MIT

Keywords

hook

FAQs

Package last updated on 02 Dec 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts