-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathload-config.ts
53 lines (47 loc) · 1.41 KB
/
load-config.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
49
50
51
52
53
import {cosmiconfig} from 'cosmiconfig';
import {TypeScriptLoader} from 'cosmiconfig-typescript-loader';
import path from 'path';
export interface LoadConfigResult {
config: unknown;
filepath: string;
isEmpty?: boolean;
}
export async function loadConfig(
cwd: string,
configPath?: string
): Promise<LoadConfigResult | null> {
const moduleName = 'commitlint';
const tsLoader = TypeScriptLoader();
const explorer = cosmiconfig(moduleName, {
searchPlaces: [
// cosmiconfig overrides default searchPlaces if any new search place is added (For e.g. `*.ts` files),
// we need to manually merge default searchPlaces from https://github.com/davidtheclark/cosmiconfig#searchplaces
'package.json',
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`,
`.${moduleName}rc.cjs`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`,
// files supported by TypescriptLoader
`.${moduleName}rc.ts`,
`.${moduleName}rc.cts`,
`${moduleName}.config.ts`,
`${moduleName}.config.cts`,
],
loaders: {
'.ts': tsLoader,
'.cts': tsLoader,
},
});
const explicitPath = configPath ? path.resolve(cwd, configPath) : undefined;
const explore = explicitPath ? explorer.load : explorer.search;
const searchPath = explicitPath ? explicitPath : cwd;
const local = await explore(searchPath);
if (local) {
return local;
}
return null;
}