forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (90 loc) · 2.81 KB
/
index.js
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
import path from 'path';
import executeRule from '@commitlint/execute-rule';
import resolveExtends from '@commitlint/resolve-extends';
import cosmiconfig from 'cosmiconfig';
import entries from 'lodash/toPairs';
import merge from 'lodash/merge';
import mergeWith from 'lodash/mergeWith';
import pick from 'lodash/pick';
import resolveFrom from 'resolve-from';
const w = (a, b) => (Array.isArray(b) ? b : undefined);
const valid = input =>
pick(input, 'extends', 'rules', 'parserPreset', 'formatter');
export default async (seed = {}, options = {cwd: process.cwd()}) => {
const loaded = await loadConfig(options.cwd, options.file);
const base = loaded.filepath ? path.dirname(loaded.filepath) : options.cwd;
// Merge passed config with file based options
const config = valid(merge(loaded.config, seed));
const opts = merge(
{extends: [], rules: {}, formatter: '@commitlint/format'},
pick(config, 'extends')
);
// Resolve parserPreset key
if (typeof config.parserPreset === 'string') {
const resolvedParserPreset = resolveFrom(base, config.parserPreset);
config.parserPreset = {
name: config.parserPreset,
path: resolvedParserPreset,
parserOpts: (await require(resolvedParserPreset)).parserOpts
};
}
// Resolve extends key
const extended = resolveExtends(opts, {
prefix: 'commitlint-config',
cwd: base,
parserPreset: config.parserPreset
});
const preset = valid(mergeWith(extended, config, w));
// Await parser-preset if applicable
if (
typeof preset.parserPreset === 'object' &&
typeof preset.parserPreset.parserOpts === 'object' &&
typeof preset.parserPreset.parserOpts.then === 'function'
) {
preset.parserPreset.parserOpts = (await preset.parserPreset
.parserOpts).parserOpts;
}
// Resolve config-relative formatter module
if (typeof config.formatter === 'string') {
preset.formatter =
resolveFrom.silent(base, config.formatter) || config.formatter;
}
// Execute rule config functions if needed
const executed = await Promise.all(
['rules']
.map(key => {
return [key, preset[key]];
})
.map(async item => {
const [key, value] = item;
const executedValue = await Promise.all(
entries(value || {}).map(entry => executeRule(entry))
);
return [
key,
executedValue.reduce((registry, item) => {
const [key, value] = item;
registry[key] = value;
return registry;
}, {})
];
})
);
// Merge executed config keys into preset
return executed.reduce((registry, item) => {
const [key, value] = item;
registry[key] = value;
return registry;
}, preset);
};
async function loadConfig(cwd, configPath) {
const explorer = cosmiconfig('commitlint', {
rcExtensions: true,
configPath: configPath ? path.resolve(cwd, configPath) : null
});
const local = await explorer.load(cwd);
if (local) {
return local;
}
return {};
}