@coveo/semantic-monorepo-tools
Version:
A library of helper functions to do SemVer2 compliant releases from Conventional Commits in monorepos
28 lines (27 loc) • 1.11 kB
JavaScript
import { join } from "node:path";
import { createReadStream, createWriteStream, statSync, truncateSync, writeFileSync, } from "node:fs";
import tempfile from "tempfile";
export default async function (PATH, changelog) {
const changelogPath = join(PATH, "CHANGELOG.md");
ensureChangelogExist(changelogPath);
const previousChangelogReadable = await getPreviousChangelogStream(changelogPath);
truncateSync(changelogPath);
const changelogWritable = createWriteStream(changelogPath);
changelogWritable.write(changelog);
return new Promise((resolve) => {
previousChangelogReadable.pipe(changelogWritable).once("finish", resolve);
});
}
function ensureChangelogExist(changelogPath) {
if (!statSync(changelogPath, { throwIfNoEntry: false })) {
writeFileSync(changelogPath, "");
}
}
function getPreviousChangelogStream(changelogPath) {
const tmp = tempfile();
return new Promise((resolve) => {
createReadStream(changelogPath)
.pipe(createWriteStream(tmp))
.once("finish", () => resolve(createReadStream(tmp)));
});
}