forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
192 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,61 @@ | ||
export async function loadParserOpts( | ||
parserName: string, | ||
pendingParser: Promise<any> | ||
) { | ||
import {ParserPreset} from '@commitlint/types'; | ||
import { | ||
isObjectLike, | ||
isParserOptsFunction, | ||
isPromiseLike, | ||
validateParser, | ||
} from './validators'; | ||
|
||
export async function loadParser( | ||
pendingParser: unknown | ||
): Promise<ParserPreset | undefined> { | ||
if (!pendingParser) { | ||
return undefined; | ||
} | ||
// Await for the module, loaded with require | ||
const parser = await pendingParser; | ||
|
||
validateParser(parser); | ||
|
||
// Await parser opts if applicable | ||
if ( | ||
typeof parser === 'object' && | ||
typeof parser.parserOpts === 'object' && | ||
typeof parser.parserOpts.then === 'function' | ||
) { | ||
return (await parser.parserOpts).parserOpts; | ||
if (isPromiseLike(parser.parserOpts)) { | ||
parser.parserOpts = ((await parser.parserOpts) as any).parserOpts; | ||
return parser; | ||
} | ||
|
||
// Create parser opts from factory | ||
if ( | ||
typeof parser === 'object' && | ||
typeof parser.parserOpts === 'function' && | ||
parserName.startsWith('conventional-changelog-') | ||
isParserOptsFunction(parser) && | ||
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) => { | ||
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 | ||
// Pull nested parserOpts, might happen if overwritten with a module in main config | ||
if ( | ||
typeof parser === 'object' && | ||
typeof parser.parserOpts === 'object' && | ||
isObjectLike(parser.parserOpts) && | ||
typeof parser.parserOpts.parserOpts === 'object' | ||
) { | ||
return parser.parserOpts.parserOpts; | ||
parser.parserOpts = parser.parserOpts.parserOpts; | ||
} | ||
|
||
return parser.parserOpts; | ||
return parser; | ||
} |
Oops, something went wrong.