Skip to content

Commit

Permalink
chore: hooks & changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed Jun 1, 2024
1 parent 6203b19 commit 4d33f6c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 31 deletions.
41 changes: 11 additions & 30 deletions commit-and-tag-version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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)}`)
Expand All @@ -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) => {
Expand Down
30 changes: 30 additions & 0 deletions hooks/_changelog.mjs
Original file line number Diff line number Diff line change
@@ -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");
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down

0 comments on commit 4d33f6c

Please sign in to comment.