2024-10-30 19:06:03 +01:00
|
|
|
#!/usr/bin/env -S npx tsx
|
2019-12-12 20:33:29 +01:00
|
|
|
|
|
|
|
/*
|
2020-02-04 23:19:28 +01:00
|
|
|
* Checks for the presence of a webapp, inspects its version and sets the
|
2019-12-12 20:33:29 +01:00
|
|
|
* version metadata of the package to match.
|
|
|
|
*/
|
|
|
|
|
2024-10-30 19:06:03 +01:00
|
|
|
import { promises as fs } from "node:fs";
|
2023-04-06 14:41:38 +02:00
|
|
|
import * as asar from "@electron/asar";
|
2024-10-30 19:06:03 +01:00
|
|
|
import * as childProcess from "node:child_process";
|
|
|
|
import * as url from "node:url";
|
2019-12-12 20:33:29 +01:00
|
|
|
|
2022-12-05 12:50:49 +01:00
|
|
|
export async function versionFromAsar(): Promise<string> {
|
2019-12-12 20:33:29 +01:00
|
|
|
try {
|
2022-12-15 12:00:58 +01:00
|
|
|
await fs.stat("webapp.asar");
|
2024-10-16 16:59:12 +02:00
|
|
|
} catch {
|
2022-12-05 12:50:49 +01:00
|
|
|
throw new Error("No 'webapp.asar' found. Run 'yarn run fetch'");
|
2019-12-12 20:33:29 +01:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:00:58 +01:00
|
|
|
return asar.extractFile("webapp.asar", "version").toString().trim();
|
2020-02-26 12:56:59 +01:00
|
|
|
}
|
2019-12-13 11:45:27 +01:00
|
|
|
|
2022-12-05 12:50:49 +01:00
|
|
|
export async function setPackageVersion(ver: string): Promise<void> {
|
2019-12-13 11:45:27 +01:00
|
|
|
// set version in package.json: electron-builder will use this to populate
|
|
|
|
// all the various version fields
|
2022-12-05 12:50:49 +01:00
|
|
|
await new Promise<void>((resolve, reject) => {
|
2022-12-15 12:00:58 +01: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,
|
|
|
|
],
|
2024-04-17 16:12:29 +02:00
|
|
|
{
|
|
|
|
// 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 12:00:58 +01:00
|
|
|
(err) => {
|
|
|
|
if (err) {
|
|
|
|
reject(err);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2019-12-12 20:33:29 +01:00
|
|
|
});
|
2020-02-26 12:56:59 +01:00
|
|
|
}
|
2019-12-13 11:45:27 +01:00
|
|
|
|
2022-12-05 12:50:49 +01:00
|
|
|
async function main(args: string[]): Promise<number> {
|
2020-03-07 17:12:59 +01:00
|
|
|
let version = args[0];
|
2020-02-26 12:56:59 +01:00
|
|
|
|
|
|
|
if (version === undefined) version = await versionFromAsar();
|
|
|
|
|
2020-03-07 17:12:59 +01:00
|
|
|
await setPackageVersion(version);
|
2022-12-05 12:50:49 +01:00
|
|
|
return 0;
|
2020-02-26 12:56:59 +01:00
|
|
|
}
|
|
|
|
|
2024-10-30 19:06:03 +01: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 12:56:59 +01:00
|
|
|
}
|