feat: adding linter for commits
This commit is contained in:
10
node_modules/husky/lib/checkGitDirEnv.js
generated
vendored
Normal file
10
node_modules/husky/lib/checkGitDirEnv.js
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const debug_1 = require("./debug");
|
||||
function checkGitDirEnv() {
|
||||
if (process.env.GIT_DIR) {
|
||||
debug_1.debug(`GIT_DIR environment variable is set to ${process.env.GIT_DIR}`);
|
||||
debug_1.debug(`If you're getting "fatal: not a git repository" errors, check GIT_DIR value`);
|
||||
}
|
||||
}
|
||||
exports.checkGitDirEnv = checkGitDirEnv;
|
||||
8
node_modules/husky/lib/debug.js
generated
vendored
Normal file
8
node_modules/husky/lib/debug.js
generated
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
function debug(...args) {
|
||||
if (['1', 'true'].includes(process.env.HUSKY_DEBUG || '')) {
|
||||
console.log('husky:debug', ...args);
|
||||
}
|
||||
}
|
||||
exports.debug = debug;
|
||||
12
node_modules/husky/lib/getConf.js
generated
vendored
Normal file
12
node_modules/husky/lib/getConf.js
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const cosmiconfig_1 = require("cosmiconfig");
|
||||
function getConf(dir) {
|
||||
const explorer = cosmiconfig_1.cosmiconfigSync('husky');
|
||||
const { config = {} } = explorer.search(dir) || {};
|
||||
const defaults = {
|
||||
skipCI: true,
|
||||
};
|
||||
return Object.assign(Object.assign({}, defaults), config);
|
||||
}
|
||||
exports.getConf = getConf;
|
||||
93
node_modules/husky/lib/installer/bin.js
generated
vendored
Normal file
93
node_modules/husky/lib/installer/bin.js
generated
vendored
Normal file
@@ -0,0 +1,93 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const chalk_1 = __importDefault(require("chalk"));
|
||||
const ci_info_1 = require("ci-info");
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const pkg_dir_1 = __importDefault(require("pkg-dir"));
|
||||
const which_pm_runs_1 = __importDefault(require("which-pm-runs"));
|
||||
const checkGitDirEnv_1 = require("../checkGitDirEnv");
|
||||
const debug_1 = require("../debug");
|
||||
const _1 = require("./");
|
||||
const gitRevParse_1 = require("./gitRevParse");
|
||||
const checkGitVersion_1 = require("./checkGitVersion");
|
||||
// Skip install if HUSKY_SKIP_INSTALL is true
|
||||
function checkSkipInstallEnv() {
|
||||
if (['1', 'true'].includes(process.env.HUSKY_SKIP_INSTALL || '')) {
|
||||
console.log('HUSKY_SKIP_INSTALL is set to true,', 'skipping Git hooks installation.');
|
||||
process.exit(0);
|
||||
}
|
||||
}
|
||||
function getDirs(cwd) {
|
||||
const { prefix, gitCommonDir } = gitRevParse_1.gitRevParse(cwd);
|
||||
debug_1.debug('Git rev-parse command returned:');
|
||||
debug_1.debug(` --git-common-dir: ${gitCommonDir}`);
|
||||
debug_1.debug(` --show-prefix: ${prefix}`);
|
||||
const absoluteGitCommonDir = path_1.default.resolve(cwd, gitCommonDir);
|
||||
// Prefix can be an empty string
|
||||
const relativeUserPkgDir = prefix || '.';
|
||||
return { relativeUserPkgDir, absoluteGitCommonDir };
|
||||
}
|
||||
// Get INIT_CWD env variable
|
||||
function getInitCwdEnv() {
|
||||
const { INIT_CWD } = process.env;
|
||||
if (INIT_CWD === undefined) {
|
||||
const { name, version } = which_pm_runs_1.default();
|
||||
throw new Error(`INIT_CWD is not set, please check that your package manager supports it (${name} ${version})
|
||||
|
||||
Alternatively, you could set it manually:
|
||||
INIT_CWD="$(pwd)" npm install husky --save-dev
|
||||
|
||||
Or upgrade to husky v5`);
|
||||
}
|
||||
debug_1.debug(`INIT_CWD is set to ${INIT_CWD}`);
|
||||
return INIT_CWD;
|
||||
}
|
||||
function getUserPkgDir(dir) {
|
||||
const userPkgDir = pkg_dir_1.default.sync(dir);
|
||||
if (userPkgDir === undefined) {
|
||||
throw new Error([
|
||||
`Can't find package.json in ${dir} directory or parents`,
|
||||
'Please check that your project has a package.json or create one and reinstall husky.',
|
||||
].join('\n'));
|
||||
}
|
||||
return userPkgDir;
|
||||
}
|
||||
function run() {
|
||||
const action = process.argv[2];
|
||||
try {
|
||||
console.log('husky > %s git hooks', action === 'install' ? 'Setting up' : 'Uninstalling');
|
||||
debug_1.debug(`Current working directory is ${process.cwd()}`);
|
||||
if (action === 'install') {
|
||||
checkSkipInstallEnv();
|
||||
checkGitVersion_1.checkGitVersion();
|
||||
}
|
||||
const INIT_CWD = getInitCwdEnv();
|
||||
const userPkgDir = getUserPkgDir(INIT_CWD);
|
||||
checkGitDirEnv_1.checkGitDirEnv();
|
||||
const { absoluteGitCommonDir, relativeUserPkgDir } = getDirs(userPkgDir);
|
||||
if (action === 'install') {
|
||||
const { name: pmName } = which_pm_runs_1.default();
|
||||
debug_1.debug(`Package manager: ${pmName}`);
|
||||
_1.install({
|
||||
absoluteGitCommonDir,
|
||||
relativeUserPkgDir,
|
||||
userPkgDir,
|
||||
pmName,
|
||||
isCI: ci_info_1.isCI,
|
||||
});
|
||||
}
|
||||
else {
|
||||
_1.uninstall({ absoluteGitCommonDir, userPkgDir });
|
||||
}
|
||||
console.log(`husky > Done`);
|
||||
}
|
||||
catch (err) {
|
||||
console.log(chalk_1.default.red(err.message.trim()));
|
||||
debug_1.debug(err.stack);
|
||||
console.log(chalk_1.default.red(`husky > Failed to ${action}`));
|
||||
}
|
||||
}
|
||||
run();
|
||||
19
node_modules/husky/lib/installer/checkGitVersion.js
generated
vendored
Normal file
19
node_modules/husky/lib/installer/checkGitVersion.js
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const cp = require("child_process");
|
||||
const find_versions_1 = __importDefault(require("find-versions"));
|
||||
const compare_versions_1 = __importDefault(require("compare-versions"));
|
||||
function checkGitVersion() {
|
||||
const { status, stderr, stdout } = cp.spawnSync('git', ['--version']);
|
||||
if (status !== 0) {
|
||||
throw new Error(`git --version command failed. Got ${String(stderr)}.`);
|
||||
}
|
||||
const [version] = find_versions_1.default(String(stdout));
|
||||
if (compare_versions_1.default(version, '2.13.0') === -1) {
|
||||
throw new Error(`Husky requires Git >=2.13.0. Got v${version}.`);
|
||||
}
|
||||
}
|
||||
exports.checkGitVersion = checkGitVersion;
|
||||
14
node_modules/husky/lib/installer/getBanner.js
generated
vendored
Normal file
14
node_modules/husky/lib/installer/getBanner.js
generated
vendored
Normal file
@@ -0,0 +1,14 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const path = require("path");
|
||||
const read_pkg_1 = require("../read-pkg");
|
||||
function getBanner() {
|
||||
const pkgHomepage = process.env.npm_package_homepage;
|
||||
const pkgDirectory = process.env.PWD;
|
||||
const { homepage: huskyHomepage, version: huskyVersion } = read_pkg_1.readPkg(path.join(__dirname, '../..'));
|
||||
const createdAt = new Date().toLocaleString();
|
||||
return `# Created by Husky v${huskyVersion} (${huskyHomepage})
|
||||
# At: ${createdAt}
|
||||
# From: ${pkgDirectory} (${pkgHomepage})`;
|
||||
}
|
||||
exports.getBanner = getBanner;
|
||||
23
node_modules/husky/lib/installer/gitRevParse.js
generated
vendored
Normal file
23
node_modules/husky/lib/installer/gitRevParse.js
generated
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const child_process_1 = __importDefault(require("child_process"));
|
||||
const slash_1 = __importDefault(require("slash"));
|
||||
function gitRevParse(cwd = process.cwd()) {
|
||||
// https://github.com/typicode/husky/issues/580
|
||||
// https://github.com/typicode/husky/issues/587
|
||||
const { status, stderr, stdout } = child_process_1.default.spawnSync('git', ['rev-parse', '--show-prefix', '--git-common-dir'], { cwd });
|
||||
if (status !== 0) {
|
||||
throw new Error(`git rev-parse command failed. Got ${String(stderr)}`);
|
||||
}
|
||||
const [prefix, gitCommonDir] = stdout
|
||||
.toString()
|
||||
.split('\n')
|
||||
.map((s) => s.trim())
|
||||
// Normalize for Windows
|
||||
.map(slash_1.default);
|
||||
return { prefix, gitCommonDir };
|
||||
}
|
||||
exports.gitRevParse = gitRevParse;
|
||||
88
node_modules/husky/lib/installer/hooks.js
generated
vendored
Normal file
88
node_modules/husky/lib/installer/hooks.js
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const is_1 = require("./is");
|
||||
const getBanner_1 = require("./getBanner");
|
||||
exports.huskyIdentifier = '# husky';
|
||||
function getHookScript() {
|
||||
return `#!/bin/sh
|
||||
${exports.huskyIdentifier}
|
||||
|
||||
${getBanner_1.getBanner()}
|
||||
|
||||
. "$(dirname "$0")/husky.sh"
|
||||
`;
|
||||
}
|
||||
exports.getHookScript = getHookScript;
|
||||
const hookList = [
|
||||
'applypatch-msg',
|
||||
'pre-applypatch',
|
||||
'post-applypatch',
|
||||
'pre-commit',
|
||||
'pre-merge-commit',
|
||||
'prepare-commit-msg',
|
||||
'commit-msg',
|
||||
'post-commit',
|
||||
'pre-rebase',
|
||||
'post-checkout',
|
||||
'post-merge',
|
||||
'pre-push',
|
||||
'post-update',
|
||||
'push-to-checkout',
|
||||
'pre-auto-gc',
|
||||
'post-rewrite',
|
||||
'sendemail-validate',
|
||||
];
|
||||
function getHooks(gitHooksDir) {
|
||||
return hookList.map((hookName) => path.join(gitHooksDir, hookName));
|
||||
}
|
||||
function writeHook(filename, script) {
|
||||
fs.writeFileSync(filename, script, 'utf-8');
|
||||
fs.chmodSync(filename, 0o0755);
|
||||
}
|
||||
function createHook(filename) {
|
||||
const name = path.basename(filename);
|
||||
const hookScript = getHookScript();
|
||||
// Check if hook exist
|
||||
if (fs.existsSync(filename)) {
|
||||
const hook = fs.readFileSync(filename, 'utf-8');
|
||||
// Migrate
|
||||
if (is_1.isGhooks(hook)) {
|
||||
console.log(`migrating existing ghooks script: ${name}`);
|
||||
return writeHook(filename, hookScript);
|
||||
}
|
||||
// Migrate
|
||||
if (is_1.isPreCommit(hook)) {
|
||||
console.log(`migrating existing pre-commit script: ${name}`);
|
||||
return writeHook(filename, hookScript);
|
||||
}
|
||||
// Update
|
||||
if (is_1.isHusky(hook) || is_1.isYorkie(hook)) {
|
||||
return writeHook(filename, hookScript);
|
||||
}
|
||||
// Skip
|
||||
console.log(`skipping existing user hook: ${name}`);
|
||||
return;
|
||||
}
|
||||
// Create hook if it doesn't exist
|
||||
writeHook(filename, hookScript);
|
||||
}
|
||||
function createHooks(gitHooksDir) {
|
||||
getHooks(gitHooksDir).forEach(createHook);
|
||||
}
|
||||
exports.createHooks = createHooks;
|
||||
function canRemove(filename) {
|
||||
if (fs.existsSync(filename)) {
|
||||
const data = fs.readFileSync(filename, 'utf-8');
|
||||
return is_1.isHusky(data);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
function removeHook(filename) {
|
||||
fs.unlinkSync(filename);
|
||||
}
|
||||
function removeHooks(gitHooksDir) {
|
||||
getHooks(gitHooksDir).filter(canRemove).forEach(removeHook);
|
||||
}
|
||||
exports.removeHooks = removeHooks;
|
||||
56
node_modules/husky/lib/installer/index.js
generated
vendored
Normal file
56
node_modules/husky/lib/installer/index.js
generated
vendored
Normal file
@@ -0,0 +1,56 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const debug_1 = require("../debug");
|
||||
const getConf_1 = require("../getConf");
|
||||
const hooks_1 = require("./hooks");
|
||||
const localScript_1 = require("./localScript");
|
||||
const mainScript_1 = require("./mainScript");
|
||||
// This prevents the case where someone would want to debug a node_module that has
|
||||
// husky as devDependency and run npm install from node_modules directory
|
||||
function isInNodeModules(dir) {
|
||||
return dir.indexOf('node_modules') !== -1;
|
||||
}
|
||||
function getGitHooksDir(gitDir) {
|
||||
return path_1.default.join(gitDir, 'hooks');
|
||||
}
|
||||
function install({ absoluteGitCommonDir, relativeUserPkgDir, userPkgDir, pmName, // package manager name
|
||||
isCI, }) {
|
||||
// Get conf from package.json or .huskyrc
|
||||
const conf = getConf_1.getConf(userPkgDir);
|
||||
// Checks
|
||||
if (isCI && conf.skipCI) {
|
||||
console.log('CI detected, skipping Git hooks installation.');
|
||||
return;
|
||||
}
|
||||
if (isInNodeModules(userPkgDir)) {
|
||||
console.log('Trying to install from node_modules directory, skipping Git hooks installation.');
|
||||
return;
|
||||
}
|
||||
// Create hooks directory if it doesn't exist
|
||||
const gitHooksDir = getGitHooksDir(absoluteGitCommonDir);
|
||||
if (!fs_1.default.existsSync(gitHooksDir)) {
|
||||
fs_1.default.mkdirSync(gitHooksDir);
|
||||
}
|
||||
debug_1.debug(`Installing hooks in ${gitHooksDir}`);
|
||||
hooks_1.createHooks(gitHooksDir);
|
||||
localScript_1.createLocalScript(gitHooksDir, pmName, relativeUserPkgDir);
|
||||
mainScript_1.createMainScript(gitHooksDir);
|
||||
}
|
||||
exports.install = install;
|
||||
function uninstall({ absoluteGitCommonDir, userPkgDir, }) {
|
||||
if (isInNodeModules(userPkgDir)) {
|
||||
console.log('Trying to uninstall from node_modules directory, skipping Git hooks uninstallation.');
|
||||
return;
|
||||
}
|
||||
// Remove hooks
|
||||
const gitHooksDir = getGitHooksDir(absoluteGitCommonDir);
|
||||
hooks_1.removeHooks(gitHooksDir);
|
||||
localScript_1.removeLocalScript(gitHooksDir);
|
||||
mainScript_1.removeMainScript(gitHooksDir);
|
||||
}
|
||||
exports.uninstall = uninstall;
|
||||
24
node_modules/husky/lib/installer/is.js
generated
vendored
Normal file
24
node_modules/husky/lib/installer/is.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const hooks_1 = require("./hooks");
|
||||
function isHusky(data) {
|
||||
// Husky v0.14 and prior used #husky as an identifier.
|
||||
// Just in case some previous hooks weren't correctly uninstalled,
|
||||
// and for a better transition this will allow v0.15+ to uninstall them as well.
|
||||
const previousHuskyIdentifier = '#husky';
|
||||
return (data.indexOf(hooks_1.huskyIdentifier) !== -1 ||
|
||||
data.indexOf(previousHuskyIdentifier) !== -1);
|
||||
}
|
||||
exports.isHusky = isHusky;
|
||||
function isYorkie(data) {
|
||||
return data.indexOf('#yorkie') !== -1;
|
||||
}
|
||||
exports.isYorkie = isYorkie;
|
||||
function isGhooks(data) {
|
||||
return data.indexOf('// Generated by ghooks. Do not edit this file.') !== -1;
|
||||
}
|
||||
exports.isGhooks = isGhooks;
|
||||
function isPreCommit(data) {
|
||||
return data.indexOf('./node_modules/pre-commit/hook') !== -1;
|
||||
}
|
||||
exports.isPreCommit = isPreCommit;
|
||||
24
node_modules/husky/lib/installer/localScript.js
generated
vendored
Normal file
24
node_modules/husky/lib/installer/localScript.js
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const getBanner_1 = require("./getBanner");
|
||||
function getLocalScript(pmName, relativeUserPkgDir) {
|
||||
return `${getBanner_1.getBanner()}
|
||||
|
||||
packageManager=${pmName}
|
||||
cd "${relativeUserPkgDir}"
|
||||
`;
|
||||
}
|
||||
exports.getLocalScript = getLocalScript;
|
||||
function createLocalScript(gitHooksDir, pmName, relativeUserPkgDir) {
|
||||
fs.writeFileSync(path.join(gitHooksDir, 'husky.local.sh'), getLocalScript(pmName, relativeUserPkgDir), 'utf-8');
|
||||
}
|
||||
exports.createLocalScript = createLocalScript;
|
||||
function removeLocalScript(gitHooksDir) {
|
||||
const filename = path.join(gitHooksDir, 'husky.local.sh');
|
||||
if (fs.existsSync(filename)) {
|
||||
fs.unlinkSync(filename);
|
||||
}
|
||||
}
|
||||
exports.removeLocalScript = removeLocalScript;
|
||||
25
node_modules/husky/lib/installer/mainScript.js
generated
vendored
Normal file
25
node_modules/husky/lib/installer/mainScript.js
generated
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const getBanner_1 = require("./getBanner");
|
||||
const read_pkg_1 = require("../read-pkg");
|
||||
function getMainScript() {
|
||||
const pkg = read_pkg_1.readPkg(path.join(__dirname, '../..'));
|
||||
const mainScript = fs
|
||||
.readFileSync(path.join(__dirname, '../../sh/husky.sh'), 'utf-8')
|
||||
.replace('huskyVersion="0.0.0"', `huskyVersion="${pkg.version}"`);
|
||||
return [getBanner_1.getBanner(), '', mainScript].join('\n');
|
||||
}
|
||||
exports.getMainScript = getMainScript;
|
||||
function createMainScript(gitHooksDir) {
|
||||
fs.writeFileSync(path.join(gitHooksDir, 'husky.sh'), getMainScript(), 'utf-8');
|
||||
}
|
||||
exports.createMainScript = createMainScript;
|
||||
function removeMainScript(gitHooksDir) {
|
||||
const filename = path.join(gitHooksDir, 'husky.sh');
|
||||
if (fs.existsSync(filename)) {
|
||||
fs.unlinkSync(filename);
|
||||
}
|
||||
}
|
||||
exports.removeMainScript = removeMainScript;
|
||||
13
node_modules/husky/lib/read-pkg.js
generated
vendored
Normal file
13
node_modules/husky/lib/read-pkg.js
generated
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
function readPkg(dir) {
|
||||
const pkgFile = path_1.default.resolve(dir, 'package.json');
|
||||
const pkgStr = fs_1.default.readFileSync(pkgFile, 'utf-8');
|
||||
return JSON.parse(pkgStr);
|
||||
}
|
||||
exports.readPkg = readPkg;
|
||||
19
node_modules/husky/lib/runner/bin.js
generated
vendored
Normal file
19
node_modules/husky/lib/runner/bin.js
generated
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const checkGitDirEnv_1 = require("../checkGitDirEnv");
|
||||
const _1 = __importDefault(require("./"));
|
||||
async function run() {
|
||||
checkGitDirEnv_1.checkGitDirEnv();
|
||||
try {
|
||||
const status = await _1.default(process.argv);
|
||||
process.exit(status);
|
||||
}
|
||||
catch (err) {
|
||||
console.log('Husky > unexpected error', err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
run();
|
||||
87
node_modules/husky/lib/runner/index.js
generated
vendored
Normal file
87
node_modules/husky/lib/runner/index.js
generated
vendored
Normal file
@@ -0,0 +1,87 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const chalk_1 = __importDefault(require("chalk"));
|
||||
const child_process_1 = require("child_process");
|
||||
const getConf_1 = require("../getConf");
|
||||
const read_pkg_1 = require("../read-pkg");
|
||||
// Husky <1.0.0 (commands were defined in pkg.scripts)
|
||||
function getOldCommand(cwd, hookName) {
|
||||
// In some cases, package.json may not exist
|
||||
// For example, when switching to gh-page branch
|
||||
let pkg = {};
|
||||
try {
|
||||
pkg = read_pkg_1.readPkg(cwd);
|
||||
}
|
||||
catch (err) {
|
||||
if (err.code !== 'ENOENT') {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
return pkg && pkg.scripts && pkg.scripts[hookName.replace('-', '')];
|
||||
}
|
||||
// Husky >= 1.0.0
|
||||
function getCommand(cwd, hookName) {
|
||||
const config = getConf_1.getConf(cwd);
|
||||
return config && config.hooks && config.hooks[hookName];
|
||||
}
|
||||
function runCommand(cwd, hookName, cmd, env) {
|
||||
console.log(`husky > ${hookName} (node ${process.version})`);
|
||||
const { status } = child_process_1.spawnSync('sh', ['-c', cmd], {
|
||||
cwd,
|
||||
env: Object.assign(Object.assign({}, process.env), env),
|
||||
stdio: 'inherit',
|
||||
});
|
||||
if (status !== 0) {
|
||||
const noVerifyMessage = [
|
||||
'commit-msg',
|
||||
'pre-commit',
|
||||
'pre-rebase',
|
||||
'pre-push',
|
||||
].includes(hookName)
|
||||
? '(add --no-verify to bypass)'
|
||||
: '(cannot be bypassed with --no-verify due to Git specs)';
|
||||
console.log(`husky > ${hookName} hook failed ${noVerifyMessage}`);
|
||||
}
|
||||
// If shell exits with 127 it means that some command was not found.
|
||||
// However, if husky has been deleted from node_modules, it'll be a 127 too.
|
||||
// To be able to distinguish between both cases, 127 is changed to 1.
|
||||
if (status === 127) {
|
||||
return 1;
|
||||
}
|
||||
return status || 0;
|
||||
}
|
||||
/**
|
||||
* @param {array} argv process.argv
|
||||
* @param {string} options.cwd cwd
|
||||
* @param {promise} options.getStdinFn - used for mocking only
|
||||
*/
|
||||
async function run([, , hookName = '', ...HUSKY_GIT_PARAMS], { cwd = process.cwd() } = {}) {
|
||||
const oldCommand = getOldCommand(cwd, hookName);
|
||||
const command = getCommand(cwd, hookName);
|
||||
// Add HUSKY_GIT_PARAMS to env
|
||||
const env = {};
|
||||
if (HUSKY_GIT_PARAMS === null || HUSKY_GIT_PARAMS === void 0 ? void 0 : HUSKY_GIT_PARAMS.length) {
|
||||
env.HUSKY_GIT_PARAMS = HUSKY_GIT_PARAMS.join(' ');
|
||||
}
|
||||
if (command) {
|
||||
return runCommand(cwd, hookName, command, env);
|
||||
}
|
||||
if (oldCommand) {
|
||||
console.log(chalk_1.default.red(`
|
||||
Warning: Setting ${hookName} script in package.json > scripts will be deprecated.
|
||||
Please move it to husky.hooks in package.json or .huskyrc file.
|
||||
|
||||
For an automatic update you can also run:
|
||||
npx --no-install husky-upgrade
|
||||
yarn husky-upgrade
|
||||
|
||||
See https://github.com/typicode/husky for more information.
|
||||
`));
|
||||
return runCommand(cwd, hookName, oldCommand, env);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
exports.default = run;
|
||||
8
node_modules/husky/lib/upgrader/bin.js
generated
vendored
Executable file
8
node_modules/husky/lib/upgrader/bin.js
generated
vendored
Executable file
@@ -0,0 +1,8 @@
|
||||
#!/usr/bin/env node
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const index_1 = __importDefault(require("./index"));
|
||||
index_1.default(process.cwd());
|
||||
65
node_modules/husky/lib/upgrader/index.js
generated
vendored
Normal file
65
node_modules/husky/lib/upgrader/index.js
generated
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
"use strict";
|
||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
const fs_1 = __importDefault(require("fs"));
|
||||
const path_1 = __importDefault(require("path"));
|
||||
const read_pkg_1 = require("../read-pkg");
|
||||
const hookList = {
|
||||
applypatchmsg: 'applypatch-msg',
|
||||
commitmsg: 'commit-msg',
|
||||
postapplypatch: 'post-applypatch',
|
||||
postcheckout: 'post-checkout',
|
||||
postcommit: 'post-commit',
|
||||
postmerge: 'post-merge',
|
||||
postreceive: 'post-receive',
|
||||
postrewrite: 'post-rewrite',
|
||||
postupdate: 'post-update',
|
||||
preapplypatch: 'pre-applypatch',
|
||||
preautogc: 'pre-auto-gc',
|
||||
precommit: 'pre-commit',
|
||||
premergecommit: 'pre-merge-commit',
|
||||
preparecommitmsg: 'prepare-commit-msg',
|
||||
prepush: 'pre-push',
|
||||
prerebase: 'pre-rebase',
|
||||
prereceive: 'pre-receive',
|
||||
pushtocheckout: 'push-to-checkout',
|
||||
sendemailvalidate: 'sendemail-validate',
|
||||
update: 'update',
|
||||
};
|
||||
function upgrade(cwd) {
|
||||
const pkgFile = path_1.default.join(cwd, 'package.json');
|
||||
if (fs_1.default.existsSync(pkgFile)) {
|
||||
const pkg = read_pkg_1.readPkg(cwd);
|
||||
console.log(`husky > upgrading ${pkgFile}`);
|
||||
// Don't overwrite 'husky' field if it exists
|
||||
if (pkg.husky) {
|
||||
return console.log(`husky field in package.json isn't empty, skipping automatic upgrade`);
|
||||
}
|
||||
const hooks = {};
|
||||
// Find hooks in package.json 'scripts' field
|
||||
Object.keys(hookList).forEach((name) => {
|
||||
if (pkg.scripts) {
|
||||
const script = pkg.scripts[name];
|
||||
if (script) {
|
||||
delete pkg.scripts[name];
|
||||
const newName = hookList[name];
|
||||
hooks[newName] = script.replace(/\bGIT_PARAMS\b/g, 'HUSKY_GIT_PARAMS');
|
||||
console.log(`moved scripts.${name} to husky.hooks.${newName}`);
|
||||
}
|
||||
}
|
||||
});
|
||||
// Move found hooks to 'husky.hooks' field
|
||||
if (Object.keys(hooks).length) {
|
||||
pkg.husky = { hooks };
|
||||
}
|
||||
else {
|
||||
console.log('no hooks found');
|
||||
}
|
||||
// Update package.json
|
||||
fs_1.default.writeFileSync(pkgFile, `${JSON.stringify(pkg, null, 2)}\n`, 'utf-8');
|
||||
console.log(`husky > done`);
|
||||
}
|
||||
}
|
||||
exports.default = upgrade;
|
||||
Reference in New Issue
Block a user