-
Notifications
You must be signed in to change notification settings - Fork 0
/
check-versions.ts
196 lines (180 loc) · 4.88 KB
/
check-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
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
/*
* This script checks if new versions of node modules are available.
* It uses naming conventions to transform constants to matching node module name.
*
* Usage:
* yarn check-versions [file|package]
*
* Positional arg:
* - [file]: relative or absolute file path to the versions file.
*
* Example:
* yarn check-versions react
*/
import { join, relative } from 'path';
import { gt } from 'semver';
import { readJsonSync, writeJsonSync } from 'fs-extra';
import * as chalk from 'chalk';
import { dasherize } from '../packages/workspace/src/utils/strings';
import * as glob from 'glob';
import { execSync } from 'child_process';
import { readFileSync, writeFileSync } from 'fs';
const root = join(__dirname, '..');
const excluded = ['nxVersion'];
const scoped = [
'babel',
'emotion',
'reduxjs',
'swc',
'testing-library',
'types',
];
try {
const files = process.argv[2]
? [normalize(process.argv[2])]
: glob.sync('packages/**/*/versions.ts').map((x) => relative(root, x));
checkFiles(files);
} catch (e) {
console.log(chalk.red(e.message));
process.exitCode = 1;
}
function normalize(x: string) {
if (x.endsWith('.ts')) {
return x;
} else {
return join('packages', x, 'src/utils/versions.ts');
}
}
// -----------------------------------------------------------------------------
function checkFiles(files: string[]) {
console.log(chalk.blue(`Checking versions in the following files...\n`));
console.log(` - ${files.join('\n - ')}\n`);
const maxFileNameLength = Math.max(...files.map((f) => f.length));
let hasError = false;
files.forEach((f) => {
const projectRoot = f.split('/src/')[0];
const migrationsPath = join(projectRoot, 'migrations.json');
const migrationsJson = readJsonSync(migrationsPath);
let versionsContent = readFileSync(f).toString();
const versions = getVersions(f);
const npmPackages = getPackages(versions);
const results = npmPackages.map(([p, v, o]) => getVersionData(p, v, o));
const logContext = `${f.padEnd(maxFileNameLength)}`;
const packageUpdates = {};
results.forEach((r) => {
if (r.outdated) {
console.log(
`${logContext} ❗ ${chalk.bold(
r.package
)} has new version ${chalk.bold(r.latest)} (current: ${r.prev})`
);
versionsContent = versionsContent.replace(
`${r.variable} = '${r.prev}'`,
`${r.variable} = '${r.latest}'`
);
packageUpdates[r.package] = {
version: r.latest,
alwaysAddToPackageJson: false,
};
}
if (r.invalid) {
hasError = true;
console.log(
`${logContext} ⚠️ ${chalk.bold(r.package)} has an invalid version (${
r.prev
}) specified. Latest is ${r.latest}.`
);
}
});
if (Object.keys(packageUpdates).length > 0) {
migrationsJson.packageJsonUpdates['x.y.z'] = {
version: 'x.y.z',
packages: packageUpdates,
};
writeFileSync(f, versionsContent);
writeJsonSync(migrationsPath, migrationsJson, { spaces: 2 });
}
});
if (hasError) {
throw new Error('Invalid versions of packages found (please see above).');
}
}
function getVersions(path: string) {
const versionsPath =
path.startsWith('.') || path.startsWith('packages')
? join(__dirname, '..', path)
: path;
try {
return require(versionsPath);
} catch {
throw new Error(`Could not load ${path}. Please make sure it is valid.`);
}
}
function getPackages(versions: Record<string, string>): string[][] {
return Object.entries(versions).reduce((acc, [name, version]) => {
if (!excluded.includes(name)) {
const npmName = getNpmName(name);
acc.push([npmName, version, name]);
}
return acc;
}, [] as string[][]);
}
function getNpmName(name: string): string {
const dashedName = dasherize(name.replace(/Version$/, ''));
const scope = scoped.find((s) => dashedName.startsWith(`${s}-`));
if (scope) {
const rest = dashedName.split(`${scope}-`)[1];
return `@${scope}/${rest}`;
} else {
return dashedName;
}
}
function getVersionData(
p: string,
v: string,
o: string
): {
variable: string;
package: string;
outdated: boolean;
invalid: boolean;
latest: string;
prev?: string;
} {
try {
const latest = JSON.parse(
execSync(`npm view ${p} version --json --silent`, {
stdio: ['ignore'],
}).toString('utf-8')
);
if (gt(latest, v)) {
return {
variable: o,
package: p,
outdated: true,
invalid: false,
latest,
prev: v,
};
}
if (gt(v, latest)) {
return {
variable: o,
package: p,
outdated: false,
invalid: true,
latest,
prev: v,
};
}
} catch {
// ignored
}
return {
variable: o,
package: p,
outdated: false,
invalid: false,
latest: v,
};
}