-
Notifications
You must be signed in to change notification settings - Fork 909
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: simplify config resolution (#2398)
* feat: basic user config validation * fix: simplify config resolution and fix issue #327 * fix: remove no longer needed function * fix: disable some unwanted validations * fix: improve config validation * fix: remove redundant validation * fix: use reduceRight instead of reverse * fix: rollback some code * fix: drop invalid type casts * fix: rollback unnecessary changes * fix: rollback config validation * fix: add missing type-guards and restore order * fix: one more order change * fix: add one more missing type guard * fix: remove unused types reference * fix: add additional unit tests * fix: add additional regression tests - remove also unnecessary type check * fix: remove more unnecessary code changes * fix: correct order of merging plugins * fix: add missing type check * fix: remove invalid type check * fix: remove redundant code * fix: rollback some unnecessary changes * fix: optimize loadParserOpts
- Loading branch information
Showing
8 changed files
with
322 additions
and
126 deletions.
There are no files selected for viewing
This file contains 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 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 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 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; | ||
|
||
// 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 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.