From 6808b236018b7efc05d147272dbb2e76c7117768 Mon Sep 17 00:00:00 2001 From: Taras Kuprunets Date: Wed, 29 Sep 2021 16:08:19 +0300 Subject: [PATCH] Feat add parser opts #821 --- README.md | 20 ++++++++++++++++++++ command.js | 4 ++++ index.js | 9 +++++++++ lib/lifecycles/bump.js | 2 +- lib/lifecycles/changelog.js | 2 +- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index f924f6073..3af84ae61 100644 --- a/README.md +++ b/README.md @@ -313,6 +313,26 @@ If you do not want to have any tag prefix you can use the `-t` flag and provide > Note: simply -t or --tag-prefix without any value will fallback to the default 'v' +### Parser Options + +If you need to include custom parser options to read your commits, you can use the `--parser-opts` to indicate the path to a .js file that exports the options. + +```sh +standard-version --parser-options ./parser-opts.js +``` + +This can be useful in cases like Azure DevOps that uses custom merge pull request titles. The `parser-opts.js` could look like this: + +```js +/** @type {import("conventional-commits-parser").Options} */ +module.exports = { + mergePattern: /^Merged PR (\d+): (\w*)(?:\(([\w\$\.\-\* ]*)\))?\: (.*)$/, + mergeCorrespondence: ['id', 'type', 'scope', 'subject'] +} +``` + +For more informantion on what parser options are available, reference to [documentation](https://github.com/conventional-changelog/conventional-changelog/tree/master/packages/conventional-commits-parser). + ### CLI Help ```sh diff --git a/command.js b/command.js index d65d5ea06..5d44798e3 100755 --- a/command.js +++ b/command.js @@ -99,6 +99,10 @@ const yargs = require('yargs') default: defaults.preset, describe: 'Commit message guideline preset' }) + .option('parser-opts', { + type: 'string', + describe: 'Path to file with Conventional Commits Parser Options' + }) .option('lerna-package', { type: 'string', describe: 'Name of the package from which the tags will be extracted' diff --git a/index.js b/index.js index 3a35555fc..91ff4c61f 100755 --- a/index.js +++ b/index.js @@ -37,6 +37,15 @@ module.exports = async function standardVersion (argv) { throw Error(`custom changelog header must not match ${changelog.START_OF_LAST_RELEASE_PATTERN}`) } + if (typeof argv.parserOpts === 'string') { + const parserOptsPath = path.resolve(argv.parserOpts) + try { + argv.parserOpts = require(parserOptsPath) + } catch(err) { + argv.parserOpts = null + } + } + const args = Object.assign({}, defaults, argv) let pkg for (const packageFile of args.packageFiles) { diff --git a/lib/lifecycles/bump.js b/lib/lifecycles/bump.js index 370fbff03..f13641e5a 100644 --- a/lib/lifecycles/bump.js +++ b/lib/lifecycles/bump.js @@ -122,7 +122,7 @@ function bumpVersion (releaseAs, currentVersion, args) { path: args.path, tagPrefix: args.tagPrefix, lernaPackage: args.lernaPackage - }, function (err, release) { + }, args.parserOpts, function (err, release) { if (err) return reject(err) else return resolve(release) }) diff --git a/lib/lifecycles/changelog.js b/lib/lifecycles/changelog.js index b52f529c0..b521b499f 100644 --- a/lib/lifecycles/changelog.js +++ b/lib/lifecycles/changelog.js @@ -36,7 +36,7 @@ function outputChangelog (args, newVersion) { debug: args.verbose && console.info.bind(console, 'conventional-changelog'), preset: presetLoader(args), tagPrefix: args.tagPrefix - }, context, { merges: null, path: args.path }) + }, context, { merges: null, path: args.path }, args.parserOpts) .on('error', function (err) { return reject(err) })