From f759989698b738c8610365250bedbc0dc5eac087 Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Thu, 18 May 2023 14:48:47 -0400 Subject: [PATCH] add to the directory --- package.json | 3 +++ src/changelord.js | 33 +++++++++++++++++++++++++++++---- src/command/add.js | 40 ++++++++++++++++++++-------------------- 3 files changed, 52 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index 7f21b17..3725072 100644 --- a/package.json +++ b/package.json @@ -24,11 +24,14 @@ "author": "Yanick Champoux (http://techblog.babyl.ca/)", "license": "ISC", "dependencies": { + "@sindresorhus/slugify": "^2.2.1", "@yanick/updeep-remeda": "^2.2.0", "ajv": "^8.12.0", "consola": "^3.1.0", + "filenamify": "^6.0.0", "fs-extra": "^11.1.1", "markdown-utils": "^1.0.0", + "nanoid": "^4.0.2", "remeda": "^1.14.0", "semver": "^7.5.0", "simple-git": "^3.18.0", diff --git a/src/changelord.js b/src/changelord.js index 985addf..83c3579 100755 --- a/src/changelord.js +++ b/src/changelord.js @@ -10,6 +10,8 @@ import u from "@yanick/updeep-remeda"; import { once } from "remeda"; import simpleGit from "simple-git"; import Ajv from "ajv"; +import { nanoid } from "nanoid"; +import slugify from "@sindresorhus/slugify"; import print from "./command/print.js"; import init from "./command/init.js"; @@ -54,12 +56,35 @@ yargs(hideBin(process.argv)) argv.yargs = yargs; 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" })); if (!next) { - next = { version: "NEXT", changes: [] }; - changelog.releases.unshift(next); + changelog.releases.unshift(base_next_version); } if (Object.keys(entry).length === 1) { @@ -68,7 +93,7 @@ yargs(hideBin(process.argv)) next.changes.push(entry); - return fs.writeFile(argv.source, yaml.stringify(changelog)); + return argv.save_changelog(); }; }) .default("source", join(process.cwd(), "CHANGELOG.yml")) diff --git a/src/command/add.js b/src/command/add.js index c4a2b24..2abc612 100644 --- a/src/command/add.js +++ b/src/command/add.js @@ -3,32 +3,32 @@ import yaml from "yaml"; import u from "@yanick/updeep-remeda"; const handler = async (config) => { - if (!config.change) - throw new Error("can't add a change without a description"); + if (!config.change) + throw new Error("can't add a change without a description"); - const entry = { - desc: config.change.join(" "), - }; + const entry = { + desc: config.change.join(" "), + }; - if (config.ticket) entry.ticket = config.ticket; - if (config.type) entry.type = config.type; + if (config.ticket) entry.ticket = config.ticket; + 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 { - command: "add [change...]", - desc: "add a change to the NEXT release", - builder: (yargs) => { - yargs - .string("ticket") - .describe("ticket", "ticket associated with the change") - .string("type") - .describe("type", "type of change"); - }, - handler, + command: "add [change...]", + desc: "add a change to the NEXT release", + builder: (yargs) => { + yargs + .string("ticket") + .describe("ticket", "ticket associated with the change") + .string("type") + .describe("type", "type of change"); + }, + handler, };