Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(load): resolve nested parser preset factories #831

Merged
merged 4 commits into from
Oct 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./first-extended']
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
extends: ['./second-extended']
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.exports = Promise.resolve().then(
() =>
function factory() {
return {
parserOpts: {
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
}
};
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
parserPreset: './conventional-changelog-factory'
};
14 changes: 10 additions & 4 deletions @commitlint/load/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
pick(config, 'extends', 'plugins', 'ignores', 'defaultIgnores')
);

// Resolve parserPreset key
// Resolve parserPreset key from flat-non-extended config
if (typeof config.parserPreset === 'string') {
const resolvedParserPreset = resolveFrom(base, config.parserPreset);
let resolvedParserConfig = await require(resolvedParserPreset);

// Resolve loaded parser preset if its a factory
// Resolve loaded parser preset factory
if (typeof resolvedParserConfig === 'function') {
resolvedParserConfig = await resolvedParserConfig();
}
Expand All @@ -61,8 +61,14 @@ export default async (seed = {}, options = {cwd: process.cwd()}) => {
typeof preset.parserPreset.parserOpts === 'object' &&
typeof preset.parserPreset.parserOpts.then === 'function'
) {
preset.parserPreset.parserOpts = (await preset.parserPreset
.parserOpts).parserOpts;
let parserPreset = await preset.parserPreset.parserOpts;

// Resolve loaded parser preset factory from extended config
if (typeof parserPreset === 'function') {
parserPreset = await parserPreset();
}

preset.parserPreset.parserOpts = parserPreset.parserOpts;
}

// Resolve config-relative formatter module
Expand Down
10 changes: 10 additions & 0 deletions @commitlint/load/src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,16 @@ test('recursive extends with parserPreset', async t => {
);
});

test('recursive extends with parserPreset factory', async t => {
const cwd = await git.bootstrap('fixtures/recursive-parser-preset-factory');
const actual = await load({}, {cwd});

t.is(actual.parserPreset.name, './conventional-changelog-factory');
t.deepEqual(actual.parserPreset.parserOpts, {
headerPattern: /^(\w*)(?:\((.*)\))?-(.*)$/
});
});

test('ignores unknow keys', async t => {
const cwd = await git.bootstrap('fixtures/trash-file');
const actual = await load({}, {cwd});
Expand Down