-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathload-parser-opts.ts
48 lines (43 loc) · 1.29 KB
/
load-parser-opts.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
export async function loadParserOpts(
parserName: string,
pendingParser: Promise<any>
) {
// 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;
}
// Create parser opts from factory
if (
typeof parser === 'object' &&
typeof parser.parserOpts === 'function' &&
parserName.startsWith('conventional-changelog-')
) {
return await new Promise((resolve) => {
const result = parser.parserOpts((_: never, opts: {parserOpts: any}) => {
resolve(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);
});
}
});
}
// 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;
}