Skip to content

Commit

Permalink
Support page header setting of changelog file (#42)
Browse files Browse the repository at this point in the history
* Support page header setting of changelog file

It is the same as the conventional-changelog-config-spec setting of standard-version.

* Update tests to support header settings

* fix extra line break logic

* replace header newline symbol
  • Loading branch information
lpreterite authored Feb 25, 2022
1 parent cef5247 commit af66ee0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
22 changes: 20 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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}`);
Expand Down
7 changes: 4 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down

0 comments on commit af66ee0

Please sign in to comment.