Skip to content

Instantly share code, notes, and snippets.

@Ankit1598
Created October 27, 2022 09:57
Show Gist options
  • Save Ankit1598/bc150f065809eb7e2e0cf5cea4ea1d62 to your computer and use it in GitHub Desktop.
Save Ankit1598/bc150f065809eb7e2e0cf5cea4ea1d62 to your computer and use it in GitHub Desktop.
Basic script to generate changelog for commits before conventional commits
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 firstCommit = child
.execSync(`git rev-list --max-parents=0 HEAD`)
.toString("utf-8").slice(0, -1);
const currentVersion = require("./package.json").version;
let newChangelog = `# [v${currentVersion}](https://github.com/AnkitC1598/admin/commit/${firstCommit}...${currentVersion}) (${new Date().toISOString().split("T")[0]})\n\n`;
const commits = [];
commitsArray.forEach(commit => {
if (commit.sha === firstCommit) commit.message = '1.0.0' + ` [${commit.message}]`
commits.push(
`* ${commit.message} ([${commit.sha.substring(
0,
6
)}](https://github.com/AnkitC1598/admin/commit/${commit.sha
}))\n`
);
}
);
if (commits.length) {
newChangelog += `## Commits\n`;
commits.forEach(commit => {
newChangelog += commit;
});
newChangelog += '\n';
}
fs.writeFileSync("./CHANGELOG.md", `${newChangelog}`);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment