|
| 1 | +// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 2 | +// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃ |
| 3 | +// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃ |
| 4 | +// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃ |
| 5 | +// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃ |
| 6 | +// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ |
| 7 | +// ┃ Copyright (c) 2017, the Perspective Authors. ┃ |
| 8 | +// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ |
| 9 | +// ┃ This file is part of the Perspective library, distributed under the terms ┃ |
| 10 | +// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
| 11 | +// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 12 | + |
| 13 | +import { execFileSync } from "node:child_process"; |
| 14 | +import fs from "node:fs"; |
| 15 | +import path from "node:path"; |
| 16 | +import url from "node:url"; |
| 17 | + |
| 18 | +const __filename = url.fileURLToPath(import.meta.url); |
| 19 | +const __dirname = path.dirname(__filename); |
| 20 | + |
| 21 | +const TEST_ROOT = path.resolve(__dirname, "..", ".."); |
| 22 | +const CACHE_DIR = path.join(TEST_ROOT, "dist", "git_snapshots"); |
| 23 | +const DEST_DIR = path.join(TEST_ROOT, "dist", "snapshots"); |
| 24 | +const DEFAULT_REF = "master"; |
| 25 | + |
| 26 | +function git(args: string[], cwd: string) { |
| 27 | + execFileSync("git", args, { cwd, stdio: ["ignore", "pipe", "pipe"] }); |
| 28 | +} |
| 29 | + |
| 30 | +function remoteHasRef(remoteUrl: string, ref: string): boolean { |
| 31 | + try { |
| 32 | + const out = execFileSync( |
| 33 | + "git", |
| 34 | + ["ls-remote", "--heads", remoteUrl, ref], |
| 35 | + { stdio: ["ignore", "pipe", "pipe"] }, |
| 36 | + ) |
| 37 | + .toString() |
| 38 | + .trim(); |
| 39 | + return out.length > 0; |
| 40 | + } catch { |
| 41 | + return false; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +function buildRemoteUrl(repo: string, token: string | undefined): string { |
| 46 | + if (token) { |
| 47 | + return `https://x-access-token:${token}@github.com/${repo}.git`; |
| 48 | + } |
| 49 | + return `git@github.com:${repo}.git`; |
| 50 | +} |
| 51 | + |
| 52 | +function mirrorSnapshots(srcRoot: string, dest: string) { |
| 53 | + const srcSnapshots = path.join(srcRoot); |
| 54 | + if (!fs.existsSync(srcSnapshots)) { |
| 55 | + throw new Error( |
| 56 | + `Snapshot clone at ${srcRoot} does not contain dist/snapshots/`, |
| 57 | + ); |
| 58 | + } |
| 59 | + |
| 60 | + fs.rmSync(dest, { recursive: true, force: true }); |
| 61 | + fs.mkdirSync(path.dirname(dest), { recursive: true }); |
| 62 | + fs.cpSync(srcSnapshots, dest, { recursive: true }); |
| 63 | +} |
| 64 | + |
| 65 | +export async function fetchSnapshots(): Promise<void> { |
| 66 | + const repo = process.env.PSP_SNAPSHOT_REPO; |
| 67 | + if (!repo) { |
| 68 | + throw new Error( |
| 69 | + "PSP_SNAPSHOT_REPO is required when fetching snapshots (e.g. 'perspective-dev/perspective-snapshots').", |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + const token = |
| 74 | + process.env.PSP_SNAPSHOT_TOKEN || process.env.GITHUB_TOKEN || undefined; |
| 75 | + const requestedRef = process.env.PSP_SNAPSHOT_REF || DEFAULT_REF; |
| 76 | + const remoteUrl = buildRemoteUrl(repo, token); |
| 77 | + |
| 78 | + let ref = requestedRef; |
| 79 | + if ( |
| 80 | + requestedRef !== DEFAULT_REF && |
| 81 | + !remoteHasRef(remoteUrl, requestedRef) |
| 82 | + ) { |
| 83 | + console.log( |
| 84 | + `Snapshot branch '${requestedRef}' not found on ${repo}; falling back to '${DEFAULT_REF}'.`, |
| 85 | + ); |
| 86 | + ref = DEFAULT_REF; |
| 87 | + } |
| 88 | + |
| 89 | + const cacheGitDir = path.join(CACHE_DIR, ".git"); |
| 90 | + if (fs.existsSync(cacheGitDir)) { |
| 91 | + try { |
| 92 | + git(["remote", "set-url", "origin", remoteUrl], CACHE_DIR); |
| 93 | + git(["fetch", "--depth", "1", "origin", ref], CACHE_DIR); |
| 94 | + git(["checkout", "-B", ref, "FETCH_HEAD"], CACHE_DIR); |
| 95 | + git(["reset", "--hard", "FETCH_HEAD"], CACHE_DIR); |
| 96 | + git(["clean", "-fdx"], CACHE_DIR); |
| 97 | + } catch { |
| 98 | + fs.rmSync(CACHE_DIR, { recursive: true, force: true }); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (!fs.existsSync(cacheGitDir)) { |
| 103 | + fs.mkdirSync(path.dirname(CACHE_DIR), { recursive: true }); |
| 104 | + execFileSync( |
| 105 | + "git", |
| 106 | + [ |
| 107 | + "clone", |
| 108 | + "--depth", |
| 109 | + "1", |
| 110 | + "--filter=blob:none", |
| 111 | + "--branch", |
| 112 | + ref, |
| 113 | + remoteUrl, |
| 114 | + CACHE_DIR, |
| 115 | + ], |
| 116 | + { stdio: ["ignore", "pipe", "pipe"] }, |
| 117 | + ); |
| 118 | + } |
| 119 | + |
| 120 | + console.log(`Fetched snapshots from ${repo}@${ref}`); |
| 121 | + mirrorSnapshots(CACHE_DIR, DEST_DIR); |
| 122 | +} |
| 123 | + |
| 124 | +export async function writebackSnapshots(): Promise<void> { |
| 125 | + if (!fs.existsSync(path.join(CACHE_DIR, ".git"))) { |
| 126 | + console.log( |
| 127 | + `No snapshot clone at ${CACHE_DIR}; skipping writeback. Run with --fetch-snapshots first to populate the cache.`, |
| 128 | + ); |
| 129 | + return; |
| 130 | + } |
| 131 | + if (!fs.existsSync(DEST_DIR)) { |
| 132 | + return; |
| 133 | + } |
| 134 | + fs.cpSync(DEST_DIR, CACHE_DIR, { recursive: true, force: true }); |
| 135 | + console.log(`Copied updated snapshots into ${CACHE_DIR}`); |
| 136 | +} |
0 commit comments