-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathload-config.ts
110 lines (95 loc) · 2.94 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { existsSync, readFileSync } from "node:fs";
import path from "node:path";
import {
cosmiconfig,
defaultLoadersSync,
type Loader,
defaultLoaders,
} from "cosmiconfig";
import { TypeScriptLoader } from "cosmiconfig-typescript-loader";
export interface LoadConfigResult {
config: unknown;
filepath: string;
isEmpty?: boolean;
}
const moduleName = "commitlint";
const searchStrategy = "global";
export async function loadConfig(
cwd: string,
configPath?: string
): Promise<LoadConfigResult | null> {
let tsLoaderInstance: Loader | undefined;
const tsLoader: Loader = (...args) => {
if (!tsLoaderInstance) {
tsLoaderInstance = TypeScriptLoader();
}
return tsLoaderInstance(...args);
};
// If dynamic await is supported (Node >= v20.8.0) or directory uses ESM, support
// async js/cjs loaders (dynamic import). Otherwise, use synchronous js/cjs loaders.
const loaders =
isDynamicAwaitSupported() || isEsmModule(cwd)
? defaultLoaders
: defaultLoadersSync;
const explorer = cosmiconfig(moduleName, {
searchStrategy,
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
"deno.json",
"package.json",
"package.yaml",
`.${moduleName}rc`,
`.${moduleName}rc.json`,
`.${moduleName}rc.yaml`,
`.${moduleName}rc.yml`,
`.${moduleName}rc.js`,
`.${moduleName}rc.cjs`,
`.${moduleName}rc.mjs`,
`${moduleName}.config.js`,
`${moduleName}.config.cjs`,
`${moduleName}.config.mjs`,
// files supported by TypescriptLoader
`.${moduleName}rc.ts`,
`.${moduleName}rc.cts`,
`${moduleName}.config.ts`,
`${moduleName}.config.cts`,
],
loaders: {
".ts": tsLoader,
".cts": tsLoader,
".cjs": loaders[".cjs"],
".js": loaders[".js"],
},
});
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;
}
// See the following issues for more context, contributing to failing Jest tests:
// - Issue: https://github.com/nodejs/node/issues/40058
// - Resolution: https://github.com/nodejs/node/pull/48510 (Node v20.8.0)
export const isDynamicAwaitSupported = () => {
const [major, minor] = process.version
.replace("v", "")
.split(".")
.map((val) => parseInt(val));
return major >= 20 && minor >= 8;
};
// Is the given directory set up to use ESM (ECMAScript Modules)?
export const isEsmModule = (cwd: string) => {
const packagePath = path.join(cwd, "package.json");
if (!existsSync(packagePath)) {
return false;
}
const packageJSON = readFileSync(packagePath, { encoding: "utf-8" });
return (
JSON.parse(packageJSON)?.type === "module" ||
existsSync(path.join(cwd, "deno.json"))
);
};