bin.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const chalk_1 = __importDefault(require("chalk"));
  7. const ci_info_1 = require("ci-info");
  8. const path_1 = __importDefault(require("path"));
  9. const pkg_dir_1 = __importDefault(require("pkg-dir"));
  10. const which_pm_runs_1 = __importDefault(require("which-pm-runs"));
  11. const checkGitDirEnv_1 = require("../checkGitDirEnv");
  12. const debug_1 = require("../debug");
  13. const _1 = require("./");
  14. const gitRevParse_1 = require("./gitRevParse");
  15. const checkGitVersion_1 = require("./checkGitVersion");
  16. // Skip install if HUSKY_SKIP_INSTALL is true
  17. function 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. }
  23. function 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
  34. function 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 check that your package manager supports it (${name} ${version})
  39. Alternatively, you could set it manually:
  40. INIT_CWD="$(pwd)" npm install husky --save-dev
  41. Or upgrade to husky v5`);
  42. }
  43. debug_1.debug(`INIT_CWD is set to ${INIT_CWD}`);
  44. return INIT_CWD;
  45. }
  46. function getUserPkgDir(dir) {
  47. const userPkgDir = pkg_dir_1.default.sync(dir);
  48. if (userPkgDir === undefined) {
  49. throw new Error([
  50. `Can't find package.json in ${dir} directory or parents`,
  51. 'Please check that your project has a package.json or create one and reinstall husky.',
  52. ].join('\n'));
  53. }
  54. return userPkgDir;
  55. }
  56. function run() {
  57. const action = process.argv[2];
  58. try {
  59. console.log('husky > %s git hooks', action === 'install' ? 'Setting up' : 'Uninstalling');
  60. debug_1.debug(`Current working directory is ${process.cwd()}`);
  61. if (action === 'install') {
  62. checkSkipInstallEnv();
  63. checkGitVersion_1.checkGitVersion();
  64. }
  65. const INIT_CWD = getInitCwdEnv();
  66. const userPkgDir = getUserPkgDir(INIT_CWD);
  67. checkGitDirEnv_1.checkGitDirEnv();
  68. const { absoluteGitCommonDir, relativeUserPkgDir } = getDirs(userPkgDir);
  69. if (action === 'install') {
  70. const { name: pmName } = which_pm_runs_1.default();
  71. debug_1.debug(`Package manager: ${pmName}`);
  72. _1.install({
  73. absoluteGitCommonDir,
  74. relativeUserPkgDir,
  75. userPkgDir,
  76. pmName,
  77. isCI: ci_info_1.isCI,
  78. });
  79. }
  80. else {
  81. _1.uninstall({ absoluteGitCommonDir, userPkgDir });
  82. }
  83. console.log(`husky > Done`);
  84. }
  85. catch (err) {
  86. console.log(chalk_1.default.red(err.message.trim()));
  87. debug_1.debug(err.stack);
  88. console.log(chalk_1.default.red(`husky > Failed to ${action}`));
  89. }
  90. }
  91. run();