UNPKG

changelog-tools

Version:

A set of tools for changelog parsing and generation

111 lines 3.78 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 output_formatter_1 = require("../lib/output-formatter"); const cli_helpers_1 = require("../lib/cli-helpers"); exports.command = ["$0 [file]", "parse [file]"]; exports.describe = "parse command"; /* c8 ignore start */ const builder = (cmd) => { cmd .positional("file", { describe: "file to parse", type: "string", check: (file) => { if (!fs_1.default.existsSync(file)) { return `File not found: ${file}`; } }, coerce: (file) => { return fs_1.default.readFileSync(file, "utf8"); }, }) .option("consolidate", { alias: "c", describe: "Consolidate changes of the same type into one version entry", type: "boolean", default: false, }) .option("semver", { alias: "s", describe: "Filter versions based on semver range. Example: '>=1.0.0'", type: "string", default: "", }) .option("error", { alias: "e", describe: "Error when no entries are found", type: "boolean", default: false, }) .option("filter", { alias: "f", describe: "Filter entries based on provided statement. Use this to access version data. Statement needs to return a boolean value. Example: 'this.version === \"1.0.0\"'", type: "string", default: "", }) .option("parser-type", { alias: "p", describe: "Parser type to use. Marked parser is expermental.", choices: ["builtin", "marked"], default: "marked", }) // output format .option("format", { describe: "output format", choices: ["json", "markdown"], default: "json", }) .option("strip-markdown", { describe: "Remove markdown formatting from the changelog", type: "boolean", default: true, }) .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) => { const parser = (0, cli_helpers_1.createParser)(argv.parserType, { text: argv.file, newline: argv.newLine, removeMarkdown: argv.stripMarkdown, }); const filterFn = (0, cli_helpers_1.createFilterFunction)(argv.filter, argv.semver); const parsedChangelog = parser.parse(); const filteredChangelog = filterFn ? parsedChangelog.filter(filterFn) : parsedChangelog; // @ToDo - support detected or provided bullets const result = argv.consolidate ? filteredChangelog.consolidate() : filteredChangelog; if (argv.format === "markdown") { console.log((0, output_formatter_1.outputMarkdown)(result, (0, cli_helpers_1.getNewlineSymbol)(argv.file, argv.newLine), (0, cli_helpers_1.detectListBullet)(argv.file))); } else { console.log(JSON.stringify(result, null, 2)); } if (argv.error && !filteredChangelog.versions.length) { process.exit(1); } }; exports.handler = handler; //# sourceMappingURL=parse.js.map