Skip to content

Instantly share code, notes, and snippets.

@crutch12
Last active May 13, 2022 13:01
Show Gist options
  • Save crutch12/830cade83e415370fb74fab55f7e58b9 to your computer and use it in GitHub Desktop.
Save crutch12/830cade83e415370fb74fab55f7e58b9 to your computer and use it in GitHub Desktop.
const execSync = require('child_process').execSync;
const fs = require('fs');
const webpack = require('webpack');
const marked = require('marked');
module.exports = (env, argv) => {
return {
plugins: [
// @NOTE: Generate CHANGELOG.md (from git log + current CHANGELOG.md), put it in bundle
env.NODE_ENV === 'production' && {
apply: (compiler) => {
const { RawSource } = webpack.sources;
compiler.hooks.thisCompilation.tap('GitLogPlugin', (compilation) => {
compilation.hooks.processAssets.tap(
{
name: 'GitLogPlugin',
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONAL,
},
() => {
let changelog = '';
try {
changelog = fs.readFileSync('./CHANGELOG.md').toString();
} catch (err) {
console.error(`Couldn't read file ${'./CHANGELOG.md'}`);
return;
}
try {
// Last git tag
const gitTag = execSync('git tag --sort=-taggerdate').toString().split('\n')[0];
// Last git log
const gitLog = execSync(`git log --pretty="* %s" --no-merges HEAD...${gitTag}`);
// Final changelog
changelog = `# New since last (${gitTag}) release\n\n${gitLog}\n${changelog}`;
} catch (err) {
console.error(err);
console.error('Couldn\'t generate git log');
} finally {
// @NOTE: Put final CHANGELOG.md in bundle
compilation.emitAsset('CHANGELOG.md', new RawSource(changelog));
// @NOTE: Generate .html for comfortable reading
compilation.emitAsset('CHANGELOG.html', new RawSource(marked.parse(changelog)));
}
},
);
});
},
},
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment