An open API service indexing awesome lists of open source software.

https://github.com/yamadayuki/ogh

Open Git Hooks
https://github.com/yamadayuki/ogh

git githook monorepo

Last synced: 5 months ago
JSON representation

Open Git Hooks

Awesome Lists containing this project

README

          

# ogh (Open Git Hooks)

This package enables to treat the git hooks and your script can be invoked via hooks.

## Installation

```sh
$ npm install --save @yamadayuki/ogh
# or
$ yarn add @yamadayuki/ogh
```

## Usage

Create your script file to perform your script for the package users. This sample means the users commit and then `pre-commit` hook is invoked, the script prints the `Hello world` string in the users console. This script holds the githooks installer and uninstaller. `entrypoint` function's first parameter is your building package name.

```js
// index.js
const { entrypoint, extractHookFromArgs } = require("@yamadayuki/ogh");

const perform = (args, config) => {
if (extractHookFromArgs(args) === "pre-commit") {
console.log("Hello world!");
}
};

entrypoint("foo", { scriptPath: "index.js", hooks: ["pre-commit"] })
.registerPerformHook(perform)
.parse(process.argv);
```

And register your `postinstall` / `preuninstall` npm hook in your package's `package.json`.

```json
{
"name": "foo",
...,
"scripts": [
...,
"postinstall": "node index.js install",
"preuninstall": "node index.js uninstall"
]
}
```

And then your package users have the `.git/hooks/pre-commit` file such as below.

```sh
#!/bin/sh
# DO NOT EDIT foo START

scriptPath="node_modules/foo/index.js"
hookName="pre-commit"
gitParams="$*"

if ! command -v node >/dev/null 2>&1; then
echo "Can't find node in PATH, trying to find a node binary on your system"
fi
if [ -f $scriptPath ]; then
node $scriptPath $hookName "$gitParams"
fi

# DO NOT EDIT foo END
```

A sample package is in the [@yamadayuki/ogh-sample](https://github.com/yamadayuki/ogh/tree/master/packages/ogh-sample) directory.

## API

`@yamadayuki/ogh` has 3 functions.

### `entrypoint(packageName, options)`

This function returns the `Ogh` class instance.

`packageName` indicates the name of your building package. It is required. This is used in constructing the content of the githook.

`options` has some optional parameters.

- `options.scriptName` indicates the script path which is invoked via git hooks. It is relative path from the package root. Default value is `lib/index.js`.
- `options.hooks` indicates the hooks which are installed. If you want to specify the hooks only `pre-commit` and `pre-push`, you should pass the array as `["pre-commit", "pre-push"]`. Default value is array of all githooks. See https://git-scm.com/docs/githooks.

### `extractHookFromArgs(args)`

It can retrieve the githook name from `args`. This `args` is `process.argv`.

```js
const perform = (args, config) => {
if (extractHookFromArgs(args) === "pre-commit") {
console.log("Hello world!");
}
};
```

### `extractGitRootDirFromArgs(args)`

It can retrieve the installed project root which has the `.git` directory. This `args` is `process.argv`.

```js
const perform = (args, config) => {
console.log(extractGitRootDirFromArgs(args)); // => /path/to/project/root
};
```