forked from web-infra-dev/rspress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update-modern.ts
71 lines (64 loc) · 1.94 KB
/
update-modern.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
/**
* In this script, we will find all the Modern.js dependencies and update the version.
*/
import path from 'path';
import fs from 'fs-extra';
import { getPackages } from '@manypkg/get-packages';
import { getPackageVersion } from '@modern-js/generator-utils';
async function run() {
const modernVersion = process.env.MODERN_VERSION || 'latest';
const cwd = process.cwd();
const pkgPath = path.join(cwd, 'package.json');
const pkgObj = fs.readJSONSync(pkgPath, 'utf-8');
pkgObj.devDependencies = await updateModernVersion(
pkgObj.devDependencies,
modernVersion,
);
fs.writeJSONSync(pkgPath, pkgObj, { spaces: 2 });
const packages = await getPackages(cwd);
for (const pkg of packages.packages) {
const { packageJson, dir } = pkg;
const { dependencies, devDependencies, peerDependencies } = packageJson;
packageJson.dependencies = await updateModernVersion(
dependencies,
modernVersion,
);
packageJson.devDependencies = await updateModernVersion(
devDependencies,
modernVersion,
);
packageJson.peerDependencies = await updateModernVersion(
peerDependencies,
modernVersion,
);
fs.writeJSONSync(path.join(dir, 'package.json'), packageJson, {
spaces: 2,
});
}
}
const versionMap = new Map();
const updateModernVersion = async (
dependencies?: Record<string, string>,
modernVersion?: string,
) => {
if (!dependencies) {
return dependencies;
}
for (const dep of Object.keys(dependencies)) {
if (dep.startsWith('@modern-js') && dep !== '@rspress/mdx-rs') {
if (versionMap.get(dep)) {
dependencies[dep] = versionMap.get(dep);
} else {
const version = await getPackageVersion(`${dep}@${modernVersion}`);
dependencies[dep] = version;
versionMap.set(dep, version);
}
}
}
return dependencies;
};
run().catch(e => {
console.error(e);
// eslint-disable-next-line no-process-exit
process.exit(1);
});