Files
element-web/scripts/set-version.ts
T

74 lines
2.2 KiB
TypeScript
Raw Normal View History

2024-10-30 18:06:03 +00:00
#!/usr/bin/env -S npx tsx
2019-12-12 19:33:29 +00:00
/*
2020-02-04 22:19:28 +00:00
* Checks for the presence of a webapp, inspects its version and sets the
2019-12-12 19:33:29 +00:00
* version metadata of the package to match.
*/
2024-10-30 18:06:03 +00:00
import { promises as fs } from "node:fs";
2023-04-06 12:41:38 +00:00
import * as asar from "@electron/asar";
2024-10-30 18:06:03 +00:00
import * as childProcess from "node:child_process";
import * as url from "node:url";
2019-12-12 19:33:29 +00:00
2022-12-05 11:50:49 +00:00
export async function versionFromAsar(): Promise<string> {
2019-12-12 19:33:29 +00:00
try {
2022-12-15 11:00:58 +00:00
await fs.stat("webapp.asar");
} catch {
2022-12-05 11:50:49 +00:00
throw new Error("No 'webapp.asar' found. Run 'yarn run fetch'");
2019-12-12 19:33:29 +00:00
}
2022-12-15 11:00:58 +00:00
return asar.extractFile("webapp.asar", "version").toString().trim();
2020-02-26 11:56:59 +00:00
}
2022-12-05 11:50:49 +00:00
export async function setPackageVersion(ver: string): Promise<void> {
// set version in package.json: electron-builder will use this to populate
// all the various version fields
2022-12-05 11:50:49 +00:00
await new Promise<void>((resolve, reject) => {
2022-12-15 11:00:58 +00:00
childProcess.execFile(
process.platform === "win32" ? "yarn.cmd" : "yarn",
[
"version",
"-s",
"--no-git-tag-version", // This also means "don't commit to git" as it turns out
"--new-version",
ver,
],
{
// We need shell mode on Windows to be able to launch `.cmd` executables
// See https://nodejs.org/en/blog/vulnerability/april-2024-security-releases-2
shell: process.platform === "win32",
},
2022-12-15 11:00:58 +00:00
(err) => {
if (err) {
reject(err);
} else {
resolve();
}
},
);
2019-12-12 19:33:29 +00:00
});
2020-02-26 11:56:59 +00:00
}
2022-12-05 11:50:49 +00:00
async function main(args: string[]): Promise<number> {
2020-03-07 16:12:59 +00:00
let version = args[0];
2020-02-26 11:56:59 +00:00
if (version === undefined) version = await versionFromAsar();
2020-03-07 16:12:59 +00:00
await setPackageVersion(version);
2022-12-05 11:50:49 +00:00
return 0;
2020-02-26 11:56:59 +00:00
}
2024-10-30 18:06:03 +00:00
if (import.meta.url.startsWith("file:")) {
const modulePath = url.fileURLToPath(import.meta.url);
if (process.argv[1] === modulePath) {
main(process.argv.slice(2))
.then((ret) => {
process.exit(ret);
})
.catch((e) => {
console.error(e);
process.exit(1);
});
}
2020-02-26 11:56:59 +00:00
}