diff --git a/scripts/update-constant.ts b/scripts/update-constant.ts new file mode 100644 index 0000000..f321f56 --- /dev/null +++ b/scripts/update-constant.ts @@ -0,0 +1,96 @@ +/** + * This script tries to update constant automatically + */ +import { path } from "../deps.ts"; + +const ROOT_DIR = path.resolve( + path.dirname(path.fromFileUrl(import.meta.url)), + "..", +); +const CONSTANT_PATH = path.join(ROOT_DIR, "constant.ts"); +const STORE_URL = + "https://apps.apple.com/us/app/nintendo-switch-online/id1234806557"; +const SPLATNET3_URL = "https://api.lp1.av5ja.srv.nintendo.net"; + +function replaceConst(content: string, name: string, value: string): string { + const regex = new RegExp(`export const ${name} = ".*?";\n`); + + return content.replace(regex, `export const ${name} = "${value}";\n`); +} + +function getConst(content: string, name: string): string { + const regex = new RegExp(`export const ${name} = (".*?");\n`); + + const match = regex.exec(content); + + if (!match) { + throw new Error(`Cannot find ${name}`); + } + + return JSON.parse(match[1]); +} + +async function printError(p: Promise): Promise { + try { + return await p; + } catch (e) { + console.error(e); + return undefined; + } +} + +async function getWebViewVer(): Promise { + const splatnet3Home = await (await fetch(SPLATNET3_URL)).text(); + + const mainJS = /src="(\/.*?\.js)"/.exec(splatnet3Home)?.[1]; + + if (!mainJS) { + throw new Error("No main.js found"); + } + + const mainJSBody = await (await fetch(SPLATNET3_URL + mainJS)).text(); + + const revision = /"([0-9a-f]{40})"/.exec(mainJSBody)?.[1]; + const version = /revision_info_not_set.*?="(\d+\.\d+\.\d+)/.exec(mainJSBody) + ?.[1]; + + if (!version || !revision) { + throw new Error("No version and revision found"); + } + + const ver = `${version}-${revision.substring(0, 8)}`; + + return ver; +} + +async function getNSOVer(): Promise { + const main = await (await fetch(STORE_URL)).text(); + const ver = />Version (.*?) = {}; + +newValues.WEB_VIEW_VERSION = await printError(getWebViewVer()); +newValues.NSOAPP_VERSION = await printError(getNSOVer()); + +for (const [key, value] of Object.entries(newValues)) { + if (value) { + content = replaceConst(content, key, value); + } +} +await Deno.writeTextFile(CONSTANT_PATH, content); + +console.log("Done"); +console.log("Old:", oldValues); +console.log("New:", newValues);