bsg
Version:
Lint all in one tool for BSG-FE
122 lines (105 loc) • 3.39 kB
JavaScript
module.exports = function ({ r }) {
const fs = require("fs");
const addStream = require("add-stream");
const conventionalChangelog = require("conventional-changelog");
const compareFunc = require("compare-func");
const tempfile = require("tempfile");
const changelogStream = conventionalChangelog(
{
preset: "angular",
releaseCount: r,
},
undefined,
undefined,
{
headerPattern: /^(\[\w*\])(?:\((.*)\))?: (.*)$/,
},
{
transform: (commit, context) => {
if (context?.packageData?.repository?.type === "gerrit") {
context.commit = "#/q";
context.linkCompare = false;
}
let discard = true;
const issues = [];
commit.notes.forEach((note) => {
note.title = "BREAKING CHANGES";
discard = false;
});
if (commit.type === "[feature]") {
commit.type = "新特性";
} else if (commit.type === "[fix]") {
commit.type = "Bug 修复";
} else if (commit.type === "[update]") {
commit.type = "特性更新";
} else if (discard) {
return;
} else if (commit.type === "[docs]") {
commit.type = "文档";
} else if (commit.type === "[style]") {
commit.type = "样式";
} else if (commit.type === "[refactor]") {
commit.type = "代码重构";
} else if (commit.type === "[test]") {
commit.type = "测试";
} else if (commit.type === "[chore]") {
commit.type = "其他";
}
if (commit.scope === "*") {
commit.scope = "";
}
if (typeof commit.hash === "string") {
commit.shortHash = commit.hash.substring(0, 7);
}
if (typeof commit.subject === "string") {
const bugUrl = context?.packageData?.bugs?.url;
let url = bugUrl
? bugUrl
: context.repository
? `${context.host}/${context.owner}/${context.repository}`
: context.repoUrl;
if (url) {
url = bugUrl ? `${url}?m=bug&f=view&bugID=` : `${url}/issues/`;
// Issue URLs.
commit.subject = commit.subject.replace(
/#([0-9]+)/g,
(_, issue) => {
issues.push(issue);
return `[#${issue}](${url}${issue})`;
}
);
}
}
// remove references that already appear in the subject
commit.references = commit.references.filter((reference) => {
if (issues.indexOf(reference.issue) === -1) {
return true;
}
return false;
});
return commit;
},
groupBy: "type",
commitGroupsSort: "title",
commitsSort: ["scope", "subject"],
noteGroupsSort: "title",
notesSort: compareFunc,
}
);
const file = "CHANGELOG.md";
function noInputFile() {
changelogStream.pipe(fs.createWriteStream(file));
}
if (r !== 0) {
const tmp = tempfile();
const readStream = fs.createReadStream(file).on("error", noInputFile);
changelogStream
.pipe(addStream(readStream))
.pipe(fs.createWriteStream(tmp))
.on("finish", function () {
fs.createReadStream(tmp).pipe(fs.createWriteStream(file));
});
} else {
noInputFile();
}
};