Skip to content

Commit

Permalink
feat: support generate changelog inFile
Browse files Browse the repository at this point in the history
  • Loading branch information
hyoban committed Apr 20, 2024
1 parent 22b35fb commit 9f2f9dc
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "node:fs";
import { EOL } from "node:os";

import { versionBump } from "bumpp";
import {
Expand Down Expand Up @@ -164,6 +165,39 @@ class ReleaseItPnpmPlugin extends Plugin {
}
}

async writeChangelog(changelog) {
const { inFile, header: _header = "# Changelog" } = this.options;
const header = _header.split(/\r\n|\r|\n/g).join(EOL);

let hasInFile = false;
try {
fs.accessSync(inFile);
hasInFile = true;
} catch (err) {
this.debug(err);
}

let previousChangelog = "";
try {
previousChangelog = await fs.promises.readFile(inFile, "utf8");
previousChangelog = previousChangelog.replace(header, "");
} catch (err) {
this.debug(err);
}

fs.writeFileSync(
inFile,
header +
(changelog ? EOL + EOL + changelog.trim() : "") +
(previousChangelog ? EOL + EOL + previousChangelog.trim() : "") +
EOL,
);

if (!hasInFile) {
await this.exec(`git add ${inFile}`);
}
}

async release() {
if (this.options?.disableRelease) return;

Expand Down Expand Up @@ -218,6 +252,11 @@ class ReleaseItPnpmPlugin extends Plugin {
return;
}

const { inFile } = this.options;
if (inFile) {
await this.writeChangelog(md);
}

if (!config.token) {
this.log.error(
red(
Expand Down

0 comments on commit 9f2f9dc

Please sign in to comment.