diff --git a/index.js b/index.js index 7fda6c7..00218b0 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(header.split(/\r\n|\r|\n/g).join(EOL), ''); + }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 + oldChangelog); if (!hasInfile) { await this.exec(`git add ${infile}`); 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); });