@joberstein12/commit-history-validator
Version:
Validates commits and release according to the conventional commit specification.
84 lines (66 loc) • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _fs = require("fs");
var _path = require("../utils/path.js");
var _git = require("../utils/git.js");
var _child_process = require("child_process");
var _path2 = require("path");
var encoding = 'utf-8';
var _default = function _default() {
var executingDir = "".concat((0, _path.getExecutingProjectDirectory)());
var executingProjectData = (0, _fs.readFileSync)("".concat(executingDir, "/package.json"), {
encoding: encoding
});
var _JSON$parse = JSON.parse(executingProjectData),
executingAppName = _JSON$parse.name,
version = _JSON$parse.version;
if ((0, _path.getWorkspaceNames)().includes(executingAppName)) {
var hookImplementationPath = (0, _path2.resolve)("".concat(executingDir, "/../hooks"));
(0, _git.setHooksPath)(hookImplementationPath);
console.info("Set local hooks path to: ".concat(hookImplementationPath));
(0, _child_process.execSync)("chmod -R +x ".concat(hookImplementationPath));
return;
}
var existingHooksPath = (0, _git.getHooksPath)();
if (existingHooksPath) {
console.warn("Hooks path already exists at: '".concat(existingHooksPath, "'"));
}
var workingDir = (0, _path.getWorkingProjectDirectory)();
var hooksPath = existingHooksPath || "".concat(workingDir, "/.git/hooks");
var hooksData = (0, _fs.readFileSync)("".concat(executingDir, "/dist/hooks.txt"), {
encoding: encoding
});
var hooks = hooksData.trim().split(' ');
console.info("Found the following hooks to install: [".concat(hooks.join(', '), "]\n"));
hooks.forEach(function (hook) {
var outputFile = [hooksPath, hook].join("/");
var contents = "npx ".concat(executingAppName, "@").concat(version, " hooks run ").concat(hook, " $@");
if (!(0, _fs.existsSync)(outputFile)) {
console.info("Writing the command to run the '".concat(hook, "' hook..."));
(0, _fs.writeFileSync)(outputFile, contents, {
mode: 493
});
return;
}
var commandMatcher = new RegExp(contents.replace("@".concat(version), '.*').replaceAll("$", "\\$"));
console.warn("Hook already exists at file: '".concat(outputFile, "'"));
var existingHookLines = (0, _fs.readFileSync)(outputFile, {
encoding: encoding
}).split('\n');
var commandLineIdx = existingHookLines.findIndex(function (line) {
return commandMatcher.test(line);
});
if (commandLineIdx < 0) {
console.info("Appending the command to run the '".concat(hook, "' hook...\n"));
(0, _fs.appendFileSync)(outputFile, '\n' + contents);
return;
}
existingHookLines.splice(commandLineIdx, 1, contents);
console.info("Updating the '".concat(hook, "' hook to version ").concat(version, ".\n"));
(0, _fs.writeFileSync)(outputFile, existingHookLines.join('\n'));
});
};
exports["default"] = _default;