Socket
Socket
Sign inDemoInstall

husky

Package Overview
Dependencies
Maintainers
1
Versions
214
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

husky - npm Package Compare versions

Comparing version 4.0.0-beta.1 to 4.0.0-beta.2

20

lib/installer/bin.js

@@ -14,3 +14,4 @@ "use strict";

// Debug
debug_1.default(`cwd: ${process.cwd()}`);
debug_1.default(`Current working directory is '${process.cwd()}'`);
debug_1.default(`INIT_CWD environment variable is set to '${process.env.INIT_CWD}'`);
// Action can be "install" or "uninstall"

@@ -23,7 +24,17 @@ // huskyDir is ONLY used in dev, don't use this arguments

console.log('husky > %s git hooks', action === 'install' ? 'Setting up' : 'Uninstalling');
// Skip install if HUSKY_SKIP_INSTALL=1
if (action === 'install' &&
['1', 'true'].includes(process.env.HUSKY_SKIP_INSTALL || '')) {
console.log("HUSKY_SKIP_INSTALL environment variable is set to 'true',", 'skipping Git hooks installation.');
process.exit(0);
}
if (process.env.GIT_DIR) {
debug_1.default(`GIT_DIR environment variable is set to '${process.env.GIT_DIR}'.`);
debug_1.default(`Unless it's on purpose, you may want to unset GIT_DIR as it will affect where Git hooks are going to be installed.`);
}
// Get top level and git dir
const { topLevel, absoluteGitDir } = gitRevParse_1.default();
// Debug
debug_1.default(`topLevel: ${topLevel}`);
debug_1.default(`gitDir: ${absoluteGitDir}`);
debug_1.default('Git rev-parse command returned:');
debug_1.default(` topLevel: ${topLevel}`);
debug_1.default(` absoluteGitDir: ${absoluteGitDir}`);
// Install or uninstall

@@ -38,2 +49,3 @@ if (action === 'install') {

}
console.log(`husky > Done`);
}

@@ -40,0 +52,0 @@ catch (error) {

8

lib/installer/index.js

@@ -12,2 +12,3 @@ "use strict";

const is_1 = require("./is");
const debug_1 = __importDefault(require("../debug"));
const hookList = [

@@ -118,6 +119,2 @@ 'applypatch-msg',

// Checks
if (['1', 'true'].includes(process.env.HUSKY_SKIP_INSTALL || '')) {
console.log("HUSKY_SKIP_INSTALL environment variable is set to 'true',", 'skipping Git hooks installation.');
return;
}
if (isCI && conf.skipCI) {

@@ -136,2 +133,3 @@ console.log('CI detected, skipping Git hooks installation.');

}
debug_1.default(`Installing hooks in '${gitHooksDir}'`);
const hooks = getHooks(gitDir);

@@ -142,3 +140,2 @@ // Path.relative can return '' if both paths are the same, so '.' is used as a default value

createHooks(hooks, script);
console.log(`husky > Done`);
}

@@ -158,4 +155,3 @@ exports.install = install;

removeHooks(hooks);
console.log('husky > Done');
}
exports.uninstall = uninstall;

@@ -9,3 +9,3 @@ "use strict";

// Debug
debug_1.default(`cwd: ${process.cwd()}`);
debug_1.default(`Current working directory is '${process.cwd()}'`);
// Run hook

@@ -12,0 +12,0 @@ _1.default(process.argv)

@@ -29,3 +29,3 @@ "use strict";

// Debug
debug_1.default(`cwd: ${cwd} (updated)`);
debug_1.default(`Changed current working directory to '${cwd}'`);
// In some cases, package.json may not exist

@@ -32,0 +32,0 @@ // For example, when switching to gh-page branch

{
"name": "husky",
"version": "4.0.0-beta.1",
"version": "4.0.0-beta.2",
"description": "Prevents bad commit or push (git hooks, pre-commit/precommit, pre-push/prepush, post-merge/postmerge and all that stuff...)",

@@ -15,3 +15,2 @@ "bin": {

"install": "node husky install",
"postinstall": "opencollective-postinstall || true",
"preuninstall": "node husky uninstall",

@@ -24,3 +23,5 @@ "build": "del-cli lib && tsc",

"lint": "eslint . --ext .js,.ts --ignore-path .gitignore",
"fix": "npm run lint -- --fix"
"fix": "npm run lint -- --fix",
"doc": "markdown-toc -i README.md",
"postinstall": "opencollective-postinstall || exit 0"
},

@@ -82,2 +83,3 @@ "repository": {

"jest": "^24.8.0",
"markdown-toc": "^1.2.0",
"mkdirp": "^0.5.1",

@@ -84,0 +86,0 @@ "pinst": "^1.1.1",

@@ -1,7 +0,5 @@

---
__Announcement__ you can support the development of v4 major release on [__Open Collective__](https://opencollective.com/husky) and [__GitHub Sponsors__](https://github.com/users/typicode/sponsorship) 🚀
# Announcement
See [next](https://github.com/typicode/husky/tree/next) branch. Feedback is welcome 🙏
__Next release__ you can support the development of v4 major release on [Open Collective](https://opencollective.com/husky) and [GitHub Sponsors](https://github.com/users/typicode/sponsorship) 🎉
---

@@ -40,3 +38,3 @@

To go further, see the docs [here](https://github.com/typicode/husky/blob/master/DOCS.md).
_Existing hooks are kept. Requires Node `>= 8.6.0` and Git `>= 2.13.2`._

@@ -49,3 +47,3 @@ ## Uninstall

Git hooks installed by husky will be removed.
_Git hooks installed by husky will be removed._

@@ -56,29 +54,88 @@ ## Financial Contributors

### Organizations
Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/husky/contribute)]
<!-- oc -->
#### Gold Sponsors
<a href="https://opencollective.com/husky/tiers/gold-sponsor/0/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/0/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/1/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/1/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/2/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/2/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/3/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/3/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/4/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/4/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/5/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/5/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/6/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/6/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/7/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/7/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/8/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/8/avatar.svg" height="60px"></a>
<a href="https://opencollective.com/husky/tiers/gold-sponsor/9/website"><img src="https://opencollective.com/husky/tiers/gold-sponsor/9/avatar.svg" height="60px"></a>
#### Silver Sponsors
<a href="https://opencollective.com/husky/tiers/silver-sponsor/0/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/0/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/1/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/1/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/2/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/2/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/3/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/3/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/4/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/4/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/5/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/5/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/6/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/6/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/7/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/7/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/8/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/8/avatar.svg" height="45px"></a>
<a href="https://opencollective.com/husky/tiers/silver-sponsor/9/website"><img src="https://opencollective.com/husky/tiers/silver-sponsor/9/avatar.svg" height="45px"></a>
#### Bronze Sponsors
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/0/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/0/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/1/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/1/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/2/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/2/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/3/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/3/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/4/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/4/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/5/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/5/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/6/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/6/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/7/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/7/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/8/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/8/avatar.svg" height="30px"></a>
<a href="https://opencollective.com/husky/tiers/bronze-sponsor/9/website"><img src="https://opencollective.com/husky/tiers/bronze-sponsor/9/avatar.svg" height="30px"></a>
<!-- oc-end -->
### Individuals
<a href="https://opencollective.com/husky"><img src="https://opencollective.com/husky/individuals.svg?width=890"></a>
<a href="https://opencollective.com/husky"><img src="https://opencollective.com/husky/individuals.svg?width=890" height="60px"></a>
### Organizations
## Used by
Support this project with your organization. Your logo will show up here with a link to your website. [[Contribute](https://opencollective.com/husky/contribute)]
* [webpack](https://github.com/webpack/webpack)
* [babel](https://github.com/babel/babel)
* [create-react-app](https://github.com/facebookincubator/create-react-app)
* ... and many other awesome projects
<a href="https://opencollective.com/husky/organization/0/website"><img src="https://opencollective.com/husky/organization/0/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/1/website"><img src="https://opencollective.com/husky/organization/1/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/2/website"><img src="https://opencollective.com/husky/organization/2/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/3/website"><img src="https://opencollective.com/husky/organization/3/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/4/website"><img src="https://opencollective.com/husky/organization/4/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/5/website"><img src="https://opencollective.com/husky/organization/5/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/6/website"><img src="https://opencollective.com/husky/organization/6/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/7/website"><img src="https://opencollective.com/husky/organization/7/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/8/website"><img src="https://opencollective.com/husky/organization/8/avatar.svg"></a>
<a href="https://opencollective.com/husky/organization/9/website"><img src="https://opencollective.com/husky/organization/9/avatar.svg"></a>
## Guides
## Requirements
<!-- toc -->
Husky requires Node `>= 8.6.0` and Git `>= 2.13.2`
* [Upgrading from 0.14](#upgrading-from-014)
* [Supported hooks](#supported-hooks)
* [Access Git params and stdin](#access-git-params-and-stdin)
* [Skip all hooks (rebase)](#skip-all-hooks-rebase)
* [Disable auto-install](#disable-auto-install)
* [CI servers](#ci-servers)
* [Monorepos](#monorepos)
* [Node version managers](#node-version-managers)
* [Local commands (~/.huskyrc)](#local-commands-huskyrc)
* [Multiple commands](#multiple-commands)
* [Troubleshoot](#troubleshoot)
+ [Debug messages](#debug-messages)
+ [Hooks aren't running](#hooks-arent-running)
+ [Commits aren't blocked](#commits-arent-blocked)
+ [Commits are slow](#commits-are-slow)
+ [Testing husky in a new repo](#testing-husky-in-a-new-repo)
## Upgrading from 0.14
<!-- tocstop -->
Simply move your existing hooks to `husky.hooks` field and use raw Git hooks names. Also, if you're using the `GIT_PARAMS` env variable, rename it to `HUSKY_GIT_PARAMS`.
### Upgrading from 0.14
Run `husky-upgrade` to automatically upgrade your configuration:
```
npx --no-install husky-upgrade
```
You can also do it manually. Move your existing hooks to `husky.hooks` field and use raw Git hooks names. Also, if you were using `GIT_PARAMS` env variable, rename it to `HUSKY_GIT_PARAMS`.
```diff

@@ -99,15 +156,76 @@ {

Alternatively, you can run the following command which will do the same automatically for you ;)
Starting with `1.0.0`, husky can be configured using `.huskyrc`, `.huskyrc.json` or `.huskyrc.js` file.
```js
// .huskyrc
{
"hooks": {
"pre-commit": "npm test"
}
}
```
./node_modules/.bin/husky-upgrade
### Supported hooks
Husky supports all Git hooks defined [here](https://git-scm.com/docs/githooks). Server-side hooks (`pre-receive`, `update` and `post-receive`) aren't supported.
### Access Git params and stdin
Git hooks can get parameters via command-line arguments and stdin. Husky makes them accessible via `HUSKY_GIT_PARAMS` and `HUSKY_GIT_STDIN` environment variables.
```
"commit-msg": "echo $HUSKY_GIT_PARAMS"
```
Starting with `1.0.0`, you can also configure hooks using `.huskyrc`, `.huskyrc.json` or `.huskyrc.js` file.
### Skip all hooks (rebase)
During a rebase you may want to skip all hooks, you can use `HUSKY_SKIP_HOOKS` environment variable.
```sh
HUSKY_SKIP_HOOKS=1 git rebase ...
```
### Disable auto-install
If you don't want husky to automatically install Git hooks, simply set `HUSKY_SKIP_INSTALL` environment variable.
```sh
HUSKY_SKIP_INSTALL=1 npm install
```
### CI servers
By default, Husky won't install on CI servers.
### Monorepos
If you have a multi-package repository, it's __recommended__ to use tools like [lerna](https://github.com/lerna/lerna) and have husky installed ONLY in the root `package.json` to act as the source of truth.
Generally speaking, you should AVOID defining husky in multiple `package.json`, as each package would overwrite previous husky installation.
```sh
.
└── root
├── .git
├── package.json 🐶 # Add husky here
└── packages
├── A
│ └── package.json
├── B
│ └── package.json
└── C
└── package.json
```
```js
// .huskyrc
// root/package.json
{
"hooks": {
"pre-commit": "npm test"
"private": true,
"devDependencies": {
"husky": "..."
},
"husky": {
"hooks": {
"pre-commit": "lerna run test"
}
}

@@ -117,22 +235,96 @@ }

To view the full list of changes, please see the [CHANGELOG](https://github.com/typicode/husky/blob/master/CHANGELOG.md).
### Node version managers
## Features
If you're on Windows, husky will simply use the version installed globally on your system.
* Keeps existing user hooks
* Supports GUI Git clients
* Supports all Git hooks (`pre-commit`, `pre-push`, ...)
For macOS and Linux users:
- if you're running `git` commands in the terminal, husky will use the version defined in your shell `PATH`. In other words, if you're a `nvm` user, husky will use the version that you've set with `nvm`.
- if you're using a GUI client and `nvm`, it may have a different `PATH` and not load `nvm`, in this case the highest `node` version installed by `nvm` will usually be picked. You can also check `~/.node_path` to see which version is used by GUIs and edit if you want to use something else.
## Used by
### Local commands (~/.huskyrc)
* [webpack](https://github.com/webpack/webpack)
* [babel](https://github.com/babel/babel)
* [create-react-app](https://github.com/facebookincubator/create-react-app)
* [Next.js](https://github.com/zeit/next.js)
* [Hyper](https://github.com/zeit/hyper)
* [Kibana](https://github.com/elastic/kibana)
* [JSON Server](https://github.com/typicode/json-server)
* [Hotel](https://github.com/typicode/hotel)
* ... and many other awesome projects
Husky will source `~/.huskyrc` file if it exists before running hook scripts.
You can use it, for example, to load a node version manager or run some `shell` commands before hooks.
```sh
# ~/.huskyrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
```
### Multiple commands
By design and just like `scripts` defined in `package.json`, husky will run hook scripts as a single command.
```json
"pre-commit": "cmd && cmd"
```
That said, if you prefer to use an array, the recommended approach is to define them in `.huskyrc.js`.
```js
const tasks = arr => arr.join(' && ')
module.exports = {
'hooks': {
'pre-commit': tasks([
'cmd',
'cmd'
])
}
}
```
Tools like [npm-run-all](https://github.com/mysticatea/npm-run-all) can help too.
### Troubleshoot
#### Debug messages
`HUSKY_DEBUG=1` can provide additional informations when running commands.
```
HUSKY_DEBUG=1 npm install husky --save-dev
HUSKY_DEBUG=1 git commit ...
```
#### Hooks aren't running
Check if hooks were installed. Verify that `.git/hooks/pre-commit` exists and have husky code. It should start with:
```sh
#!/bin/sh
# husky...
```
If not, you may have another Git hooks manager defined in your `package.json` overwriting husky's hooks. Check also the output during install, you should see:
```
husky > Setting up git hooks
husky > Done
```
#### Commits aren't blocked
For a commit to be blocked, `pre-commit` script must exit with a non-zero exit code. If you commit isn't blocked, check your script exit code.
#### Commits are slow
Husky is fast and only adds a few tenth of seconds to commits (`~0.3s` on a low-end PC). So it's most probably related to how many things are done during `pre-commit`. You can often improve this by using cache on your tools (babel, eslint, ...) and using [lint-staged](https://github.com/okonet/lint-staged).
#### Testing husky in a new repo
To isolate your issue, you can also create a new repo:
```sh
mkdir foo && cd foo
git init && npm init -y
npm install husky --save-dev
# Add a failing pre-commit hook to your package.json:
# "pre-commit": "echo \"this should fail\" && exit 1"
# Make a commit
```
## See also

@@ -144,3 +336,3 @@

### Patreon
## Patreon

@@ -147,0 +339,0 @@ People and companies supporting via Patreon: [thanks](https://thanks.typicode.com)

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc