93 lines
2.6 KiB
JavaScript
Executable File
93 lines
2.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
import { hideBin } from "yargs/helpers";
|
|
import yargs from "yargs";
|
|
import { join } from "path";
|
|
import yaml from "yaml";
|
|
import fs from "fs-extra";
|
|
import consola from "consola";
|
|
import u from "@yanick/updeep-remeda";
|
|
import { once } from "remeda";
|
|
import simpleGit from "simple-git";
|
|
import Ajv from "ajv";
|
|
|
|
import print from "./command/print.js";
|
|
import init from "./command/init.js";
|
|
import schema from "./command/schema.js";
|
|
import add from "./command/add.js";
|
|
import cut from "./command/cut.js";
|
|
import upcoming, { next_release } from "./command/upcoming.js";
|
|
import latest, { latest_version } from "./command/latest-version.js";
|
|
import validate from "./command/validate.js";
|
|
import git_gather from "./command/git-gather.js";
|
|
import schemaV1 from "./changelog-schema.js";
|
|
|
|
consola.raw = (...args) => console.log(...args);
|
|
|
|
yargs(hideBin(process.argv))
|
|
.middleware((config) => {
|
|
config.git = once(simpleGit);
|
|
config.consola = consola;
|
|
config.changelog = once(() =>
|
|
fs
|
|
.readFile(config.source, "utf-8")
|
|
.then(yaml.parse)
|
|
.then((doc) => {
|
|
const ajv = new Ajv();
|
|
const validate = ajv.compile(schemaV1);
|
|
const valid = validate(doc);
|
|
if (valid) return doc;
|
|
|
|
config.consola.error("invalid changelog: ", validate.errors);
|
|
throw "changelog is invalid";
|
|
})
|
|
);
|
|
config.save_changelog = async (changelog) => {
|
|
if (!changelog) changelog = await config.changelog();
|
|
return fs.writeFile(config.source, yaml.stringify(changelog));
|
|
};
|
|
config.next_release = next_release.bind(config);
|
|
config.latest_version = latest_version.bind(config);
|
|
return config;
|
|
})
|
|
.middleware((argv, yargs) => {
|
|
argv.yargs = yargs;
|
|
|
|
argv.add_to_next = async (entry) => {
|
|
const changelog = yaml.parse(await fs.readFile(argv.source, "utf-8"));
|
|
|
|
let next = changelog.releases.find(u.matches({ version: "NEXT" }));
|
|
if (!next) {
|
|
next = { version: "NEXT", changes: [] };
|
|
changelog.releases.unshift(next);
|
|
}
|
|
|
|
if (Object.keys(entry).length === 1) {
|
|
entry = entry.desc;
|
|
}
|
|
|
|
next.changes.push(entry);
|
|
|
|
return fs.writeFile(argv.source, yaml.stringify(changelog));
|
|
};
|
|
})
|
|
.default("source", join(process.cwd(), "CHANGELOG.yml"))
|
|
.describe("source", "changelog source")
|
|
.command(init)
|
|
.command(add)
|
|
.command(print)
|
|
.command(cut)
|
|
.command(schema)
|
|
.command(upcoming)
|
|
.command({
|
|
...upcoming,
|
|
command: "next",
|
|
desc: 'alias for "upcoming"',
|
|
})
|
|
.command(latest)
|
|
.command(git_gather)
|
|
.command(validate)
|
|
.strictCommands()
|
|
.help()
|
|
.parse();
|