forked from conventional-changelog/commitlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
120 lines (100 loc) Β· 3.26 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import path from 'path';
import 'resolve-global'; // eslint-disable-line import/no-unassigned-import
import requireUncached from 'require-uncached';
import resolveFrom from 'resolve-from';
import merge from 'lodash/merge';
import omit from 'lodash/omit';
// Resolve extend configs
export default function resolveExtends(config = {}, context = {}) {
const {extends: e} = config;
const extended = loadExtends(config, context).reduceRight(
(r, c) => merge(r, omit(c, 'extends')),
e ? {extends: e} : {}
);
// Remove deprecation warning in version 3
if (typeof config === 'object' && 'wildcards' in config) {
console.warn(
`'wildcards' found in top-level configuration ignored. Remove them from your config to silence this warning.`
);
}
return merge({}, extended, config);
}
// (any, string, string, Function) => any[];
function loadExtends(config = {}, context = {}) {
return (config.extends || []).reduce((configs, raw) => {
const load = context.require || require;
const resolved = resolveConfig(raw, context);
const c = load(resolved);
const cwd = path.dirname(resolved);
// Remove deprecation warning in version 3
if (typeof c === 'object' && 'wildcards' in c) {
console.warn(
`'wildcards' found in '${raw}' ignored. To silence this warning raise an issue at 'npm repo ${raw}' to remove the wildcards.`
);
}
const ctx = merge({}, context, {cwd});
// Resolve parser preset if none was present before
if (
!context.parserPreset &&
typeof c === 'object' &&
typeof c.parserPreset === 'string'
) {
const resolvedParserPreset = resolveFrom(cwd, c.parserPreset);
const parserPreset = {
name: c.parserPreset,
path: `./${path.relative(process.cwd(), resolvedParserPreset)}`
.split(path.sep)
.join('/'),
parserOpts: require(resolvedParserPreset)
};
ctx.parserPreset = parserPreset;
config.parserPreset = parserPreset;
}
return [...configs, c, ...loadExtends(c, ctx)];
}, []);
}
function getId(raw = '', prefix = '') {
const first = raw.charAt(0);
const scoped = first === '@';
const relative = first === '.';
return scoped || relative ? raw : [prefix, raw].filter(String).join('-');
}
function resolveConfig(raw, context = {}) {
const resolve = context.resolve || resolveId;
const id = getId(raw, context.prefix);
try {
return resolve(id, context);
} catch (err) {
const legacy = getId(raw, 'conventional-changelog-lint-config');
const resolved = resolve(legacy, context);
console.warn(
`Resolving ${raw} to legacy config ${legacy}. To silence this warning raise an issue at 'npm repo ${legacy}' to rename to ${id}.`
);
return resolved;
}
}
function resolveId(id, context = {}) {
const cwd = context.cwd || process.cwd();
const localPath = resolveFromSilent(cwd, id);
if (typeof localPath === 'string') {
return localPath;
}
const globalPath = resolveGlobalSilent(id);
if (typeof globalPath === 'string') {
return globalPath;
}
const err = new Error(`Cannot find module "${id}" from "${cwd}"`);
err.code = 'MODULE_NOT_FOUND';
throw err;
}
function resolveFromSilent(cwd, id) {
try {
return resolveFrom(cwd, id);
} catch (err) {}
}
function resolveGlobalSilent(id) {
try {
const resolveGlobal = requireUncached('resolve-global');
return resolveGlobal(id);
} catch (err) {}
}