-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversionCheck.js
88 lines (77 loc) · 3.23 KB
/
versionCheck.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
#!/usr/bin/env node
const Fs = require('fs');
const Path = require('path');
const baseDir = process.cwd();
const shouldFix = process.argv[2] === 'fix';
const pkgPaths = new Set();
recursivelyFindPackageFiles(Path.resolve(baseDir, 'package.json'));
const pkgVersionByName = {};
for (const pkgPath of Array.from(pkgPaths)) {
const pkg = require(pkgPath);
pkgVersionByName[pkg.name] = pkg.version;
if (!pkg.version) {
console.log(`MISSING version ${pkg.name}`);
}
}
console.log('Checking Workspace Dependencies match these versions', pkgVersionByName);
for (const pkgPath of Array.from(pkgPaths)) {
const pkg = require(pkgPath);
checkDependencies('dependency', pkg?.dependencies, pkgPath);
checkDependencies('devDependency', pkg?.devDependencies, pkgPath);
checkDependencies('peerDependencies', pkg?.peerDependencies, pkgPath);
Fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
}
for (const sourcePkgPath of Array.from(pkgPaths)) {
for (const overrides of ['package.build.json', 'package.dist.json']) {
const pkgPath = sourcePkgPath.replace('package.json', overrides);
if (!Fs.existsSync(pkgPath)) continue;
const pkg = require(pkgPath);
checkDependencies('dependency', pkg?.dependencies, pkgPath);
checkDependencies('devDependency', pkg?.devDependencies, pkgPath);
checkDependencies('peerDependencies', pkg?.peerDependencies, pkgPath);
Fs.writeFileSync(pkgPath, JSON.stringify(pkg, null, 2) + '\n');
}
}
// HELPERS /////////////////////////////////////////////////////////////////////////////////////////////////////////////
function checkDependencies(type, dependencies, pkgPath) {
for (const [name, version] of Object.entries(dependencies || {})) {
const versionToBe = pkgVersionByName[name];
if (versionToBe && versionToBe !== version) {
if (shouldFix) {
dependencies[name] = versionToBe;
console.log(`UPGRADED ${name} from ${version} to ${versionToBe}: ${pkgPath}`);
} else {
console.log(
`MISMATCHED dependency version for ${name} (${version} should be ${versionToBe}): ${pkgPath}`,
);
}
}
}
}
function recursivelyFindPackageFiles(pkgPath) {
if (!Fs.existsSync(pkgPath)) return;
const pkg = JSON.parse(Fs.readFileSync(pkgPath, 'utf8'));
const pkgDir = Path.dirname(pkgPath);
if (pkgDir.startsWith('build')) return;
const workspaces = (Array.isArray(pkg.workspaces) ? pkg.workspaces : pkg.workspaces?.packages)?.map(x => x.replace('/build', '')) || [];
for (const workspace of workspaces) {
if (workspace.includes('/*')) {
const workspaceDir = workspace.replace('/*', '');
const subWorkspaces = Fs.readdirSync(Path.resolve(pkgDir, workspaceDir));
for (const subWorkspace of subWorkspaces) {
if (subWorkspace.startsWith('build')) continue;
const newPkgPath = Path.resolve(pkgDir, workspaceDir, subWorkspace, 'package.json');
if (Fs.existsSync(newPkgPath)) {
recursivelyFindPackageFiles(newPkgPath);
pkgPaths.add(newPkgPath);
}
}
} else {
const newPkgPath = Path.resolve(pkgDir, workspace, 'package.json');
if (Fs.existsSync(newPkgPath)) {
recursivelyFindPackageFiles(newPkgPath);
pkgPaths.add(newPkgPath);
}
}
}
}