From ca252d4a19d808ae476b14f6a19471ff7da58141 Mon Sep 17 00:00:00 2001 From: Packy Date: Mon, 26 Apr 2021 17:00:15 +0800 Subject: [PATCH 1/4] Support page header setting of changelog file It is the same as the conventional-changelog-config-spec setting of standard-version. --- index.js | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 7fda6c7..ced65d8 100644 --- a/index.js +++ b/index.js @@ -90,8 +90,18 @@ class ConventionalChangelog extends Plugin { }); } - async writeChangelog() { + getOldChangelog() { const { infile } = this.options; + return new Promise((resolve, reject) => { + const readStream = fs.createReadStream(infile); + const resolver = result => resolve(result.toString().trim()); + readStream.pipe(concat(resolver)); + readStream.on('error', reject); + }); + } + + async writeChangelog() { + const { infile, header } = this.options; let { changelog } = this.config.getContext(); let hasInfile = false; @@ -102,12 +112,20 @@ class ConventionalChangelog extends Plugin { this.debug(err); } + let oldChangelog = "" + try{ + oldChangelog = await this.getOldChangelog(); + oldChangelog = oldChangelog.replace(new RegExp(EOL, 'g'), '\n').replace(header, ''); + }catch(err){ + this.debug(err); + } + if (!hasInfile) { changelog = await this.generateChangelog({ releaseCount: 0 }); this.debug({ changelog }); } - await prependFile(infile, changelog + EOL + EOL); + fs.writeFileSync(infile, header + EOL + EOL + changelog + EOL + EOL + oldChangelog); if (!hasInfile) { await this.exec(`git add ${infile}`); From 159ee8120d9c65edbf4bf0d3db173a54a39c6aa7 Mon Sep 17 00:00:00 2001 From: Packy Date: Thu, 10 Feb 2022 15:37:23 +0800 Subject: [PATCH 2/4] Update tests to support header settings --- test.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test.js b/test.js index 264596e..4c78ac7 100644 --- a/test.js +++ b/test.js @@ -130,15 +130,16 @@ test('should use provided version', async t => { }); test(`should write and update infile (${infile})`, async t => { - const options = { [namespace]: { preset, infile }, git }; + const header = "The header" + const options = { [namespace]: { preset, infile, header }, git }; const plugin = factory(Plugin, { namespace, options }); await runTasks(plugin); const changelog = fs.readFileSync(infile); - assert.strictEqual(changelog.toString().trim(), 'The changelog'); + assert.strictEqual(changelog.toString().trim(), `${header}${EOL}${EOL}The changelog`); { await runTasks(plugin); const changelog = fs.readFileSync(infile); - assert.strictEqual(changelog.toString().trim(), `The changelog${EOL}${EOL}The changelog`); + assert.strictEqual(changelog.toString().trim(), `${header}${EOL}${EOL}The changelog${EOL}${EOL}The changelog`); } fs.unlinkSync(infile); }); From 508dad6af4bd63df12efda384c26b120fcc8fee5 Mon Sep 17 00:00:00 2001 From: Packy Date: Thu, 10 Feb 2022 15:38:22 +0800 Subject: [PATCH 3/4] fix extra line break logic --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index ced65d8..904e982 100644 --- a/index.js +++ b/index.js @@ -115,7 +115,7 @@ class ConventionalChangelog extends Plugin { let oldChangelog = "" try{ oldChangelog = await this.getOldChangelog(); - oldChangelog = oldChangelog.replace(new RegExp(EOL, 'g'), '\n').replace(header, ''); + oldChangelog = oldChangelog.replace(header, ''); }catch(err){ this.debug(err); } @@ -125,7 +125,7 @@ class ConventionalChangelog extends Plugin { this.debug({ changelog }); } - fs.writeFileSync(infile, header + EOL + EOL + changelog + EOL + EOL + oldChangelog); + fs.writeFileSync(infile, header + EOL + EOL + changelog + oldChangelog); if (!hasInfile) { await this.exec(`git add ${infile}`); From 9bb248236fafbfe97bd8f768992d18bbb1e0cc26 Mon Sep 17 00:00:00 2001 From: Packy Date: Wed, 23 Feb 2022 17:48:22 +0800 Subject: [PATCH 4/4] replace header newline symbol --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 904e982..00218b0 100644 --- a/index.js +++ b/index.js @@ -115,7 +115,7 @@ class ConventionalChangelog extends Plugin { let oldChangelog = "" try{ oldChangelog = await this.getOldChangelog(); - oldChangelog = oldChangelog.replace(header, ''); + oldChangelog = oldChangelog.replace(header.split(/\r\n|\r|\n/g).join(EOL), ''); }catch(err){ this.debug(err); }