From 1dcae0c20a9f0265c6d1dd7360cfa1598e6fea05 Mon Sep 17 00:00:00 2001 From: Pihkaal Date: Wed, 31 Jul 2024 15:12:43 +0200 Subject: [PATCH] feat(build): handle offline --- build/manifestPlugin.ts | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/build/manifestPlugin.ts b/build/manifestPlugin.ts index 963961b..4003480 100644 --- a/build/manifestPlugin.ts +++ b/build/manifestPlugin.ts @@ -1,8 +1,9 @@ import { Plugin } from "vite"; import { readFile, writeFile } from "fs/promises"; import { spawnSync } from "child_process"; -import { Octokit } from "@octokit/rest"; +import { Octokit, RestEndpointMethodTypes } from "@octokit/rest"; import { env } from "./env"; +import { existsSync } from "fs"; type Manifest = { files: string[]; @@ -26,11 +27,23 @@ export const manifest = (): Plugin => ({ name: "generate-pages-plugin", buildStart: async () => { const octokit = new Octokit({ auth: env.GITHUB_PAT }); + let manifestRepo: RestEndpointMethodTypes["repos"]["get"]["response"]["data"]; + + try { + const { data } = await octokit.repos.get({ + owner: env.GITHUB_USERNAME, + repo: env.GITHUB_USERNAME, + }); + manifestRepo = data; + } catch { + if (existsSync("./node_modules/.cache/assets")) { + console.warn("WARNING: Can't update assets, using cached ones"); + return; + } else { + throw new Error("Can't update assets, nothing cached"); + } + } - const { data: manifestRepo } = await octokit.repos.get({ - owner: env.GITHUB_USERNAME, - repo: env.GITHUB_USERNAME, - }); try { const storedUpdatedAt = ( await readFile("./node_modules/.cache/assets")