Skip to content

Instantly share code, notes, and snippets.

@Yanrishatum
Created April 28, 2023 10:57
Show Gist options
  • Save Yanrishatum/e8b942ade1301dc939fcefe5c4d532f0 to your computer and use it in GitHub Desktop.
Save Yanrishatum/e8b942ade1301dc939fcefe5c4d532f0 to your computer and use it in GitHub Desktop.
Peacock update script
// Requires Deno runtime, because Node is trash
// Install:
// 1. Put update.ts into peacock directory.
// 2. Create an empty version.txt file (IMPORTANT).
// Usage:
// 1. Turn off server/patcher (duh)
// 2. Run `deno run -A update.ts`
// How it works:
// It fetches latest release from peacock github and checks if the release tag mismatches contents of version.txt file.
// If it mismatches - downloads the lite variant of the release, unpacks it, and copies contents into main directory.
// Then it just saves the release tag and deletes unpacked dir/zip file.
// It requires version.txt to exist because for whatever reason Deno doesn't have "exists" check.
// Frankly should be fairly easy to reimplement in node-compatible way, but I can't be assed.
import * as zip from "https://deno.land/x/zip@v1.2.5/mod.ts";
async function recursiveCopy(path: string, to: string): Promise<unknown> {
const promises: Promise<unknown>[] = [];
for (const de of Deno.readDirSync(path)) {
const fromPath = path + "/" + de.name;
const toPath = to + "/" + de.name;
if (de.isFile) {
promises.push(Deno.copyFile(fromPath, toPath));
} else {
await Deno.mkdir(toPath, { recursive: true });
promises.push(recursiveCopy(fromPath, toPath));
}
}
return await Promise.all(promises);
}
async function main() {
const data = await (await fetch("https://api.github.com/repos/thepeacockproject/Peacock/releases/latest")).json();
const tag = data.tag_name;
const lastTag = await Deno.readTextFile("version.txt");
if (tag == lastTag) {
console.log("Already on latest version!");
return;
}
console.log(`Upgrading ${lastTag} -> ${tag}`);
const liteAsset = data.assets.find(a => a.name.includes("-lite.zip"));
if (!liteAsset) {
console.log("ERROR: Couldn't find lite zip!");
return;
}
await Deno.writeFile(liteAsset.name, await (await fetch(liteAsset.browser_download_url)).arrayBuffer());
console.log("Unzipping...");
await zip.decompress(liteAsset.name, ".");
const dir = liteAsset.name.substring(0, liteAsset.name.indexOf(".zip"));
console.log("Copying over...");
await recursiveCopy(dir, ".");
console.log("Cleaning up...");
Deno.remove(liteAsset.name);
Deno.remove(dir, { recursive: true });
await Deno.writeTextFile("version.txt", tag);
console.log("Done!");
}
await main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment