From 9f2f9dc8cbea9bcc29655fd971319d46eab71be5 Mon Sep 17 00:00:00 2001 From: Stephen Zhou Date: Sat, 20 Apr 2024 23:10:38 +0800 Subject: [PATCH] feat: support generate changelog inFile --- src/index.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/index.js b/src/index.js index 4d8deb6..49743ba 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ import fs from "node:fs"; +import { EOL } from "node:os"; import { versionBump } from "bumpp"; import { @@ -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; @@ -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(