changelord.js/src/command/cut.js

84 lines
2.1 KiB
JavaScript

import yaml from "yaml";
import fs from "fs-extra";
import u from "@yanick/updeep-remeda";
import semverInc from "semver/functions/inc.js";
import { simpleGit } from "simple-git";
import { base_next_version } from "../utils.js";
const code_churn = async (previous_version) => {
previous_version = previous_version
? "v" + previous_version.version
: "4b825dc642cb6eb9a060e54bf8d69288fbee4904"; // empty tree sha1
return simpleGit().diff(["--shortstat", previous_version, "HEAD"]);
};
const handler = async (config) => {
config.consola.start("cutting the next version...");
const changelog = await config.changelog();
const next = changelog.releases.find(u.matches({ version: "NEXT" }));
if (!next) throw new Error("no changes since last version, aborting");
const previous_version = changelog.releases.find(
({ version }) => version !== "NEXT"
);
next.date = new Date().toISOString().replace(/T.*/, "");
const type_to_level = (type = "") =>
changelog.change_types.find((t) => t.keywords.includes(type)).level;
const bump_level = (types) =>
types.includes("major")
? "major"
: types.includes("minor")
? "minor"
: "patch";
const bumper = bump_level(
next.changes.map(({ type }) => type_to_level(type))
);
if (changelog.project?.with_stats) {
next.changes.push({
type: "stats",
desc: "code churn:" + (await code_churn(previous_version)),
});
}
next.version = semverInc(
previous_version ? previous_version.version : "0.0.0",
bumper
);
// add a new NEXT
changelog.releases.unshift(base_next_version);
if (config.dry) {
config.consola.info("running in dry mode, not saving\n", next);
} else {
await config.save_changelog(changelog);
if (changelog.project?.next_directory) {
config.consola.info(
`removing files in ${changelog.project.next_directory}`
);
await config.delete_next_dir_entries();
}
}
config.consola.success(`version ${next.version} is cut!`);
};
export default {
command: "cut",
desc: "cut the next version",
builder: (yargs) => {
yargs.boolean("dry");
},
handler,
};