git-commit-stamper
Version:
Updates a log file with selected last git commit data
64 lines • 2.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = require("fs");
const util_1 = require("util");
const handlebars_1 = __importDefault(require("handlebars"));
const async_last_commit_1 = require("./async-last-commit");
const asyncReadFile = util_1.promisify(fs_1.readFile);
const asyncWriteFile = util_1.promisify(fs_1.writeFile);
const CHANGELOG_BODY_REGEX = /==summary==([\S\s]*?)==end summary==/gm;
const FIELDS_TO_ADD_NEWLINES = [
'subject',
'sanitizedSubject',
'body',
'summary',
'note'
];
function addNewlinesToCommitData(commitData) {
FIELDS_TO_ADD_NEWLINES.forEach(field => {
let content = commitData[field];
if (content) {
content = commitData[field].trimEnd();
content = content + '\n\n';
commitData[field] = content;
}
});
}
exports.addNewlinesToCommitData = addNewlinesToCommitData;
function parseAndCompileChangelog(commitData, changelog) {
const template = handlebars_1.default.compile(changelog);
return template(commitData);
}
exports.parseAndCompileChangelog = parseAndCompileChangelog;
async function handleStamper(params) {
const commit = await async_last_commit_1.getTheLastCommit();
if (commit.subject.includes('[skip-changelog]')) {
console.log('Skipping changelog stamping\n');
return;
}
extractSummary(commit);
addNewlinesToCommitData(commit);
let logData = await asyncReadFile(params.logFile, 'utf8');
let newLog = parseAndCompileChangelog(commit, logData);
if (params.simulate) {
console.log(newLog);
return;
}
const outPath = params.outFile || params.logFile;
await asyncWriteFile(outPath, newLog, 'utf8');
console.log('Changelog file updated\n');
}
exports.handleStamper = handleStamper;
function extractSummary(commit) {
const bodyMatches = CHANGELOG_BODY_REGEX.exec(commit.body);
commit['summary'] = bodyMatches ? bodyMatches[1].trim() : '';
if (commit['summary']) {
// remove the changelog section from the original body
commit.body = commit.body.replace(bodyMatches[0], '').trim();
}
}
exports.extractSummary = extractSummary;
//# sourceMappingURL=handle-stamper.js.map