forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolutions.ts
More file actions
57 lines (48 loc) · 1.26 KB
/
resolutions.ts
File metadata and controls
57 lines (48 loc) · 1.26 KB
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
/**
* Parse input strings like `package-1/package-2` to an array of packages
*/
function parsePackagePath(input: string) {
return input.match(/(@[^/]+\/)?([^/]+)/g) || [];
}
const WRONG_PATTERNS = /\/$|\/{2,}|\*+$/;
const GLOBAL_NESTED_DEP_PATTERN = '**/';
function isValidPackagePath(input: string) {
return !WRONG_PATTERNS.test(input);
}
export interface IParsedResolution {
name: string;
range: string;
globPattern: string;
pattern: string;
}
export function parsePatternInfo(
globPattern: string,
range: string
): IParsedResolution {
if (!isValidPackagePath(globPattern)) {
console.warn('invalidResolutionName');
return null;
}
const directories = parsePackagePath(globPattern);
const name = directories.pop();
// For legacy support of resolutions, replace `name` with `**/name`
if (name === globPattern) {
// eslint-disable-next-line
globPattern = `${GLOBAL_NESTED_DEP_PATTERN}${name}`;
}
return {
name,
range,
globPattern,
pattern: `${name}@${range}`,
};
}
export function parseResolutions(resolutions?: {
[name: string]: string;
}): IParsedResolution[] {
if (!resolutions) {
return [];
}
const keys = Object.keys(resolutions);
return keys.map(key => parsePatternInfo(key, resolutions[key]));
}