UNPKG

3.48 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6const chalk_1 = __importDefault(require("chalk"));
7const ci_info_1 = require("ci-info");
8const path_1 = __importDefault(require("path"));
9const pkg_dir_1 = __importDefault(require("pkg-dir"));
10const which_pm_runs_1 = __importDefault(require("which-pm-runs"));
11const checkGitDirEnv_1 = require("../checkGitDirEnv");
12const debug_1 = require("../debug");
13const _1 = require("./");
14const gitRevParse_1 = require("./gitRevParse");
15const checkGitVersion_1 = require("./checkGitVersion");
16// Skip install if HUSKY_SKIP_INSTALL is true
17function checkSkipInstallEnv() {
18 if (['1', 'true'].includes(process.env.HUSKY_SKIP_INSTALL || '')) {
19 console.log('HUSKY_SKIP_INSTALL is set to true,', 'skipping Git hooks installation.');
20 process.exit(0);
21 }
22}
23function getDirs(cwd) {
24 const { prefix, gitCommonDir } = gitRevParse_1.gitRevParse(cwd);
25 debug_1.debug('Git rev-parse command returned:');
26 debug_1.debug(` --git-common-dir: ${gitCommonDir}`);
27 debug_1.debug(` --show-prefix: ${prefix}`);
28 const absoluteGitCommonDir = path_1.default.resolve(cwd, gitCommonDir);
29 // Prefix can be an empty string
30 const relativeUserPkgDir = prefix || '.';
31 return { relativeUserPkgDir, absoluteGitCommonDir };
32}
33// Get INIT_CWD env variable
34function getInitCwdEnv() {
35 const { INIT_CWD } = process.env;
36 if (INIT_CWD === undefined) {
37 const { name, version } = which_pm_runs_1.default();
38 throw new Error(`INIT_CWD is not set, please upgrade your package manager (${name} ${version})`);
39 }
40 debug_1.debug(`INIT_CWD is set to ${INIT_CWD}`);
41 return INIT_CWD;
42}
43function getUserPkgDir(dir) {
44 const userPkgDir = pkg_dir_1.default.sync(dir);
45 if (userPkgDir === undefined) {
46 throw new Error([
47 `Can't find package.json in ${dir} directory or parents`,
48 'Please check that your project has a package.json or create one and reinstall husky.'
49 ].join('\n'));
50 }
51 return userPkgDir;
52}
53function run() {
54 const action = process.argv[2];
55 try {
56 console.log('husky > %s git hooks', action === 'install' ? 'Setting up' : 'Uninstalling');
57 debug_1.debug(`Current working directory is ${process.cwd()}`);
58 if (action === 'install') {
59 checkSkipInstallEnv();
60 checkGitVersion_1.checkGitVersion();
61 }
62 const INIT_CWD = getInitCwdEnv();
63 const userPkgDir = getUserPkgDir(INIT_CWD);
64 checkGitDirEnv_1.checkGitDirEnv();
65 const { absoluteGitCommonDir, relativeUserPkgDir } = getDirs(userPkgDir);
66 if (action === 'install') {
67 const { name: pmName } = which_pm_runs_1.default();
68 debug_1.debug(`Package manager: ${pmName}`);
69 _1.install({
70 absoluteGitCommonDir,
71 relativeUserPkgDir,
72 userPkgDir,
73 pmName,
74 isCI: ci_info_1.isCI
75 });
76 }
77 else {
78 _1.uninstall({ absoluteGitCommonDir, userPkgDir });
79 }
80 console.log(`husky > Done`);
81 }
82 catch (err) {
83 console.log(chalk_1.default.red(err.message.trim()));
84 debug_1.debug(err.stack);
85 console.log(chalk_1.default.red(`husky > Failed to ${action}`));
86 }
87}
88run();