changelog-tools
Version:
A set of tools for changelog parsing and generation
151 lines • 5.3 kB
JavaScript
import fs from "fs";
import { ListItemChar } from "../lib/types";
import { createParser, detectListBullet, getNewVersionTitle, parseRawChangelogChanges, } from "../lib/cli-helpers";
export const command = ["add [file]"];
export const describe = "add version command";
/* c8 ignore start */
export const builder = (cmd) => {
cmd
.positional("file", {
describe: "file to parse",
type: "string",
check: (file) => {
console.log("file", file);
if (!fs.existsSync(file)) {
return `File not found: ${file}`;
}
},
coerce: (file) => {
return fs.readFileSync(file, "utf8");
},
})
.option("title", {
alias: "t",
describe: "Title of the version to add (e.g. -t '1.0.0 (2020-01-01) - My Version Title'). Title can include semver increase level (e.g. -t 'v:patch') - in this case last known version will be bumped by 1 on the level specified.",
type: "string",
demandOption: true,
})
.option("new-changelog", {
describe: "Raw changelog data to be parsed into the new entry",
type: "array",
default: [],
})
.option("added", {
alias: "a",
describe: "Added entry (e.g. -a 'new feature' -a 'new feature 2').",
type: "array",
default: [],
})
.option("changed", {
alias: "c",
describe: "Changed entry (e.g. -c 'format 1' -c 'api description').",
type: "array",
default: [],
})
.option("deprecated", {
alias: "d",
describe: "Deprecated entry (e.g. -d 'old feature' -d 'old feature 2').",
type: "array",
default: [],
})
.option("removed", {
alias: "r",
describe: "Removed entry (e.g. -r 'old feature' -r 'old feature 2').",
type: "array",
default: [],
})
.option("fixed", {
alias: "f",
describe: "Fixed entry (e.g. -f 'bug 1' -f 'bug 2').",
type: "array",
default: [],
})
.option("list-bullet", {
alias: "l",
describe: "Character to be used for list items (e.g. -l '*').",
type: "string",
default: ListItemChar.AUTO,
choices: Object.values(ListItemChar),
})
.option("output", {
alias: "o",
describe: "Output file",
type: "string",
default: "stdout",
})
.option("parser-type", {
alias: "p",
describe: "Parser type to use. Marked parser is expermental.",
choices: ["builtin", "marked"],
default: "marked",
})
.middleware((argv) => {
const stdin = !process.stdin.isTTY
? fs.readFileSync(process.stdin.fd, "utf8")
: "";
if (stdin.length) {
argv.file = stdin;
}
}, true)
.check((argv) => {
if (!argv.file) {
throw new Error("Specify input either as a file or via stdin.");
}
return true;
});
};
/* c8 ignore stop */
export const handler = (argv) => {
var _a, _b, _c, _d, _e;
const parser = createParser(argv.parserType, {
text: argv.file,
newline: argv.newLine,
});
const addedSections = new Map();
parseRawChangelogChanges(argv.newChangelog, argv.newLine, argv.parserType, addedSections);
const parsedChangelog = parser.parse();
const insertAtLine = (_b = (_a = parsedChangelog.versions[0]) === null || _a === void 0 ? void 0 : _a.line) !== null && _b !== void 0 ? _b : parser.totalParsedLines + 1;
const newlineRegex = /\r\n|\r|\n/g;
const eol = (_d = (_c = RegExp(newlineRegex).exec(argv.file)) === null || _c === void 0 ? void 0 : _c[0]) !== null && _d !== void 0 ? _d : "\n";
const linies = argv.file.split(newlineRegex);
const sections = {
added: "Added",
changed: "Changed",
deprecated: "Deprecated",
removed: "Removed",
fixed: "Fixed",
};
for (const section of Object.keys(sections)) {
if (argv[section].length) {
const secttionTitle = sections[section];
if (!addedSections.has(secttionTitle)) {
addedSections.set(secttionTitle, new Set());
}
for (const entry of argv[section]) {
(_e = addedSections.get(secttionTitle)) === null || _e === void 0 ? void 0 : _e.add(entry.trim());
}
}
}
const bullet = detectListBullet(argv.file, argv.listBullet);
const tile = getNewVersionTitle(argv.title, parsedChangelog);
// @ToDo - use the output formatter to generate the markdown
const newLines = [];
newLines.push(`## ${tile}`);
for (const [section, entries] of addedSections) {
if (entries.size) {
newLines.push(`### ${section}`);
for (const entry of entries) {
newLines.push(`${bullet} ${entry}`);
}
newLines.push("");
}
}
linies.splice(insertAtLine - 1, 0, newLines.join(eol).replace(/\r\n?|\n/g, eol));
if (!argv.output || argv.output === "stdout") {
console.log(linies.join(eol));
}
else {
fs.writeFileSync(argv.output, linies.join(eol));
}
};
//# sourceMappingURL=add.js.map