UNPKG

@joberstein12/commit-history-validator

Version:

Validates commits and release according to the conventional commit specification.

137 lines (102 loc) 4.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setHooksPath = exports.resetHooksPath = exports.isRemoteBranch = exports.hasChanges = exports.getHooksPath = exports.getFirstMissingCommit = exports.getCurrentBranch = exports.fetchRemoteBranch = void 0; var _child_process = require("child_process"); function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; } var encoding = 'utf-8'; var getCurrentBranch = function getCurrentBranch() { var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, cwd = _ref.cwd; var command = "git branch --show-current"; return (0, _child_process.execSync)(command, { encoding: encoding, cwd: cwd }).trim(); }; exports.getCurrentBranch = getCurrentBranch; var getHooksPath = function getHooksPath() { var _ref2 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, cwd = _ref2.cwd; try { var command = "git config core.hooksPath"; return (0, _child_process.execSync)(command, { encoding: encoding, cwd: cwd }).trim(); } catch (e) { return ''; } }; exports.getHooksPath = getHooksPath; var setHooksPath = function setHooksPath(path) { var _ref3 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, cwd = _ref3.cwd; (0, _child_process.execSync)("git config core.hooksPath ".concat(path), { cwd: cwd }); }; exports.setHooksPath = setHooksPath; var resetHooksPath = function resetHooksPath() { var _ref4 = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}, cwd = _ref4.cwd; (0, _child_process.execSync)("git config --unset core.hooksPath", { cwd: cwd }); }; exports.resetHooksPath = resetHooksPath; var getFirstMissingCommit = function getFirstMissingCommit(branch1, branch2) { var _ref5 = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}, cwd = _ref5.cwd; var command = "git rev-list ^".concat(branch1, " \"").concat(branch2, "\" | tail -n 1"); return (0, _child_process.execSync)(command, { encoding: encoding, cwd: cwd }).trim(); }; exports.getFirstMissingCommit = getFirstMissingCommit; var fetchRemoteBranch = function fetchRemoteBranch(branch) { var _ref6 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, cwd = _ref6.cwd; (0, _child_process.execSync)("git fetch origin ".concat(branch, ":").concat(branch), { cwd: cwd, stdio: 'ignore' }); }; exports.fetchRemoteBranch = fetchRemoteBranch; var hasChanges = function hasChanges(_ref7) { var branch = _ref7.branch, _ref7$files = _ref7.files, files = _ref7$files === void 0 ? [] : _ref7$files, relative = _ref7.relative; var _ref8 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, cwd = _ref8.cwd; var args = ["--quiet", branch].concat(_toConsumableArray(relative ? ["--relative=".concat(relative)] : [])); var command = "git diff ".concat(args.join(' '), " -- ").concat(files.join(' ')); try { (0, _child_process.execSync)(command, { encoding: encoding, cwd: cwd }); return false; } catch (e) { return true; } }; exports.hasChanges = hasChanges; var isRemoteBranch = function isRemoteBranch(branch) { var _ref9 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}, cwd = _ref9.cwd; var command = "git ls-remote --heads origin ".concat(branch); return !!(0, _child_process.execSync)(command, { encoding: encoding, cwd: cwd }).trim(); }; exports.isRemoteBranch = isRemoteBranch;