UNPKG

prepush-if-changed

Version:

A tool that runs your commands if any git-pushed files match the glob written to be used as a pre-push hook

91 lines (69 loc) 4.07 kB
"use strict"; function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.executeIfMatches = void 0; var _micromatch = _interopRequireDefault(require("micromatch")); var _execSh = _interopRequireWildcard(require("exec-sh")); var _git = _interopRequireWildcard(require("./git")); function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function _getRequireWildcardCache() { return cache; }; return cache; } function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } } function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } var execute = function execute(command) { (0, _execSh["default"])(command, function (err) { if (err) { throw err; } }); }; var executeIfMatches = /*#__PURE__*/ function () { var _ref = _asyncToGenerator( /*#__PURE__*/ regeneratorRuntime.mark(function _callee(config) { var filenames; return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: if (config) { _context.next = 2; break; } return _context.abrupt("return", Promise.reject(Error('Expected config of type object on input'))); case 2: _context.next = 4; return _git["default"].getCommittedFiles(_git["default"].getBranch()); case 4: filenames = _context.sent; Object.keys(config).forEach(function (pattern) { var match = (0, _micromatch["default"])(filenames, [pattern]); if (match.length) { execute(config[pattern]); } }); case 6: case "end": return _context.stop(); } } }, _callee); })); return function executeIfMatches(_x) { return _ref.apply(this, arguments); }; }(); exports.executeIfMatches = executeIfMatches; var withErrorHandling = function withErrorHandling(fn) { return function () { return fn.apply(void 0, arguments)["catch"](function (err) { process.exit(err === _git.UnknownRevisionError ? 0 : 1); }); }; }; var _default = withErrorHandling(executeIfMatches); exports["default"] = _default;