changelog-tools
Version:
A set of tools for changelog parsing and generation
103 lines • 3.35 kB
JavaScript
import fs from "fs";
import { outputMarkdown } from "../lib/output-formatter";
import { createFilterFunction, createParser, detectListBullet, getNewlineSymbol, } from "../lib/cli-helpers";
export const command = ["$0 [file]", "parse [file]"];
export const describe = "parse command";
/* c8 ignore start */
export const builder = (cmd) => {
cmd
.positional("file", {
describe: "file to parse",
type: "string",
check: (file) => {
if (!fs.existsSync(file)) {
return `File not found: ${file}`;
}
},
coerce: (file) => {
return fs.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.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) => {
const parser = createParser(argv.parserType, {
text: argv.file,
newline: argv.newLine,
removeMarkdown: argv.stripMarkdown,
});
const filterFn = 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(outputMarkdown(result, getNewlineSymbol(argv.file, argv.newLine), detectListBullet(argv.file)));
}
else {
console.log(JSON.stringify(result, null, 2));
}
if (argv.error && !filteredChangelog.versions.length) {
process.exit(1);
}
};
//# sourceMappingURL=parse.js.map