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): add support for factory-callback parser presets #834

Merged
merged 3 commits into from
Oct 21, 2019

Conversation

byCedric
Copy link
Member

@byCedric byCedric commented Oct 17, 2019

Description

Fixes #830

Motivation and Context

Not all presets worked flawlessly with the existing load-preset code. The code had no support for presets that required a initialise-callback. I'm not sure if the "example atom preset" from here worked because of this. But this PR helps solving that issue.

We could also integrate the conventional-changelog-loader into @commitlint/load, didn't have much luck with that but I could try implementing it again. I do have some doubt if it will remove some of the code already added in this PR, because we still have to initialise it.

Usage examples

How Has This Been Tested?

Instead of creating similar-structured presets, I added the actual preset modules in the tests. So far, I could find 3 different types of presets.

  • presets that return a promise with an object: { parserOpts: {} }, like angular
  • presets that return a method with a required callback, containing: (null, { parserOpts: {} }), like atom
  • presets that return a method with an optional callback (or returns a promise that with parserOpts as function), like conventionalcommits

All these changelogs are now feature-tested and included in the PR.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • All new and existing tests passed.


config.parserPreset = {
name: config.parserPreset,
path: resolvedParserPreset,
parserOpts: resolvedParserConfig.parserOpts
parserOpts: require(resolvedParserPreset)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The code before the first attempt was (await require(resolvedParserPreset)).parserOpts, which is not going to work with the newer callback-presets. I took a look at @commitlint/resolve-extended and it does exactly the same as written here, I think that's a better way-to-go compared to the instant-extraction of parserOpts (like the code I just mentioned).

typeof parser.parserOpts === 'object' &&
typeof parser.parserOpts.parserOpts === 'object'
) {
return parser.parserOpts.parserOpts;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how this "happens", but I think it's related to the context-overwrite when calling @commitlint/resolve-extended. The failing test was the parser-preset-override, I don't think anything is wrong with that tests though.

What I think what happens:

  • commitlint.config.js overwrites parserPreset with a module
  • overwrite is picked up and module is loaded
  • due to this change, it's not using parserOpts but the parser preset itself.

'fixtures/recursive-parser-preset-conventional-atom'
);
// the package file is nested in 2 folders, `npm.bootstrap` cant do that
await execa('npm', ['install'], {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is any other way, let me know 😄 I think this is not that bad, but I didn't see any other option for now.

@marionebl marionebl merged commit 492d330 into master Oct 21, 2019
@byCedric byCedric deleted the refactor/load-preset branch October 22, 2019 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

When extending config the parser preset is not loaded
2 participants