-
-
Notifications
You must be signed in to change notification settings - Fork 152
/
config-loader.ts
57 lines (55 loc) · 1.48 KB
/
config-loader.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
54
55
56
57
import { TextLintModuleResolver } from "../engine/textlint-module-resolver";
import { moduleInterop } from "@textlint/module-interop";
import { rcFile } from "rc-config-loader";
/**
* @param configFileName "textlint" for .textlinrc
* @param cwd current working dir
* @param configFilePath it is preferred than configFileName
* @param moduleResolver
*/
export function loadConfig({
cwd,
configFileName,
configFilePath,
moduleResolver
}: {
cwd?: string;
configFileName: string;
configFilePath?: string;
moduleResolver: TextLintModuleResolver;
}): {
config: { [index: string]: any };
filePath: string | undefined;
} {
// if specify Config module, use it
if (configFilePath) {
try {
const modulePath = moduleResolver.resolveConfigPackageName(configFilePath);
return {
config: moduleInterop(require(modulePath)),
filePath: modulePath
};
} catch (error) {
// not found config module
}
}
// auto or specify path to config file
const result = rcFile(configFileName, {
configFileName: configFilePath,
defaultExtension: [".json", ".js", ".yml"],
packageJSON: {
fieldName: "textlint"
},
cwd
});
if (result === undefined) {
return {
config: {},
filePath: undefined
};
}
return {
config: result.config,
filePath: result.filePath
};
}