forked from conventional-changelog/commitlint
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload.ts
190 lines (168 loc) Β· 4.74 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import path from 'path';
import {fileURLToPath, pathToFileURL} from 'url';
import {validateConfig} from '@commitlint/config-validator';
import executeRule from '@commitlint/execute-rule';
import resolveExtends from '@commitlint/resolve-extends';
import {
LoadOptions,
PluginRecords,
QualifiedConfig,
QualifiedRules,
UserConfig,
} from '@commitlint/types';
import {resolve} from 'import-meta-resolve';
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';
const dynamicImport = async <T>(id: string): Promise<T> => {
const imported = await import(
path.isAbsolute(id) ? pathToFileURL(id).toString() : id
);
return ('default' in imported && imported.default) || imported;
};
const resolveFrom = (parent: string, id: string) => {
let resolved: string | undefined;
let error: Error | undefined;
for (const suffix of ['', '.js', '.json', '/index.js', '/index.json']) {
try {
resolved = resolve(
id + suffix,
pathToFileURL(path.resolve(parent, '__test__.js')).toString()
);
if (/^file:/.test(resolved)) {
resolved = fileURLToPath(resolved);
}
break;
} catch (err) {
if (!error) {
error = err as Error;
}
}
}
if (resolved) {
return resolved;
}
throw (
error ||
Object.assign(new Error(`Cannot find module "${id}" from "${parent}"`), {
code: 'MODULE_NOT_FOUND',
})
);
};
const resolveParserPreset = async (resolvedParserPreset: string) => {
let finalParserPreset = resolvedParserPreset;
let finalParserOpts: unknown;
let finalError: Error | undefined;
for (const suffix of ['', '.js', '.json', '/index.js', '/index.json']) {
try {
finalParserOpts = await dynamicImport(resolvedParserPreset + suffix);
finalParserPreset = resolvedParserPreset + suffix;
break;
} catch (err) {
if (!finalError) {
finalError = err as Error;
}
}
}
if (finalError) {
throw finalError;
}
return {
path: `./${path.relative(process.cwd(), finalParserPreset)}`
.split(path.sep)
.join('/'),
parserOpts: finalParserOpts,
};
};
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 base = loaded && loaded.filepath ? path.dirname(loaded.filepath) : cwd;
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(base, config.parserPreset);
config.parserPreset = {
name: config.parserPreset,
...resolveParserPreset(resolvedParserPreset),
};
}
// Resolve extends key
const extended = await resolveExtends(config, {
prefix: 'commitlint-config',
cwd: base,
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: resolveFrom(base, extended.formatter) || extended.formatter,
// Resolve parser-opts from preset
parserPreset: await loadParserOpts(extended.parserPreset),
ignores: extended.ignores,
defaultIgnores: extended.defaultIgnores,
plugins: plugins,
rules: rules,
helpUrl: helpUrl,
prompt,
};
}