1
0
silverbullet/cmd/upgrade.ts

113 lines
3.4 KiB
TypeScript
Raw Normal View History

2022-10-24 11:51:26 +00:00
import { version } from "../version.ts";
export async function upgradeCommand() {
2022-10-29 14:06:18 +00:00
console.log("Now going to attempt an upgrade...");
2022-10-24 11:51:26 +00:00
2023-10-04 08:47:52 +00:00
const command = new Deno.Command("deno", {
args: ["cache", "--reload", Deno.mainModule],
stdout: "inherit",
stderr: "inherit",
2022-10-24 11:51:26 +00:00
});
2023-10-04 08:47:52 +00:00
const commandOutput = await command.output();
if (!commandOutput.success) {
2022-10-24 11:51:26 +00:00
console.error("Something went wrong there...");
Deno.exit(1);
}
console.log(
"So, that's done. Now let's see if this actually did anything...",
);
2023-10-04 08:47:52 +00:00
const vp = new Deno.Command("deno", {
args: ["run", "-A", "--unstable", Deno.mainModule, "version"],
2022-10-24 11:51:26 +00:00
});
2023-10-04 08:47:52 +00:00
const versionStatus = await vp.output();
2022-10-24 11:51:26 +00:00
if (!versionStatus.success) {
console.error("Could not run version command, something is wrong.");
Deno.exit(1);
}
2023-10-04 08:47:52 +00:00
const newVersion = new TextDecoder().decode(versionStatus.stdout).trim();
2022-10-24 11:51:26 +00:00
if (newVersion === version) {
console.log(
2022-12-13 05:04:28 +00:00
`Nope. I hate to tell you this, but it looks like we're still running ${newVersion}. This was a bit of a futile exercise. Let's try again soon some time.`,
2022-10-24 11:51:26 +00:00
);
} else {
console.log(
2022-12-13 05:04:28 +00:00
`Congrats, we've upgraded you from ${version} to ${newVersion}. Seems like quite a bump, enjoy! https://silverbullet.md/CHANGELOG may give you more hints on what's new.`,
2022-10-24 11:51:26 +00:00
);
}
}
// WIP: This is not yet working, but it's a start
import { dirname } from "https://deno.land/std@0.186.0/path/mod.ts";
const silverBulletReleases =
"https://github.com/silverbulletmd/silverbullet/releases";
export async function compiledUpgradeCommand() {
console.log("Now going to attempt an upgrade...");
const resp = await fetch(`${silverBulletReleases}/latest`, {
redirect: "manual",
});
const versionedUrl = resp.headers.get("location")!;
const latestVersion = /([^\/]+)$/.exec(versionedUrl);
if (!latestVersion) {
console.error("Could not fetch latest version");
}
if (version === latestVersion![0]) {
console.log("No version available, we're done here!");
return;
}
console.log(
"New version available:",
latestVersion![0],
"which I will now personally download. Hang on...",
);
const installDir = dirname(new URL(Deno.mainModule).pathname);
const tmpDir = Deno.makeTempDirSync();
// const zipUrl = "https://github.com/silverbulletmd/silverbullet/releases/download/test-release/silverbullet-server-apple-aarch64.zip";
const zipUrl =
`${versionedUrl}/download/silverbullet-server-${Deno.build.os}-${Deno.build.arch}.zip`;
const zipPath = `${tmpDir}/silverbullet.zip`;
const command = new Deno.Command("curl", {
args: [
"-L",
"-o",
zipPath,
zipUrl,
],
});
const curlOutput = await command.output();
console.log(
"Now going to replace the existing silverbullet binary in",
installDir,
);
if (curlOutput.code !== 0) {
console.error(
"Download failed",
new TextDecoder().decode(curlOutput.stderr),
);
return;
}
const unzipCommand = new Deno.Command("unzip", {
args: [
"-o",
"-d",
installDir,
`${tmpDir}/silverbullet.zip`,
],
});
const zipOutput = await unzipCommand.output();
if (zipOutput.code !== 0) {
console.error(
"Download failed",
new TextDecoder().decode(curlOutput.stderr),
);
return;
}
await Deno.chmod(`${installDir}/silverbullet`, 0o755);
await Deno.remove(zipPath);
console.log("And done! Restart your server to get the latest and greatest!");
}