|
| 1 | +#!/usr/bin/env nix-shell |
| 2 | +/* |
| 3 | +#!nix-shell -i node --pure --packages cacert nodejs yq-go |
| 4 | +*/ |
| 5 | +import * as assert from "node:assert/strict"; |
| 6 | +import * as child_process from "node:child_process"; |
| 7 | +import * as fsPromises from "node:fs/promises"; |
| 8 | +import * as path from "node:path"; |
| 9 | + |
| 10 | +const __dirname = import.meta.dirname; |
| 11 | + |
| 12 | +/** @typedef {{ |
| 13 | + version: string; |
| 14 | + path: `${string}.zip`; |
| 15 | + sha512: string; |
| 16 | + releaseDate: string; |
| 17 | + files: Array<{ url: string; sha512: string; size: number }>; |
| 18 | +}} LiveCheckInfo */ |
| 19 | + |
| 20 | +/** @typedef {{ |
| 21 | + version: string; |
| 22 | + url: string; |
| 23 | + hash: `sha512-${string}`; |
| 24 | +}} Info */ |
| 25 | + |
| 26 | +/** @typedef {{ |
| 27 | + "x86_64-darwin": Info; |
| 28 | + "aarch64-darwin": Info; |
| 29 | +}} InfoMap */ |
| 30 | + |
| 31 | +const BASE_URL = "https://desktop-release.notion-static.com/"; |
| 32 | +/** |
| 33 | + * |
| 34 | + * @param {"latest-mac.yml" | "arm64-mac.yml"} liveCheckFile |
| 35 | + * @returns {Promise<Info>} |
| 36 | + */ |
| 37 | +async function getInfo(liveCheckFile) { |
| 38 | + const url = /** @type {const} */ (`${BASE_URL}${liveCheckFile}`); |
| 39 | + const response = await fetch(url); |
| 40 | + assert.ok(response.ok, `Failed to fetch ${url}`); |
| 41 | + /** @type {LiveCheckInfo} */ |
| 42 | + const { version, path, sha512 } = JSON.parse( |
| 43 | + child_process |
| 44 | + .execSync(`yq eval --output-format=json`, { |
| 45 | + input: Buffer.from(await response.arrayBuffer()), |
| 46 | + }) |
| 47 | + .toString() |
| 48 | + ); |
| 49 | + assert.ok(version, "version is required"); |
| 50 | + assert.ok(path, "path is required"); |
| 51 | + assert.ok(sha512, "sha512 is required"); |
| 52 | + return { |
| 53 | + version, |
| 54 | + url: BASE_URL + path, |
| 55 | + hash: `sha512-${sha512}`, |
| 56 | + }; |
| 57 | +} |
| 58 | + |
| 59 | +async function main() { |
| 60 | + const filePath = path.join(__dirname, "../info.json"); |
| 61 | + /** @type {InfoMap} */ |
| 62 | + const oldInfo = JSON.parse( |
| 63 | + await fsPromises.readFile(filePath, { encoding: "utf-8" }) |
| 64 | + ); |
| 65 | + /** @type {InfoMap} */ |
| 66 | + const info = { |
| 67 | + "x86_64-darwin": await getInfo("latest-mac.yml"), |
| 68 | + "aarch64-darwin": await getInfo("arm64-mac.yml"), |
| 69 | + }; |
| 70 | + if (JSON.stringify(oldInfo) === JSON.stringify(info)) { |
| 71 | + console.log("[update] No updates found"); |
| 72 | + return; |
| 73 | + } |
| 74 | + const platforms = /** @type {const} */ (["x86_64-darwin", "aarch64-darwin"]); |
| 75 | + for (const platform of platforms) { |
| 76 | + console.log( |
| 77 | + `[update] Updating Notion ${platform} ${oldInfo[platform].version} -> ${info[platform].version}` |
| 78 | + ); |
| 79 | + } |
| 80 | + await fsPromises.writeFile( |
| 81 | + filePath, |
| 82 | + JSON.stringify(info, null, 2) + "\n", |
| 83 | + "utf-8" |
| 84 | + ); |
| 85 | + console.log("[update] Updating Notion complete"); |
| 86 | +} |
| 87 | + |
| 88 | +main(); |
0 commit comments