Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support page header setting of changelog file #42

Merged
merged 4 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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