-
Notifications
You must be signed in to change notification settings - Fork 933
/
Copy pathload.ts
130 lines (110 loc) Β· 3.73 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
import Path from 'path';
import merge from 'lodash/merge';
import mergeWith from 'lodash/mergeWith';
import pick from 'lodash/pick';
import union from 'lodash/union';
import resolveFrom from 'resolve-from';
import executeRule from '@commitlint/execute-rule';
import resolveExtends from '@commitlint/resolve-extends';
import {
UserConfig,
LoadOptions,
QualifiedConfig,
UserPreset,
QualifiedRules,
ParserPreset,
} from '@commitlint/types';
import loadPlugin from './utils/load-plugin';
import {loadConfig} from './utils/load-config';
import {loadParserOpts} from './utils/load-parser-opts';
import {pickConfig} from './utils/pick-config';
const w = <T>(_: unknown, b: ArrayLike<T> | null | undefined | false) =>
Array.isArray(b) ? b : undefined;
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;
// TODO: validate loaded.config against UserConfig type
// Might amount to breaking changes, defer until 9.0.0
// Merge passed config with file based options
const config = pickConfig(merge({}, loaded ? loaded.config : null, seed));
const opts = merge(
{extends: [], rules: {}, formatter: '@commitlint/format'},
pick(config, 'extends', 'plugins', 'ignores', 'defaultIgnores')
);
// Resolve parserPreset key
if (typeof config.parserPreset === 'string') {
const resolvedParserPreset = resolveFrom(base, config.parserPreset);
config.parserPreset = {
name: config.parserPreset,
path: resolvedParserPreset,
parserOpts: require(resolvedParserPreset),
};
}
// Resolve extends key
const extended = resolveExtends(opts, {
prefix: 'commitlint-config',
cwd: base,
parserPreset: config.parserPreset,
});
const preset = (pickConfig(
mergeWith(extended, config, w)
) as unknown) as UserPreset;
preset.plugins = {};
// TODO: check if this is still necessary with the new factory based conventional changelog parsers
// config.extends = Array.isArray(config.extends) ? config.extends : [];
// Resolve parser-opts from preset
if (typeof preset.parserPreset === 'object') {
preset.parserPreset.parserOpts = await loadParserOpts(
preset.parserPreset.name,
// TODO: fix the types for factory based conventional changelog parsers
preset.parserPreset as any
);
}
// Resolve config-relative formatter module
if (typeof config.formatter === 'string') {
preset.formatter =
resolveFrom.silent(base, config.formatter) || config.formatter;
}
// Read plugins from extends
if (Array.isArray(extended.plugins)) {
config.plugins = union(config.plugins, extended.plugins || []);
}
// resolve plugins
if (Array.isArray(config.plugins)) {
config.plugins.forEach((plugin) => {
if (typeof plugin === 'string') {
loadPlugin(preset.plugins, plugin, process.env.DEBUG === 'true');
} else {
preset.plugins.local = plugin;
}
});
}
const rules = preset.rules ? preset.rules : {};
const qualifiedRules = (
await Promise.all(
Object.entries(rules || {}).map((entry) => executeRule<any>(entry))
)
).reduce<QualifiedRules>((registry, item) => {
const [key, value] = item as any;
(registry as any)[key] = value;
return registry;
}, {});
const helpUrl =
typeof config.helpUrl === 'string'
? config.helpUrl
: 'https://github.com/conventional-changelog/commitlint/#what-is-commitlint';
return {
extends: preset.extends!,
formatter: preset.formatter!,
parserPreset: preset.parserPreset! as ParserPreset,
ignores: preset.ignores!,
defaultIgnores: preset.defaultIgnores!,
plugins: preset.plugins!,
rules: qualifiedRules,
helpUrl,
};
}