add to the directory

This commit is contained in:
Yanick Champoux 2023-05-18 14:48:47 -04:00
parent 09d60859e1
commit f759989698
3 changed files with 52 additions and 24 deletions

View File

@ -24,11 +24,14 @@
"author": "Yanick Champoux <yanick@babyl.ca> (http://techblog.babyl.ca/)", "author": "Yanick Champoux <yanick@babyl.ca> (http://techblog.babyl.ca/)",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {
"@sindresorhus/slugify": "^2.2.1",
"@yanick/updeep-remeda": "^2.2.0", "@yanick/updeep-remeda": "^2.2.0",
"ajv": "^8.12.0", "ajv": "^8.12.0",
"consola": "^3.1.0", "consola": "^3.1.0",
"filenamify": "^6.0.0",
"fs-extra": "^11.1.1", "fs-extra": "^11.1.1",
"markdown-utils": "^1.0.0", "markdown-utils": "^1.0.0",
"nanoid": "^4.0.2",
"remeda": "^1.14.0", "remeda": "^1.14.0",
"semver": "^7.5.0", "semver": "^7.5.0",
"simple-git": "^3.18.0", "simple-git": "^3.18.0",

View File

@ -10,6 +10,8 @@ import u from "@yanick/updeep-remeda";
import { once } from "remeda"; import { once } from "remeda";
import simpleGit from "simple-git"; import simpleGit from "simple-git";
import Ajv from "ajv"; import Ajv from "ajv";
import { nanoid } from "nanoid";
import slugify from "@sindresorhus/slugify";
import print from "./command/print.js"; import print from "./command/print.js";
import init from "./command/init.js"; import init from "./command/init.js";
@ -54,12 +56,35 @@ yargs(hideBin(process.argv))
argv.yargs = yargs; argv.yargs = yargs;
argv.add_to_next = async (entry) => { argv.add_to_next = async (entry) => {
const changelog = yaml.parse(await fs.readFile(argv.source, "utf-8")); const dir = await argv.changelog().then((c) => c.project.next_directory);
if (dir) {
await fs.ensureDir(dir);
const filename = join(
dir,
[
entry.ticket,
entry.feat,
slugify(entry.desc, {
separator: "_",
}).slice(0, 10),
]
.filter((x) => x)
.join("-") + ".yml"
);
argv.consola.info(`writing change to ${filename}`);
if (fs.existsSync(filename)) {
argv.consola.error(`file ${filename} already exist`);
yargs.exit(1);
}
return fs.writeFile(filename, yaml.stringify([entry]));
}
const changelog = await argv.changelog();
let next = changelog.releases.find(u.matches({ version: "NEXT" })); let next = changelog.releases.find(u.matches({ version: "NEXT" }));
if (!next) { if (!next) {
next = { version: "NEXT", changes: [] }; changelog.releases.unshift(base_next_version);
changelog.releases.unshift(next);
} }
if (Object.keys(entry).length === 1) { if (Object.keys(entry).length === 1) {
@ -68,7 +93,7 @@ yargs(hideBin(process.argv))
next.changes.push(entry); next.changes.push(entry);
return fs.writeFile(argv.source, yaml.stringify(changelog)); return argv.save_changelog();
}; };
}) })
.default("source", join(process.cwd(), "CHANGELOG.yml")) .default("source", join(process.cwd(), "CHANGELOG.yml"))

View File

@ -3,32 +3,32 @@ import yaml from "yaml";
import u from "@yanick/updeep-remeda"; import u from "@yanick/updeep-remeda";
const handler = async (config) => { const handler = async (config) => {
if (!config.change) if (!config.change)
throw new Error("can't add a change without a description"); throw new Error("can't add a change without a description");
const entry = { const entry = {
desc: config.change.join(" "), desc: config.change.join(" "),
}; };
if (config.ticket) entry.ticket = config.ticket; if (config.ticket) entry.ticket = config.ticket;
if (config.type) entry.type = config.type; if (config.type) entry.type = config.type;
config.consola.start(`adding '${entry.desc}' to the changelog`); config.consola.start(`adding '${entry.desc}' to the changelog`);
config.add_to_next(entry); await config.add_to_next(entry);
config.consola.success("done!"); config.consola.success("done!");
}; };
export default { export default {
command: "add [change...]", command: "add [change...]",
desc: "add a change to the NEXT release", desc: "add a change to the NEXT release",
builder: (yargs) => { builder: (yargs) => {
yargs yargs
.string("ticket") .string("ticket")
.describe("ticket", "ticket associated with the change") .describe("ticket", "ticket associated with the change")
.string("type") .string("type")
.describe("type", "type of change"); .describe("type", "type of change");
}, },
handler, handler,
}; };