-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathindex.ts
147 lines (124 loc) Β· 3.7 KB
/
index.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
import path from 'path';
import 'resolve-global';
import resolveFrom from 'resolve-from';
import {isArray, merge, mergeWith, omit} from 'lodash';
const importFresh = require('import-fresh');
export interface ResolvedConfig {
parserPreset?: unknown;
[key: string]: unknown;
}
export interface ResolveExtendsConfig {
parserPreset?: unknown;
extends?: string[];
[key: string]: unknown;
}
export interface ResolveExtendsContext {
cwd?: string;
parserPreset?: unknown;
prefix?: string;
resolve?(id: string, ctx?: {prefix?: string; cwd?: string}): string;
resolveGlobal?: (id: string) => string;
require?<T>(id: string): T;
}
export default function resolveExtends(
config: ResolveExtendsConfig = {},
context: ResolveExtendsContext = {}
) {
const {extends: e} = config;
const extended = loadExtends(config, context).reduceRight(
(r, c) =>
mergeWith(r, omit(c, 'extends'), (objValue, srcValue) => {
if (isArray(objValue)) {
return srcValue;
}
}),
e ? {extends: e} : {}
);
return merge({}, extended, config);
}
function loadExtends(
config: ResolveExtendsConfig = {},
context: ResolveExtendsContext = {}
): ResolvedConfig[] {
return (config.extends || []).reduce<ResolvedConfig[]>((configs, raw) => {
const load = context.require || require;
const resolved = resolveConfig(raw, context);
const c = load(resolved);
const cwd = path.dirname(resolved);
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: string = '', prefix: string = ''): string {
const first = raw.charAt(0);
const scoped = first === '@';
const relative = first === '.';
const absolute = path.isAbsolute(raw);
if (scoped) {
return raw.includes('/') ? raw : [raw, prefix].filter(String).join('/');
}
return relative || absolute ? raw : [prefix, raw].filter(String).join('-');
}
function resolveConfig<T>(
raw: string,
context: ResolveExtendsContext = {}
): string {
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: string,
context: {cwd?: string; resolveGlobal?: (id: string) => string | void} = {}
): string {
const cwd = context.cwd || process.cwd();
const localPath = resolveFromSilent(cwd, id);
if (typeof localPath === 'string') {
return localPath;
}
const resolveGlobal = context.resolveGlobal || resolveGlobalSilent;
const globalPath = resolveGlobal(id);
if (typeof globalPath === 'string') {
return globalPath;
}
const err = new Error(`Cannot find module "${id}" from "${cwd}"`);
(err as any).code = 'MODULE_NOT_FOUND';
throw err;
}
function resolveFromSilent(cwd: string, id: string): string | void {
try {
return resolveFrom(cwd, id);
} catch (err) {}
}
function resolveGlobalSilent(id: string): string | void {
try {
const resolveGlobal = importFresh('resolve-global');
return resolveGlobal(id);
} catch (err) {}
}