Skip to content

Commit

Permalink
Add a Plugin system (#237)
Browse files Browse the repository at this point in the history
* add import-cwd dependency

* add --plugin option to cli

* call plugins functions during parseReleases

* Tidy up

---------

Co-authored-by: Pete Cook <[email protected]>
  • Loading branch information
mansona and cookpete authored May 15, 2023
1 parent 91226a7 commit 37cdfed
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ Options:
--append-git-tag [string] # string to append to git tag command
--prepend # prepend changelog to output file
--stdout # output changelog to stdout
--plugins [...name] # use plugins to augment commit/merge/release information
-V, --version # output the version number
-h, --help # output usage information

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"dependencies": {
"commander": "^7.2.0",
"handlebars": "^4.7.7",
"import-cwd": "^3.0.0",
"node-fetch": "^2.6.1",
"parse-github-url": "^1.0.2",
"semver": "^7.3.5"
Expand Down
12 changes: 12 additions & 0 deletions src/releases.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ const parseReleases = async (tags, options, onParsed) => {
const commits = await fetchCommits(tag.diff, options)
const merges = commits.filter(commit => commit.merge).map(commit => commit.merge)
const fixes = commits.filter(commit => commit.fixes).map(commit => ({ fixes: commit.fixes, commit }))

for (const plugin of options.plugins) {
if (plugin.processCommits) await plugin.processCommits(commits)
if (plugin.processMerges) await plugin.processMerges(merges)
if (plugin.processFixes) await plugin.processFixes(merges)
}

const emptyRelease = merges.length === 0 && fixes.length === 0
const { message } = commits[0] || { message: null }
const breakingCount = commits.filter(c => c.breaking).length
Expand All @@ -27,6 +34,11 @@ const parseReleases = async (tags, options, onParsed) => {
fixes
}
}))

for (const plugin of options.plugins) {
if (plugin.processReleases) await plugin.processReleases(releases)
}

return releases.filter(filterReleases(options))
}

Expand Down
8 changes: 6 additions & 2 deletions src/run.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { Command } = require('commander')
const importCwd = require('import-cwd')
const { version } = require('../package.json')
const { fetchRemote } = require('./remote')
const { fetchTags } = require('./tags')
Expand All @@ -16,7 +17,8 @@ const DEFAULT_OPTIONS = {
sortCommits: 'relevance',
appendGitLog: '',
appendGitTag: '',
config: '.auto-changelog'
config: '.auto-changelog',
plugins: []
}

const PACKAGE_FILE = 'package.json'
Expand Down Expand Up @@ -58,6 +60,7 @@ const getOptions = async argv => {
.option('--append-git-tag <string>', 'string to append to git tag command')
.option('--prepend', 'prepend changelog to output file')
.option('--stdout', 'output changelog to stdout')
.option('--plugins [name...]', 'use plugins to augment commit/merge/release information')
.version(version)
.parse(argv)
.opts()
Expand All @@ -76,7 +79,8 @@ const getOptions = async argv => {
return {
...options,
...remote,
latestVersion
latestVersion,
plugins: options.plugins.map(p => importCwd(`auto-changelog-${p}`))
}
}

Expand Down
14 changes: 14 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1377,6 +1377,13 @@ ignore@~5.1.9:
resolved "https://registry.yarnpkg.com/ignore/-/ignore-5.1.9.tgz#9ec1a5cbe8e1446ec60d4420060d43aa6e7382fb"
integrity sha512-2zeMQpbKz5dhZ9IwL0gbxSW5w0NK/MSAMtNuhgIHEPmaU3vPdKPL0UdvUCXs5SS4JAwsBxysK5sFMW8ocFiVjQ==

import-cwd@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/import-cwd/-/import-cwd-3.0.0.tgz#20845547718015126ea9b3676b7592fb8bd4cf92"
integrity sha512-4pnzH16plW+hgvRECbDWpQl3cqtvSofHWh44met7ESfZ8UZOWWddm8hEyDTqREJ9RbYHY8gi8DqmaelApoOGMg==
dependencies:
import-from "^3.0.0"

import-fresh@^3.0.0, import-fresh@^3.2.1:
version "3.3.0"
resolved "https://registry.yarnpkg.com/import-fresh/-/import-fresh-3.3.0.tgz#37162c25fcb9ebaa2e6e53d5b4d88ce17d9e0c2b"
Expand All @@ -1385,6 +1392,13 @@ import-fresh@^3.0.0, import-fresh@^3.2.1:
parent-module "^1.0.0"
resolve-from "^4.0.0"

import-from@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/import-from/-/import-from-3.0.0.tgz#055cfec38cd5a27d8057ca51376d7d3bf0891966"
integrity sha512-CiuXOFFSzkU5x/CR0+z7T91Iht4CXgfCxVOFRhh2Zyhg5wOpWvvDLQUsWl+gcN+QscYBjez8hDCt85O7RLDttQ==
dependencies:
resolve-from "^5.0.0"

imurmurhash@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/imurmurhash/-/imurmurhash-0.1.4.tgz#9218b9b2b928a238b13dc4fb6b6d576f231453ea"
Expand Down

0 comments on commit 37cdfed

Please sign in to comment.