-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathupdate-version.ts
36 lines (31 loc) · 1.16 KB
/
update-version.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
import pc from "picocolors";
import { isMatching, match, P } from "ts-pattern";
import { ignores } from "./ignores";
import { glob, readJsonFile, writeJsonFile } from "./libs";
import { version } from "./version";
const GLOB_PACKAGE_JSON = ["package.json", "packages/*/package.json", "packages/*/*/package.json"];
async function update(path: string) {
const packageJson = await readJsonFile(path);
if (!isMatching({ version: P.string }, packageJson)) {
throw new Error(`Invalid package.json at ${path}`);
}
const newVersion = version;
const oldVersion = match(packageJson)
.with({ version: P.select(P.string) }, (v) => v)
.otherwise(() => "0.0.0");
if (oldVersion === newVersion) {
console.info(pc.greenBright(`Skipping ${path} as it's already on version ${newVersion}`));
return;
}
const packageJsonUpdated = {
...packageJson,
version: newVersion,
};
await writeJsonFile(path, packageJsonUpdated);
console.info(pc.green(`Updated ${path} to version ${packageJsonUpdated.version}`));
}
async function main() {
const tasks = glob(GLOB_PACKAGE_JSON, ignores);
await Promise.all(tasks.map((path) => update(path)));
}
await main();