-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
260 lines (212 loc) · 6.63 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
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
'use strict';
const resolvePackagePath = require('resolve-package-path');
const semver = require('semver');
const path = require('path');
// avoid checking multiple times from the same location
const HasPeerDepsInstalled = new Map();
const NullCache = new (class NullCache {
get() {}
set() {}
has() {
return false;
}
})();
function throwUsefulError(result) {
let { missingPeerDependencies, incompatibleRanges } = result;
let missingPeerDependenciesMessage = (missingPeerDependencies || []).reduce(
(message, metadata) => {
return `${message}\n\t* ${metadata.name}: \`${metadata.specifiedPeerDependencyRange}\`; it was not installed`;
},
''
);
let incompatiblePeerDependenciesMessage = (incompatibleRanges || []).reduce(
(message, metadata) => {
return `${message}\n\t* ${metadata.name}: \`${metadata.specifiedPeerDependencyRange}\`; it was resolved to \`${metadata.version}\``;
},
''
);
throw new Error(
`${result.pkg.name} has the following unmet peerDependencies:\n${missingPeerDependenciesMessage}${incompatiblePeerDependenciesMessage}`
);
}
function resolvePackageVersion(
packageName,
resolvePeerDependenciesFrom,
cache
) {
let assumedVersion = AssumptionMap.get(packageName);
if (assumedVersion !== undefined) {
return assumedVersion;
}
let peerDepPackagePath = resolvePackagePath(
packageName,
resolvePeerDependenciesFrom,
cache === NullCache ? false : undefined
);
if (peerDepPackagePath === null) {
return null;
}
return require(peerDepPackagePath).version;
}
module.exports = function validatePeerDependencies(parentRoot, options = {}) {
if (
process.env.VALIDATE_PEER_DEPENDENCIES &&
process.env.VALIDATE_PEER_DEPENDENCIES.toLowerCase() === 'false'
) {
// do not validate
return;
}
let { cache, handleFailure, resolvePeerDependenciesFrom } = options;
if (cache === false) {
cache = NullCache;
} else if (cache === undefined || cache === true) {
cache = HasPeerDepsInstalled;
}
if (typeof handleFailure !== 'function') {
handleFailure = throwUsefulError;
}
let cacheKey = parentRoot;
if (resolvePeerDependenciesFrom === undefined) {
resolvePeerDependenciesFrom = parentRoot;
} else {
cacheKey += `\0${resolvePeerDependenciesFrom}`;
}
if (cache.has(cacheKey)) {
let result = cache.get(cacheKey);
if (result !== true) {
handleFailure(result);
}
return;
}
let packagePath = resolvePackagePath.findUpPackagePath(
parentRoot,
cache === NullCache ? false : undefined
);
if (packagePath === null) {
throw new Error(
`validate-peer-dependencies could not find a package.json when resolving upwards from:\n\t${parentRoot}.`
);
}
let ignoredPeerDependencies = process.env.IGNORE_PEER_DEPENDENCIES
? new Set(process.env.IGNORE_PEER_DEPENDENCIES.split(','))
: new Set();
let pkg = require(packagePath);
let { dependencies, peerDependencies, peerDependenciesMeta } = pkg;
let hasDependencies = Boolean(dependencies);
let hasPeerDependenciesMeta = Boolean(peerDependenciesMeta);
// lazily created as needed
let missingPeerDependencies = null;
let incompatibleRanges = null;
let invalidPackageConfiguration = null;
for (let packageName in peerDependencies) {
if (hasDependencies && packageName in dependencies) {
if (invalidPackageConfiguration === null) {
invalidPackageConfiguration = [];
}
invalidPackageConfiguration.push({
name: packageName,
reason: 'included both as dependency and as a peer dependency',
});
}
if (ignoredPeerDependencies.has(packageName)) {
continue;
}
// foo-package: >= 1.9.0 < 2.0.0
// foo-package: >= 1.9.0
// foo-package: ^1.9.0
let specifiedPeerDependencyRange = peerDependencies[packageName];
let foundPackageVersion = resolvePackageVersion(
packageName,
resolvePeerDependenciesFrom,
cache
);
if (foundPackageVersion === null) {
if (
hasPeerDependenciesMeta &&
packageName in peerDependenciesMeta &&
peerDependenciesMeta[packageName].optional
) {
continue;
}
if (missingPeerDependencies === null) {
missingPeerDependencies = [];
}
missingPeerDependencies.push({
name: packageName,
specifiedPeerDependencyRange,
});
continue;
}
if (
!semver.satisfies(foundPackageVersion, specifiedPeerDependencyRange, {
includePrerelease: true,
})
) {
if (incompatibleRanges === null) {
incompatibleRanges = [];
}
incompatibleRanges.push({
name: packageName,
version: foundPackageVersion,
specifiedPeerDependencyRange,
});
continue;
}
}
if (invalidPackageConfiguration !== null) {
// intentionally throwing an error here (not going through `handleFailure`) because
// this represents a problem with the including package itself that should not be
// squelchable by a custom `handleFailure`
let invalidPackageConfigurationMessage = invalidPackageConfiguration.reduce(
(message, metadata) =>
`${message}\n\t* ${metadata.name}: ${metadata.reason}`,
''
);
let relativePath = path.relative(process.cwd(), parentRoot);
throw new Error(
`${pkg.name} (at \`./${relativePath}\`) is improperly configured:\n${invalidPackageConfigurationMessage}`
);
}
let isValid = missingPeerDependencies === null && incompatibleRanges === null;
let result;
if (isValid) {
result = true;
} else {
result = {
pkg,
packagePath,
incompatibleRanges,
missingPeerDependencies,
};
}
cache.set(cacheKey, result);
if (result !== true) {
handleFailure(result);
}
};
let AssumptionMapName = '__ValidatePeerDependenciesAssumeProvided';
if (!(AssumptionMapName in global)) {
global[AssumptionMapName] = new Map();
}
// make sure to re-use the map created by a different instance of
// validate-peer-dependencies
let AssumptionMap = global[AssumptionMapName];
module.exports.assumeProvided = function ({ name, version } = {}) {
if (name === undefined || version === undefined) {
throw new Error(
`assumeProvided({ name, version}): name and version are required, but name='${name}' version='${version}'`
);
}
AssumptionMap.set(name, version);
};
Object.defineProperty(module.exports, '__HasPeerDepsInstalled', {
enumerable: false,
configurable: false,
value: HasPeerDepsInstalled,
});
module.exports._resetCache = function () {
HasPeerDepsInstalled.clear();
};
module.exports._resetAssumptions = function () {
global[AssumptionMapName].clear();
};