-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathversion.build.ts
45 lines (36 loc) · 1.41 KB
/
version.build.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
// Call the script with a new dev version as its argument.
// If the argument is not provided a new version will be
// automatically generated from package.json and current date,
// plus a gitHead field with the current Git commit hash
// will be added to package.json.
//
// The version format should be as follows:
// <MAJOR>.<MINOR>.<PATCH>-dev.<ISO-8601-DATE-WITHOUT-DELIMITERS>
// e.g. something like 1.4.0-dev.20240711
import { execSync } from "child_process";
import { readFileSync, writeFileSync } from "fs";
import { join } from "path";
import { z } from "zod";
const packageSchema = z.object({
version: z.string(),
});
const packageJsonPath = join(__dirname, "package.json");
const currentCommit = execSync("git rev-parse HEAD", {
encoding: "utf-8",
}).trim();
const packageJson = JSON.parse(readFileSync(packageJsonPath, "utf-8"));
if (typeof packageJson !== "object" || packageJson === null) {
throw new Error("package.json is not an object");
}
const generateVersion = () => {
const oldVersion = packageSchema.parse(packageJson).version;
const date = new Date().toISOString().substring(0, 10).replace("-", "");
return `${oldVersion}-dev.${date}`;
};
const newPackageJson = {
...packageJson,
version: process.argv[2] ?? generateVersion(),
gitHead: currentCommit,
};
writeFileSync(packageJsonPath, JSON.stringify(newPackageJson, null, 2));
execSync(`yarn prettier -w "${packageJsonPath}"`);