-
Notifications
You must be signed in to change notification settings - Fork 927
feat: simplify config resolution #2398
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
Merged
escapedcat
merged 25 commits into
conventional-changelog:master
from
armano2:refactor/load
Nov 16, 2021
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d315e02
feat: basic user config validation
armano2 a9477b8
fix: simplify config resolution and fix issue #327
armano2 01ef361
fix: remove no longer needed function
armano2 e09fc01
fix: disable some unwanted validations
armano2 26612a8
fix: improve config validation
armano2 fb7064c
fix: remove redundant validation
armano2 b0d4829
fix: use reduceRight instead of reverse
armano2 ff67ad7
fix: rollback some code
armano2 3bd8384
fix: drop invalid type casts
armano2 7fcf5e0
fix: rollback unnecessary changes
armano2 5a10589
fix: rollback config validation
armano2 b2af2e0
fix: add missing type-guards and restore order
armano2 a165e15
fix: one more order change
armano2 28185b6
fix: add one more missing type guard
armano2 0c75eb9
fix: remove unused types reference
armano2 45d1e25
fix: add additional unit tests
armano2 75d9011
fix: add additional regression tests
armano2 bd6e958
fix: remove more unnecessary code changes
armano2 9303234
fix: correct order of merging plugins
armano2 1e20607
fix: add missing type check
armano2 b750743
fix: remove invalid type check
armano2 1c251d2
fix: remove redundant code
armano2 f1bf2d2
fix: rollback some unnecessary changes
armano2 2c32314
fix: optimize loadParserOpts
armano2 19b76c1
Merge branch 'master' into refactor/load
armano2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,71 @@ | ||
import {ParserPreset} from '@commitlint/types'; | ||
|
||
function isObjectLike(obj: unknown): obj is Record<string, unknown> { | ||
return Boolean(obj) && typeof obj === 'object'; // typeof null === 'object' | ||
} | ||
|
||
function isParserOptsFunction<T extends ParserPreset>( | ||
obj: T | ||
): obj is T & { | ||
parserOpts: (...args: any[]) => any; | ||
} { | ||
return typeof obj.parserOpts === 'function'; | ||
} | ||
|
||
export async function loadParserOpts( | ||
parserName: string, | ||
pendingParser: Promise<any> | ||
) { | ||
pendingParser: string | ParserPreset | Promise<ParserPreset> | undefined | ||
): Promise<ParserPreset | undefined> { | ||
if (!pendingParser || typeof pendingParser !== 'object') { | ||
return undefined; | ||
} | ||
// Await for the module, loaded with require | ||
const parser = await pendingParser; | ||
armano2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// Await parser opts if applicable | ||
if ( | ||
typeof parser === 'object' && | ||
typeof parser.parserOpts === 'object' && | ||
typeof parser.parserOpts.then === 'function' | ||
) { | ||
return (await parser.parserOpts).parserOpts; | ||
// exit early, no opts to resolve | ||
if (!parser.parserOpts) { | ||
return parser; | ||
} | ||
|
||
// Pull nested parserOpts, might happen if overwritten with a module in main config | ||
if (typeof parser.parserOpts === 'object') { | ||
// Await parser opts if applicable | ||
parser.parserOpts = await parser.parserOpts; | ||
if ( | ||
isObjectLike(parser.parserOpts) && | ||
isObjectLike(parser.parserOpts.parserOpts) | ||
) { | ||
parser.parserOpts = parser.parserOpts.parserOpts; | ||
} | ||
return parser; | ||
} | ||
|
||
// Create parser opts from factory | ||
if ( | ||
typeof parser === 'object' && | ||
typeof parser.parserOpts === 'function' && | ||
parserName.startsWith('conventional-changelog-') | ||
isParserOptsFunction(parser) && | ||
typeof parser.name === 'string' && | ||
parser.name.startsWith('conventional-changelog-') | ||
) { | ||
return await new Promise((resolve) => { | ||
const result = parser.parserOpts((_: never, opts: {parserOpts: any}) => { | ||
resolve(opts.parserOpts); | ||
return new Promise((resolve) => { | ||
const result = parser.parserOpts((_: never, opts: any) => { | ||
resolve({ | ||
...parser, | ||
parserOpts: opts?.parserOpts, | ||
}); | ||
}); | ||
|
||
// If result has data or a promise, the parser doesn't support factory-init | ||
// due to https://github.com/nodejs/promises-debugging/issues/16 it just quits, so let's use this fallback | ||
if (result) { | ||
Promise.resolve(result).then((opts) => { | ||
resolve(opts.parserOpts); | ||
resolve({ | ||
...parser, | ||
parserOpts: opts?.parserOpts, | ||
}); | ||
}); | ||
} | ||
return; | ||
}); | ||
} | ||
|
||
// Pull nested paserOpts, might happen if overwritten with a module in main config | ||
if ( | ||
typeof parser === 'object' && | ||
typeof parser.parserOpts === 'object' && | ||
typeof parser.parserOpts.parserOpts === 'object' | ||
) { | ||
return parser.parserOpts.parserOpts; | ||
} | ||
|
||
return parser.parserOpts; | ||
return parser; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.