UNPKG

changelog-tools

Version:

A set of tools for changelog parsing and generation

159 lines 5.72 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.handler = exports.builder = exports.describe = exports.command = void 0; const fs_1 = __importDefault(require("fs")); const types_1 = require("../lib/types"); const cli_helpers_1 = require("../lib/cli-helpers"); exports.command = ["add [file]"]; exports.describe = "add version command"; /* c8 ignore start */ const builder = (cmd) => { cmd .positional("file", { describe: "file to parse", type: "string", check: (file) => { console.log("file", file); if (!fs_1.default.existsSync(file)) { return `File not found: ${file}`; } }, coerce: (file) => { return fs_1.default.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: types_1.ListItemChar.AUTO, choices: Object.values(types_1.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_1.default.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; }); }; exports.builder = builder; /* c8 ignore stop */ const handler = (argv) => { var _a, _b, _c, _d, _e; const parser = (0, cli_helpers_1.createParser)(argv.parserType, { text: argv.file, newline: argv.newLine, }); const addedSections = new Map(); (0, cli_helpers_1.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 = (0, cli_helpers_1.detectListBullet)(argv.file, argv.listBullet); const tile = (0, cli_helpers_1.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_1.default.writeFileSync(argv.output, linies.join(eol)); } }; exports.handler = handler; //# sourceMappingURL=add.js.map