From 4d33f6cfed4f6ef9cc6f4310e2b4b1993b47f1f8 Mon Sep 17 00:00:00 2001 From: Mara Date: Sat, 1 Jun 2024 09:42:54 +0200 Subject: [PATCH] chore: hooks & changelog --- commit-and-tag-version.mjs | 41 ++++++++++---------------------------- hooks/_changelog.mjs | 30 ++++++++++++++++++++++++++++ package.json | 2 +- 3 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 hooks/_changelog.mjs diff --git a/commit-and-tag-version.mjs b/commit-and-tag-version.mjs index 7b333fda..213c369a 100644 --- a/commit-and-tag-version.mjs +++ b/commit-and-tag-version.mjs @@ -4,23 +4,6 @@ import dedent from "dedent"; import pkg from "ansi-colors"; const { red, dim, gray, italic, bold, cyan, blue, green, underline, yellow, theme } = pkg; -import { readFileSync, writeFile } from "fs"; - -/** - * Remove text from the file - * @param {string} path - */ -function removeText(path) { - const toRemove = ["# Changelog", "All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines."] - let changelog = readFileSync(path, "utf8"); - for (const remove of toRemove) changelog = changelog.replace(remove, "").trim(); - changelog = changelog.replaceAll(/[\n\r]{3,}/gm, "\n\n").trim(); - changelog = changelog.replaceAll(/## (.*)[\n\r]{2}### /gm, "## $1\n### ").trim(); - writeFile(path, changelog.trim(), "utf8", (err) => { - if (err) return console.error(err); - }); -} - const program = new Command(); theme({ @@ -58,14 +41,12 @@ const opt = program.opts(); const betaMsg = opt.beta ? em("- Pre-release\n\t") : ""; const dryRunMsg = opt.dryRun ? em("- Dry run\n\t") : ""; -const releaseAsMsg = opt.releaseAs - ? em(`- Release as ${underline(opt.releaseAs)}`) - : ""; +const releaseAsMsg = opt.releaseAs ? em(`- Release as ${underline(opt.releaseAs)}`) : ""; const msg = dedent(` -${heading("Options :")} - ${betaMsg}${dryRunMsg}${releaseAsMsg} -`); + ${heading("Options :")} + ${betaMsg}${dryRunMsg}${releaseAsMsg} + `); console.log(msg); console.log(); @@ -93,16 +74,16 @@ if (opt.beta) { prerelease: "", dryRun: opt.dryRun, tagPrefix: "", + scripts: { + postchangelog: "node _changelog.mjs -b", + }, }) .then(() => { - if (!opt.dryRun) - removeText("CHANGELOG-beta.md"); console.log("Done"); }) .catch((err) => { console.error(err); }); - removeText("CHANGELOG-beta.md"); } else { const versionBumped = opt.releaseAs ? info(`Release as ${underline(opt.releaseAs)}`) @@ -126,20 +107,20 @@ if (opt.beta) { { filename: "manifest.json", type: "json", - } + }, ]; - commitAndTagVersion({ infile: "CHANGELOG.md", bumpFiles, dryRun: opt.dryRun, tagPrefix: "", releaseAs: opt.releaseAs, + scripts: { + postchangelog: "node _changelog.mjs", + }, }) .then(() => { - if (!opt.dryRun) - removeText("CHANGELOG.md"); console.log("Done"); }) .catch((err) => { diff --git a/hooks/_changelog.mjs b/hooks/_changelog.mjs new file mode 100644 index 00000000..004e9bec --- /dev/null +++ b/hooks/_changelog.mjs @@ -0,0 +1,30 @@ +import { readFileSync, writeFile } from "fs"; + +import { Command } from "commander"; +const program = new Command(); + +program.option("-b, --beta", "Pre-release version"); + +program.parse(); +const opt = program.opts(); + +/** + * Remove text from the file + * @param {string} path + */ +function removeText(path) { + const toRemove = [ + "# Changelog", + "All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.", + ]; + let changelog = readFileSync(path, "utf8"); + for (const remove of toRemove) changelog = changelog.replace(remove, "").trim(); + changelog = changelog.replaceAll(/[\n\r]{3,}/gm, "\n\n").trim(); + changelog = changelog.replaceAll(/## (.*)[\n\r]{2}### /gm, "## $1\n### ").trim(); + writeFile(path, changelog.trim(), "utf8", (err) => { + if (err) return console.error(err); + }); +} + +if (!opt.beta) removeText("CHANGELOG.md"); +else removeText("CHANGELOG-beta.md"); diff --git a/package.json b/package.json index e72c2b03..537158c2 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "dev": "node esbuild.config.mjs", "export": "node esbuild.config.mjs --production --vault", "bump": "node commit-and-tag-version.mjs", - "postbump": "git commit --amend --no-edit' && git push --follow-tags origin master", + "postbump": "git push --follow-tags origin master", "predeploy": "pnpm run bump", "deploy": "pnpm run export" },