-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathversionUpdate.mjs
89 lines (82 loc) · 2.46 KB
/
versionUpdate.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
* versionUpdate
* This script is meant to be used by CI workflows to update a service's new version
* in the version.json file.
* Usage: `node versionUpdate service [name] [newVersion]`
* Usage: `node versionUpdate version [newVersion]`
*/
import path from "path";
import fs from "fs/promises";
const versionPath = path.join(process.cwd(), "version.json");
async function getVersionFile() {
try {
const contents = await fs.readFile(versionPath, { encoding: "utf-8" });
return JSON.parse(contents);
} catch (err) {
console.error(`Error reading version file:\n`, err);
process.exit(1);
}
}
async function writeVersionFile(contents) {
try {
await fs.writeFile(versionPath, JSON.stringify(contents, null, 3), {
encoding: "utf-8",
});
} catch (err) {
console.error(`Error writing version file:\n`, err);
process.exit(1);
}
}
async function updateServiceVersion(service, newVersion) {
const versions = await getVersionFile();
const old = versions.services[service];
if (!old) {
console.log(
"Invalid service name '%s', valid service names: \n - %s",
service,
Object.keys(versions.services).join("\n - ")
);
process.exit(1);
}
// Docker tags cannot contain `+`
versions.services[service] = newVersion.replaceAll("+", "_");
writeVersionFile(versions);
console.log("Updated %s from %s to %s", service, old, newVersion);
}
async function updateMetaVersion(version) {
const versions = await getVersionFile();
versions.version = version;
writeVersionFile(versions);
}
const [, , command, arg1, version] = process.argv;
switch (command) {
case "service":
if (!arg1) {
console.log(
"Expected argument name (service name)\nusage: node versionUpdate service [name] [version]"
);
process.exit(1);
}
if (!version) {
console.log(
"Expected argument version\nusage: node versionUpdate service [name] [version]"
);
process.exit(1);
}
updateServiceVersion(arg1, version);
break;
case "version":
if (!arg1) {
console.log(
"Expected argument type\nusage: node versionUpdate version [version]"
);
process.exit(1);
}
updateMetaVersion(arg1);
break;
default:
console.log(
"Unkown command: %s\nusage: node versionUpdate [ service | version ] ",
command
);
}