-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcheck-npm-versions.ts
77 lines (64 loc) · 2.18 KB
/
check-npm-versions.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
import { Meteor } from 'meteor/meteor';
import semver from 'semver';
type boolOrString = boolean | string;
interface indexBoolorString {
[key: string]: boolOrString
}
interface indexAny {
[key: string]: any
}
// Returns:
// - true if a version of the package in the range is installed
// - false if no version is installed
// - version# if incompatible version is installed
const compatibleVersionIsInstalled = (name: string, range: string | semver.Range): boolOrString => {
try {
// eslint-disable-next-line
const installedVersion = require(`${name}/package.json`).version;
if (semver.satisfies(installedVersion, range)) {
return true;
} else {
return installedVersion;
}
} catch (e: any) {
// XXX add something to the tool to make this more reliable
const message = e.toString();
// One message comes out of the installation npm package the other from npm directly
if (message.includes('Cannot find module') || message.includes("Can't find npm module")) {
return false;
} else {
throw e;
}
}
};
export const checkNpmVersions = (packages: indexAny, packageName: string): void => {
if (Meteor.isDevelopment) {
const failures: indexBoolorString = {};
for (const name of Object.keys(packages)) {
const range = packages[name];
const failure = compatibleVersionIsInstalled(name, range);
if (failure !== true) {
failures[name] = failure;
}
}
if (Object.keys(failures).length === 0) {
return;
}
const errors: string[] = [];
for (const name of Object.keys(failures)) {
const installed = failures[name];
const requirement = `${name}@${packages[name]}`;
if (installed) {
errors.push(` - ${name}@${installed} installed, ${requirement} needed`);
} else {
errors.push(` - ${name}@${packages[name]} not installed.`);
}
}
const qualifier = packageName ? `(for ${packageName}) ` : '';
console.warn(`WARNING: npm peer requirements ${qualifier}not installed:
${errors.join('\n')}
Read more about installing npm peer dependencies:
https://guide.meteor.com/using-packages.html#peer-npm-dependencies
`);
}
};