Skip to content

Instantly share code, notes, and snippets.

@jackyef
Created October 20, 2019 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jackyef/5cdfaf39d54002e6dede630808dec0ba to your computer and use it in GitHub Desktop.
Save jackyef/5cdfaf39d54002e6dede630808dec0ba to your computer and use it in GitHub Desktop.
const child = require("child_process");
const fs = require("fs");
const output = child
.execSync(`git log --format=%B%H----DELIMITER----`)
.toString("utf-8");
const commitsArray = output
.split("----DELIMITER----\n")
.map(commit => {
const [message, sha] = commit.split("\n");
return { sha, message };
})
.filter(commit => Boolean(commit.sha));
const currentChangelog = fs.readFileSync("./CHANGELOG.md", "utf-8");
const currentVersion = Number(require("./package.json").version);
const newVersion = currentVersion + 1;
let newChangelog = `# Version ${newVersion} (${
new Date().toISOString().split("T")[0]
})\n\n`;
const features = [];
const chores = [];
commitsArray.forEach(commit => {
if (commit.message.startsWith("feature: ")) {
features.push(
`* ${commit.message.replace("feature: ", "")} ([${commit.sha.substring(
0,
6
)}](https://github.com/jackyef/changelog-generator/commit/${
commit.sha
}))\n`
);
}
if (commit.message.startsWith("chore: ")) {
chores.push(
`* ${commit.message.replace("chore: ", "")} ([${commit.sha.substring(
0,
6
)}](https://github.com/jackyef/changelog-generator/commit/${
commit.sha
}))\n`
);
}
});
if (features.length) {
newChangelog += `## Features\n`;
features.forEach(feature => {
newChangelog += feature;
});
newChangelog += '\n';
}
if (chores.length) {
newChangelog += `## Chores\n`;
chores.forEach(chore => {
newChangelog += chore;
});
newChangelog += '\n';
}
// prepend the newChangelog to the current one
fs.writeFileSync("./CHANGELOG.md", `${newChangelog}${currentChangelog}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment