Skip to content

Commit

Permalink
ci: switch to mjs for release & tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Mara-Li committed May 30, 2024
1 parent 7468450 commit 2df9c97
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 2 deletions.
148 changes: 148 additions & 0 deletions commit-and-tag-version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import { Command, Option } from "commander";
import commitAndTagVersion from "commit-and-tag-version";
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({
danger: red,
dark: dim.gray,
disabled: gray,
em: italic,
heading: bold.underline,
info: cyan,
muted: dim,
primary: blue,
strong: bold,
success: green.bold,
warning: yellow.underline,
});

const info = (msg) => pkg.info(msg);
const heading = (msg) => pkg.heading(msg);
const em = (msg) => pkg.em(msg);

program
.description("Bump version and create a new tag")
.option("-b, --beta", "Pre-release version")
.option("--dry-run", "Dry run")
.addOption(
new Option("-r, --release-as <size>", "release type version").choices([
"major",
"minor",
"patch",
])
);

program.parse();
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 msg = dedent(`
${heading("Options :")}
${betaMsg}${dryRunMsg}${releaseAsMsg}
`);

console.log(msg);
console.log();

if (opt.beta) {
console.log(`${bold.green(">")} ${info.underline("Bumping beta version...")}`);
console.log();
const bumpFiles = [
{
filename: "manifest-beta.json",
type: "json",
},
{
filename: "package.json",
type: "json",
},
{
filename: "package-lock.json",
type: "json",
},
];
commitAndTagVersion({
infile: "CHANGELOG-beta.md",
bumpFiles,
prerelease: "",
dryRun: opt.dryRun,
tagPrefix: "",
})
.then(() => {
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)}`)
: info("Release");
console.log(`${bold.green(">")} ${underline(versionBumped)}`);
console.log();

const bumpFiles = [
{
filename: "manifest-beta.json",
type: "json",
},
{
filename: "package.json",
type: "json",
},
{
filename: "package-lock.json",
type: "json",
},
{
filename: "manifest.json",
type: "json",
}
];


commitAndTagVersion({
infile: "CHANGELOG.md",
bumpFiles,
dryRun: opt.dryRun,
tagPrefix: "",
releaseAs: opt.releaseAs,
})
.then(() => {
removeText("CHANGELOG.md");
console.log("Done");
})
.catch((err) => {
console.error(err);
});
removeText("CHANGELOG.md");
}
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dev:prod": "node esbuild.config.mjs --vault",
"dev": "node esbuild.config.mjs",
"export": "node esbuild.config.mjs --production --vault",
"bump": "node commit-and-tag-version.js",
"bump": "node commit-and-tag-version.mjs",
"postbump": "git push --follow-tags origin master",
"predeploy": "pnpm run bump",
"deploy": "pnpm run export"
Expand Down Expand Up @@ -59,4 +59,4 @@
"got@<11.8.5": ">=11.8.5"
}
}
}
}

0 comments on commit 2df9c97

Please sign in to comment.