-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathload.ts
141 lines (126 loc) Β· 3.61 KB
/
load.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import path from "node:path";
import { validateConfig } from "@commitlint/config-validator";
import executeRule from "@commitlint/execute-rule";
import resolveExtends, {
resolveFrom,
resolveFromSilent,
resolveGlobalSilent,
loadParserPreset,
} from "@commitlint/resolve-extends";
import {
LoadOptions,
PluginRecords,
QualifiedConfig,
QualifiedRules,
UserConfig,
} from "@commitlint/types";
import isPlainObject from "lodash.isplainobject";
import merge from "lodash.merge";
import uniq from "lodash.uniq";
import { loadConfig } from "./utils/load-config.js";
import { loadParserOpts } from "./utils/load-parser-opts.js";
import loadPlugin from "./utils/load-plugin.js";
/**
* formatter should be kept as is when unable to resolve it from config directory
*/
const resolveFormatter = (formatter: string, parent?: string): string => {
try {
return resolveFrom(formatter, parent);
} catch (error) {
return formatter;
}
};
export default async function load(
seed: UserConfig = {},
options: LoadOptions = {},
): Promise<QualifiedConfig> {
const cwd = typeof options.cwd === "undefined" ? process.cwd() : options.cwd;
const loaded = await loadConfig(cwd, options.file);
const baseDirectory = loaded?.filepath ? path.dirname(loaded.filepath) : cwd;
const configFilePath = loaded?.filepath;
let config: UserConfig = {};
if (loaded) {
validateConfig(loaded.filepath || "", loaded.config);
config = loaded.config;
}
// Merge passed config with file based options
config = merge(
{
extends: [],
plugins: [],
rules: {},
},
config,
seed,
);
// Resolve parserPreset key
if (typeof config.parserPreset === "string") {
const resolvedParserPreset = resolveFrom(
config.parserPreset,
configFilePath,
);
config.parserPreset = {
name: config.parserPreset,
...(await loadParserPreset(resolvedParserPreset)),
};
}
// Resolve extends key
const extended = await resolveExtends(config, {
prefix: "commitlint-config",
cwd: baseDirectory,
parserPreset: await config.parserPreset,
});
if (!extended.formatter || typeof extended.formatter !== "string") {
extended.formatter = "@commitlint/format";
}
let plugins: PluginRecords = {};
if (Array.isArray(extended.plugins)) {
for (const plugin of uniq(extended.plugins)) {
if (typeof plugin === "string") {
plugins = await loadPlugin(
plugins,
plugin,
process.env.DEBUG === "true",
);
} else {
plugins.local = plugin;
}
}
}
const rules = (
await Promise.all(
Object.entries(extended.rules || {}).map((entry) => executeRule(entry)),
)
).reduce<QualifiedRules>((registry, item) => {
// type of `item` can be null, but Object.entries always returns key pair
const [key, value] = item!;
registry[key] = value;
return registry;
}, {});
const helpUrl =
typeof extended.helpUrl === "string"
? extended.helpUrl
: typeof config.helpUrl === "string"
? config.helpUrl
: "https://github.com/conventional-changelog/commitlint/#what-is-commitlint";
const prompt =
extended.prompt && isPlainObject(extended.prompt) ? extended.prompt : {};
return {
extends: Array.isArray(extended.extends)
? extended.extends
: typeof extended.extends === "string"
? [extended.extends]
: [],
// Resolve config-relative formatter module
formatter: resolveFormatter(extended.formatter, configFilePath),
// Resolve parser-opts from preset
parserPreset: await loadParserOpts(extended.parserPreset),
ignores: extended.ignores,
defaultIgnores: extended.defaultIgnores,
plugins: plugins,
rules: rules,
helpUrl: helpUrl,
prompt,
};
}
export { resolveFrom, resolveFromSilent, resolveGlobalSilent };