From 4b5300a1494636e8977d57d2fa1e8dd20318779d Mon Sep 17 00:00:00 2001 From: Cedric van Putten Date: Wed, 16 Oct 2019 21:27:53 +0200 Subject: [PATCH] feat(config-conventional): use parser with short breaking change support (#821) * feat(load): add parser preset factory support The latest conventional changelog parser options is loaded using a secondary promise. Load it before using the parserOpts property. * refactor(config-conventional): use preset with short breaking change * fix(config-conventional): define new parser as dependency --- @commitlint/config-conventional/index.js | 1 + @commitlint/config-conventional/package.json | 3 +++ .../parser-preset-factory/commitlint.config.js | 5 +++++ .../conventional-changelog-factory.js | 10 ++++++++++ @commitlint/load/src/index.js | 8 +++++++- @commitlint/load/src/index.test.js | 14 ++++++++++++++ yarn.lock | 8 ++++++++ 7 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 @commitlint/load/fixtures/parser-preset-factory/commitlint.config.js create mode 100644 @commitlint/load/fixtures/parser-preset-factory/conventional-changelog-factory.js diff --git a/@commitlint/config-conventional/index.js b/@commitlint/config-conventional/index.js index f26ad5a57f..83a4c24847 100644 --- a/@commitlint/config-conventional/index.js +++ b/@commitlint/config-conventional/index.js @@ -1,4 +1,5 @@ module.exports = { + parserPreset: 'conventional-changelog-conventionalcommits', rules: { 'body-leading-blank': [1, 'always'], 'footer-leading-blank': [1, 'always'], diff --git a/@commitlint/config-conventional/package.json b/@commitlint/config-conventional/package.json index df6174a42d..e947cda843 100644 --- a/@commitlint/config-conventional/package.json +++ b/@commitlint/config-conventional/package.json @@ -33,5 +33,8 @@ "homepage": "https://github.com/conventional-changelog/commitlint#readme", "devDependencies": { "@commitlint/utils": "^8.2.0" + }, + "dependencies": { + "conventional-changelog-conventionalcommits": "^4.1.0" } } diff --git a/@commitlint/load/fixtures/parser-preset-factory/commitlint.config.js b/@commitlint/load/fixtures/parser-preset-factory/commitlint.config.js new file mode 100644 index 0000000000..f3dd71ee2a --- /dev/null +++ b/@commitlint/load/fixtures/parser-preset-factory/commitlint.config.js @@ -0,0 +1,5 @@ +module.exports = { + parserOpts: { + parserPreset: './conventional-changelog-factory' + } +}; diff --git a/@commitlint/load/fixtures/parser-preset-factory/conventional-changelog-factory.js b/@commitlint/load/fixtures/parser-preset-factory/conventional-changelog-factory.js new file mode 100644 index 0000000000..7ac0b81719 --- /dev/null +++ b/@commitlint/load/fixtures/parser-preset-factory/conventional-changelog-factory.js @@ -0,0 +1,10 @@ +module.exports = Promise.resolve().then( + () => + function factory() { + return { + parserOpts: { + headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/ + } + }; + } +); diff --git a/@commitlint/load/src/index.js b/@commitlint/load/src/index.js index 1fb4c789f5..6845172940 100644 --- a/@commitlint/load/src/index.js +++ b/@commitlint/load/src/index.js @@ -33,11 +33,17 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => { // Resolve parserPreset key if (typeof config.parserPreset === 'string') { const resolvedParserPreset = resolveFrom(base, config.parserPreset); + let resolvedParserConfig = await require(resolvedParserPreset); + + // Resolve loaded parser preset if its a factory + if (typeof resolvedParserConfig === 'function') { + resolvedParserConfig = await resolvedParserConfig(); + } config.parserPreset = { name: config.parserPreset, path: resolvedParserPreset, - parserOpts: (await require(resolvedParserPreset)).parserOpts + parserOpts: resolvedParserConfig.parserOpts }; } diff --git a/@commitlint/load/src/index.test.js b/@commitlint/load/src/index.test.js index 4dbf31444b..c2b16719fa 100644 --- a/@commitlint/load/src/index.test.js +++ b/@commitlint/load/src/index.test.js @@ -84,6 +84,20 @@ test('uses seed with parserPreset', async t => { }); }); +test('uses seed with parserPreset factory', async t => { + const cwd = await git.bootstrap('fixtures/parser-preset-factory'); + const {parserPreset: actual} = await load( + { + parserPreset: './conventional-changelog-factory' + }, + {cwd} + ); + t.is(actual.name, './conventional-changelog-factory'); + t.deepEqual(actual.parserOpts, { + headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/ + }); +}); + test('invalid extend should throw', async t => { const cwd = await git.bootstrap('fixtures/extends-invalid'); await t.throws(load({}, {cwd})); diff --git a/yarn.lock b/yarn.lock index 1788822a8d..b4431e1e00 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3707,6 +3707,14 @@ conventional-changelog-angular@^5.0.3: compare-func "^1.3.1" q "^1.5.1" +conventional-changelog-conventionalcommits@^4.1.0: + version "4.1.0" + resolved "https://registry.npmjs.org/conventional-changelog-conventionalcommits/-/conventional-changelog-conventionalcommits-4.1.0.tgz#eb7d47a9c5f1a6f9846a649482294e4ac50d7683" + integrity sha512-J3xolGrH8PTxpCqueHOuZtv3Cp73SQOWiBQzlsaugZAZ+hZgcJBonmC+1bQbfGs2neC2S18p2L1Gx+nTEglJTQ== + dependencies: + compare-func "^1.3.1" + q "^1.5.1" + conventional-changelog-core@^3.1.6: version "3.1.6" resolved "https://registry.npmjs.org/conventional-changelog-core/-/conventional-changelog-core-3.1.6.tgz#ac1731a461c50d150d29c1ad4f33143293bcd32f"