-
Notifications
You must be signed in to change notification settings - Fork 135
/
checkPackages.js
75 lines (63 loc) · 2.29 KB
/
checkPackages.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
const lockfile = require('@yarnpkg/lockfile');
const fs = require('fs');
const develLock = lockfile.parse(fs.readFileSync('yarn.lock', { encoding: 'utf8' })).object;
const develPackage = JSON.parse(fs.readFileSync('package.json', { encoding: 'utf8' })).dependencies;
const releaseLock = lockfile.parse(fs.readFileSync('app/yarn.lock', { encoding: 'utf8' })).object;
const releasePackage = JSON.parse(fs.readFileSync('app/package.json', { encoding: 'utf8' })).dependencies;
function checkVersions() {
let valid = true;
Object.keys(releaseLock).forEach(key => {
if ((key[0] === '@') || (key[1] === '@')) {
return;
}
const id = key.split('@');
if (releasePackage[id[0]] !== id[1]) {
// only check the packages we resolve directly
return;
}
const devKey = Object.keys(develLock).find(iter => (id[1].startsWith('file'))
? iter.split('@')[0] === id[0]
: iter === key);
if (devKey === undefined) {
console.log('No entry in dev found', key);
return;
}
if (develLock[devKey].version !== releaseLock[key].version) {
console.error('Version mismatch!', key, releaseLock[key].version, 'vs', develLock[devKey].version);
valid = false;
} else if (develLock[devKey].resolved !== releaseLock[key].resolved) {
console.error('Same version but different commit id', key);
}
});
return valid;
}
function checkDevPackages() {
let valid = true;
Object.keys(develPackage).forEach(pkg => {
if (releasePackage[pkg] === undefined) {
console.error('Package not referenced in release', pkg);
valid = false;
} else if (!releasePackage[pkg].startsWith('file') && (releasePackage[pkg] !== develPackage[pkg])) {
console.error('Referenced version mismatch', pkg, releasePackage[pkg], 'vs', develPackage[pkg]);
valid = false;
}
});
return valid;
}
function checkRelPackages() {
let valid = true;
Object.keys(releasePackage).forEach(pkg => {
if (develPackage[pkg] === undefined) {
console.error('Package not referenced in devel', pkg);
valid = false;
}
});
return valid;
}
const success = checkRelPackages() && checkDevPackages() && checkVersions();
if (success) {
console.log('packages are valid');
} else {
console.error('packages are invalid');
}
process.exit(success ? 0 : 1);