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
- Host: GitHub
- URL: https://github.com/yamadayuki/ogh
- Owner: yamadayuki
- License: mit
- Created: 2019-02-13T13:13:37.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-01-15T15:04:14.000Z (over 3 years ago)
- Last Synced: 2025-10-04T08:18:35.776Z (9 months ago)
- Topics: git, githook, monorepo
- Language: TypeScript
- Size: 2.3 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 25
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
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
};
```