forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge-dependency.ts
More file actions
218 lines (193 loc) · 5.74 KB
/
merge-dependency.ts
File metadata and controls
218 lines (193 loc) · 5.74 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
import { uniq } from 'lodash-es';
import * as semver from 'semver';
export interface ILambdaResponse {
contents: {
[path: string]: { content: string };
};
dependency: {
name: string;
version: string;
};
peerDependencies: {
[dep: string]: string;
};
dependencyDependencies: {
[dep: string]: {
semver: string;
resolved: string;
parents: string[];
entries: string[];
};
};
dependencyAliases: {
[dep: string]: {
[dep: string]: string;
};
};
}
interface IDepDepInfo {
semver: string;
resolved: string;
parents: string[];
entries: string[];
}
interface IResponse {
contents: { [path: string]: { content: string } };
dependencies: Array<{ name: string; version: string }>;
dependencyAliases: { [dep: string]: { [dep: string]: string } };
dependencyDependencies: {
[dep: string]: IDepDepInfo;
};
}
/**
* Compare two sorted string arrays
*
* @param {string[]} s1
* @param {string[]} s2
* @returns
*/
function isEqual(s1: string[], s2: string[]) {
if (s1.length !== s2.length) {
return false;
}
for (let i = 0; i < s1.length; i++) {
if (s1[i] !== s2[i]) {
return false;
}
}
return true;
}
/**
* Replaces the start of a key with a new string
*
* @param {{ [key: string]: string }} paths
* @param {string} oldName
* @param {string} newName
*/
function replacePaths(
paths: { [key: string]: any },
oldName: string,
newName: string
) {
Object.keys(paths).forEach(al => {
if (al.startsWith(`${oldName}/`) || al === oldName) {
paths[al.replace(oldName, newName)] =
typeof paths[al] === 'string'
? paths[al].replace(oldName, newName)
: paths[al];
delete paths[al];
}
});
}
function replaceDependencyInfo(
r: ILambdaResponse,
depDepName: string,
newDepDep: IDepDepInfo
) {
if (process.env.NODE_ENV === 'development') {
// eslint-disable-next-line
console.log(
'Resolving conflict for ' +
depDepName +
' new version: ' +
newDepDep.resolved
);
}
const newPath = `${depDepName}/${newDepDep.resolved}`;
replacePaths(
r.contents,
`/node_modules/${depDepName}`,
`/node_modules/${newPath}`
);
r.dependencyDependencies[newPath] = r.dependencyDependencies[depDepName];
delete r.dependencyDependencies[depDepName];
// eslint-disable-next-line
for (const n of Object.keys(r.dependencyDependencies)) {
r.dependencyDependencies[n].parents = r.dependencyDependencies[
n
].parents.map(p => (p === depDepName ? newPath : p));
}
r.dependencyAliases = r.dependencyAliases || {};
newDepDep.parents.forEach(p => {
r.dependencyAliases[p] = r.dependencyAliases[p] || {};
r.dependencyAliases[p][depDepName] = newPath;
});
replacePaths(r.dependencyAliases, depDepName, newPath);
}
const intersects = (v1: string, v2: string) => {
try {
return semver.intersects(v1, v2);
} catch (e) {
return false;
}
};
export function mergeDependencies(responses: ILambdaResponse[]) {
// For consistency between requests
const sortedResponses = responses.sort((a, b) =>
a.dependency.name.localeCompare(b.dependency.name)
);
const response: IResponse = {
contents: {},
dependencies: sortedResponses.map(r => r.dependency),
dependencyAliases: {},
dependencyDependencies: {},
};
// eslint-disable-next-line
for (const r of sortedResponses) {
for (let i = 0; i < Object.keys(r.dependencyDependencies).length; i++) {
const depDepName = Object.keys(r.dependencyDependencies)[i];
const newDepDep = r.dependencyDependencies[depDepName];
const rootDependency = response.dependencies.find(
d => d.name === depDepName
);
if (
rootDependency &&
!intersects(rootDependency.version, newDepDep.semver) &&
// If the resolved or semver is the same we don't have to actually replace dependency info
rootDependency.version !== newDepDep.resolved &&
rootDependency.version !== newDepDep.semver &&
rootDependency.name !== r.dependency.name // and this dependency doesn't require an older version of itself
) {
// If a root dependency is in conflict with a child dependency, we always
// go for the root dependency
replaceDependencyInfo(r, depDepName, newDepDep);
// Start from the beginning, to make sure everything is correct
i = -1;
} else if (response.dependencyDependencies[depDepName]) {
const exDepDep = response.dependencyDependencies[depDepName];
if (exDepDep.resolved === newDepDep.resolved) {
exDepDep.parents = uniq([...exDepDep.parents, ...newDepDep.parents]);
exDepDep.entries = uniq([...exDepDep.entries, ...newDepDep.entries]);
} else if (
intersects(exDepDep.semver, newDepDep.semver) &&
isEqual(exDepDep.entries, newDepDep.entries)
) {
const replacingDepDep = semver.gt(
newDepDep.resolved,
exDepDep.resolved
)
? newDepDep
: exDepDep;
response.dependencyDependencies[depDepName] = replacingDepDep;
response.dependencyDependencies[depDepName].parents = uniq([
...exDepDep.parents,
...newDepDep.parents,
]);
} else {
replaceDependencyInfo(r, depDepName, newDepDep);
// Start from the beginning, to make sure everything is correct
i = -1;
}
} else {
response.dependencyDependencies[depDepName] =
r.dependencyDependencies[depDepName];
}
}
response.dependencyAliases = {
...response.dependencyAliases,
...r.dependencyAliases,
};
response.contents = { ...response.contents, ...r.contents };
}
return response;
}