UNPKG

husky

Version:

Prevents bad commit or push (git hooks, pre-commit/precommit, pre-push/prepush, post-merge/postmerge and all that stuff...)

64 lines (63 loc) 3.09 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const execa = require("execa"); const getStdin = require("get-stdin"); const path = require("path"); const readPkg = require("read-pkg"); const getConf_1 = require("../getConf"); /** * @param argv - process.argv */ function default_1([, scriptPath, hookName = '', HUSKY_GIT_PARAMS], getStdinFn = getStdin // Used for mocking ) { return __awaiter(this, void 0, void 0, function* () { const cwd = path.resolve(scriptPath.split('node_modules')[0]); const pkg = readPkg.sync(cwd, { normalize: false }); const config = getConf_1.default(cwd); const command = config && config.hooks && config.hooks[hookName]; const oldCommand = pkg && pkg.scripts && pkg.scripts[hookName.replace('-', '')]; try { const env = {}; if (HUSKY_GIT_PARAMS) { env.HUSKY_GIT_PARAMS = HUSKY_GIT_PARAMS; } if (['pre-push', 'pre-receive', 'post-receive', 'post-rewrite'].includes(hookName)) { env.HUSKY_GIT_STDIN = yield getStdinFn(); } if (command) { console.log(`husky > ${hookName} (node ${process.version})`); execa.shellSync(command, { cwd, env, stdio: 'inherit' }); return 0; } if (oldCommand) { console.log(); console.log(`Warning: Setting ${hookName} script in package.json > scripts will be deprecated in v1.0`); console.log(`Please move it to husky.hooks in package.json, a .huskyrc file, or a husky.config.js file`); console.log(`Or run ./node_modules/.bin/husky-upgrade for automatic update`); console.log(); console.log(`See https://github.com/typicode/husky for usage`); console.log(); console.log(`husky > ${hookName} (node ${process.version})`); execa.shellSync(oldCommand, { cwd, env, stdio: 'inherit' }); return 0; } return 0; } catch (err) { const noVerifyMessage = hookName === 'prepare-commit-msg' ? '(cannot be bypassed with --no-verify due to Git specs)' : '(add --no-verify to bypass)'; console.log(`husky > ${hookName} hook failed ${noVerifyMessage}`); return err.code; } }); } exports.default = default_1;