index.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 child_process_1 = require("child_process");
  8. const getConf_1 = require("../getConf");
  9. const read_pkg_1 = require("../read-pkg");
  10. // Husky <1.0.0 (commands were defined in pkg.scripts)
  11. function getOldCommand(cwd, hookName) {
  12. // In some cases, package.json may not exist
  13. // For example, when switching to gh-page branch
  14. let pkg = {};
  15. try {
  16. pkg = read_pkg_1.readPkg(cwd);
  17. }
  18. catch (err) {
  19. if (err.code !== 'ENOENT') {
  20. throw err;
  21. }
  22. }
  23. return pkg && pkg.scripts && pkg.scripts[hookName.replace('-', '')];
  24. }
  25. // Husky >= 1.0.0
  26. function getCommand(cwd, hookName) {
  27. const config = getConf_1.getConf(cwd);
  28. return config && config.hooks && config.hooks[hookName];
  29. }
  30. function runCommand(cwd, hookName, cmd, env) {
  31. console.log(`husky > ${hookName} (node ${process.version})`);
  32. const { status } = child_process_1.spawnSync('sh', ['-c', cmd], {
  33. cwd,
  34. env: Object.assign(Object.assign({}, process.env), env),
  35. stdio: 'inherit',
  36. });
  37. if (status !== 0) {
  38. const noVerifyMessage = [
  39. 'commit-msg',
  40. 'pre-commit',
  41. 'pre-rebase',
  42. 'pre-push',
  43. ].includes(hookName)
  44. ? '(add --no-verify to bypass)'
  45. : '(cannot be bypassed with --no-verify due to Git specs)';
  46. console.log(`husky > ${hookName} hook failed ${noVerifyMessage}`);
  47. }
  48. // If shell exits with 127 it means that some command was not found.
  49. // However, if husky has been deleted from node_modules, it'll be a 127 too.
  50. // To be able to distinguish between both cases, 127 is changed to 1.
  51. if (status === 127) {
  52. return 1;
  53. }
  54. return status || 0;
  55. }
  56. /**
  57. * @param {array} argv process.argv
  58. * @param {string} options.cwd cwd
  59. * @param {promise} options.getStdinFn - used for mocking only
  60. */
  61. async function run([, , hookName = '', ...HUSKY_GIT_PARAMS], { cwd = process.cwd() } = {}) {
  62. const oldCommand = getOldCommand(cwd, hookName);
  63. const command = getCommand(cwd, hookName);
  64. // Add HUSKY_GIT_PARAMS to env
  65. const env = {};
  66. if (HUSKY_GIT_PARAMS === null || HUSKY_GIT_PARAMS === void 0 ? void 0 : HUSKY_GIT_PARAMS.length) {
  67. env.HUSKY_GIT_PARAMS = HUSKY_GIT_PARAMS.join(' ');
  68. }
  69. if (command) {
  70. return runCommand(cwd, hookName, command, env);
  71. }
  72. if (oldCommand) {
  73. console.log(chalk_1.default.red(`
  74. Warning: Setting ${hookName} script in package.json > scripts will be deprecated.
  75. Please move it to husky.hooks in package.json or .huskyrc file.
  76. For an automatic update you can also run:
  77. npx --no-install husky-upgrade
  78. yarn husky-upgrade
  79. See https://github.com/typicode/husky for more information.
  80. `));
  81. return runCommand(cwd, hookName, oldCommand, env);
  82. }
  83. return 0;
  84. }
  85. exports.default = run;