conventional-changelog-ghostwriter
Version:
A configurable conventional-changelog preset
104 lines (103 loc) • 4.61 kB
JavaScript
;
/* eslint-disable global-require */
/* eslint-disable import/no-dynamic-require */
Object.defineProperty(exports, "__esModule", { value: true });
const compare_func_1 = require("compare-func");
const fs_1 = require("fs");
const path_1 = require("path");
const configuration_1 = require("./configuration");
let issuePrefixes = [];
exports.default = {
commitGroupsSort: 'title',
commitPartial: (0, fs_1.readFileSync)((0, path_1.resolve)(__dirname, './templates/commit.hbs'), 'utf-8'),
commitsSort: ['scope', 'subject'],
footerPartial: (0, fs_1.readFileSync)((0, path_1.resolve)(__dirname, './templates/footer.hbs'), 'utf-8'),
finalizeContext: (context) => {
var _a;
if (!context.config) {
context.config = (0, configuration_1.getConfiguration)(context);
}
if (context.currentTag && context.previousTag) {
context.config.compareUrlFormat = context.config.compareUrlFormat
.replace('{{PREVIOUS_TAG}}', context.previousTag)
.replace('{{CURRENT_TAG}}', context.currentTag);
}
if ((_a = context.gitSemverTags) === null || _a === void 0 ? void 0 : _a.length) {
const currentTag = `${context.packageData.name}@${context.packageData.version}`;
const [previousTag] = context.gitSemverTags;
context.config.compareUrlFormat = context.config.compareUrlFormat
.replace('{{PREVIOUS_TAG}}', previousTag)
.replace('{{CURRENT_TAG}}', currentTag);
context.previousTag = previousTag;
}
return context;
},
groupBy: 'type',
headerPartial: (0, fs_1.readFileSync)((0, path_1.resolve)(__dirname, './templates/header.hbs'), 'utf-8'),
mainTemplate: (0, fs_1.readFileSync)((0, path_1.resolve)(__dirname, './templates/template.hbs'), 'utf-8'),
noteGroupsSort: 'title',
notesSort: compare_func_1.default,
transform: (commit, context) => {
var _a, _b;
if (!context.config) {
context.config = (0, configuration_1.getConfiguration)(context);
issuePrefixes = context.config.issuePrefixes.map((issuePrefix) => {
return {
issuePrefix,
regExp: new RegExp(`#${issuePrefix}(\\S+)`, 'g'),
};
});
}
const { config } = context;
const uniqueReferences = new Set();
const references = [];
if ((_a = commit.footer) === null || _a === void 0 ? void 0 : _a.length) {
const { footer } = commit;
issuePrefixes.forEach(({ issuePrefix, regExp }) => {
const issues = Array.from(footer.matchAll(regExp), (matches) => matches[1]);
issues.forEach((issue) => {
const referenceKey = `${issuePrefix}${issue}`;
if (uniqueReferences.has(referenceKey)) {
return;
}
references.push({
issue,
issueUrl: config.issueUrlFormat
.replace('{{ISSUE_PREFIX}}', issuePrefix)
.replace('{{ISSUE_NUMBER}}', issue),
prefix: issuePrefix,
});
uniqueReferences.add(referenceKey);
});
});
}
const hash = commit.commit !== undefined
? commit.commit
: {
long: commit.hash,
short: commit.hash.substr(0, 7),
};
const result = {
commitUrl: config.commitUrlFormat
.replace('{{LONG_HASH}}', hash.long)
.replace('{{SHORT_HASH}}', hash.short),
hash,
notes: commit.notes.map((note) => {
return {
text: note.text.replace(new RegExp(config.issuePrefixes[1], 'g'), '').trim(),
title: 'BREAKING CHANGES',
};
}),
references,
scope: commit.scope === '*' ? null : commit.scope,
subject: commit.subject,
type: commit.type,
};
const supportedType = config.types.find(({ type }) => type === result.type);
if (result.notes.length === 0 && (!supportedType || supportedType.hidden)) {
return undefined;
}
result.type = (_b = supportedType === null || supportedType === void 0 ? void 0 : supportedType.section) !== null && _b !== void 0 ? _b : result.type;
return result;
},
};