-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathupdater.mjs
68 lines (65 loc) · 2.05 KB
/
updater.mjs
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
import latestVersion from 'latest-version';
import { parse } from 'semver';
import { writeFileSync, readFileSync } from 'node:fs';
import { dirname, join } from 'node:path';
import sortPackageJson from 'sort-package-json';
const paths = [
'./packages/babel/package.json',
'./packages/codestyle/package.json',
'./packages/compiler/package.json',
'./packages/starter/package.json',
'./packages/tester/package.json',
'./packages/utils/package.json',
];
const fields = ['dependencies', 'devDependencies'];
const pkgs = paths.map((p) => {
const data = readFileSync(p, 'utf8');
return {
data: JSON.parse(data),
path: p,
};
});
for (let i = 0; i < pkgs.length; i++) {
const pkg = pkgs[i].data;
const p = pkgs[i].path;
for (let y = 0; y < fields.length; y++) {
const field = fields[y];
const deps = pkg[field] || {};
const names = Object.keys(deps);
const forUpdate = {};
for (let j = 0; j < names.length; j++) {
const dep = names[j];
if (dep.indexOf('@rockpack/') !== -1) {
continue;
}
const newVersion = await latestVersion(dep);
const oldVersion = deps[dep];
if (newVersion !== oldVersion) {
console.log(
`[${pkg.name}] dependency ${dep} from "${field}" will be updated from ${oldVersion} to ${newVersion}`,
);
const newVersionParsed = parse(newVersion);
const oldVersionParsed = parse(oldVersion);
if (newVersionParsed.major !== oldVersionParsed.major) {
console.warn(`Major dependency for ${dep} will be updated`);
}
forUpdate[dep] = newVersion;
}
}
if (Object.keys(forUpdate).length > 0) {
const pathToPackageJson = dirname(p);
console.warn(`[${pkg.name}] package.json will be updated`);
const updatedPackageJson = JSON.stringify(
sortPackageJson({
...pkg,
...{
[field]: { ...deps, ...forUpdate },
},
}),
null,
2,
);
writeFileSync(join(pathToPackageJson, 'package.json'), updatedPackageJson);
}
}
}