|
| 1 | +#!/usr/bin/env node |
| 2 | +// Vendors skills from the upstream coder/skills repository into |
| 3 | +// plugins/coder/skills/. Run manually when bumping the pin: update `pin` |
| 4 | +// in sync-skills-vendor.json, run this script, then commit both together. |
| 5 | +// |
| 6 | +// Usage: |
| 7 | +// node .github/scripts/sync-skills.mjs |
| 8 | +// |
| 9 | +// Steps: |
| 10 | +// 1. Read sync-skills-vendor.json for the repo, ref, and paths. |
| 11 | +// 2. Download the tarball from codeload.github.com (public, no auth). |
| 12 | +// 3. Extract it into a temp directory. |
| 13 | +// 4. Replace each listed path under plugins/coder/, removing the previous |
| 14 | +// copy first so stale files never survive a sync. |
| 15 | +// |
| 16 | +// The pin is the single source of truth. There is no runtime override. |
| 17 | + |
| 18 | +import { promises as fs, createWriteStream } from "node:fs"; |
| 19 | +import { Readable } from "node:stream"; |
| 20 | +import { pipeline } from "node:stream/promises"; |
| 21 | +import path from "node:path"; |
| 22 | +import { spawnSync } from "node:child_process"; |
| 23 | +import { tmpdir } from "node:os"; |
| 24 | +import { fileURLToPath } from "node:url"; |
| 25 | + |
| 26 | +const scriptDir = path.dirname(fileURLToPath(import.meta.url)); |
| 27 | +const repoRoot = path.resolve(scriptDir, "..", ".."); |
| 28 | +const pluginDir = path.join(repoRoot, "plugins", "coder"); |
| 29 | +const vendorFile = path.join(scriptDir, "sync-skills-vendor.json"); |
| 30 | + |
| 31 | +async function fileExists(filePath) { |
| 32 | + try { |
| 33 | + await fs.access(filePath); |
| 34 | + return true; |
| 35 | + } catch { |
| 36 | + return false; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +async function downloadTarball(repo, ref, destPath) { |
| 41 | + const url = `https://codeload.github.com/${repo}/tar.gz/${encodeURIComponent(ref)}`; |
| 42 | + const res = await fetch(url, { redirect: "follow" }); |
| 43 | + if (!res.ok) { |
| 44 | + throw new Error(`Could not download ${repo}@${ref} (HTTP ${res.status})`); |
| 45 | + } |
| 46 | + await pipeline(Readable.fromWeb(res.body), createWriteStream(destPath)); |
| 47 | + console.log(` fetched ${url}`); |
| 48 | +} |
| 49 | + |
| 50 | +// GitHub tarballs contain exactly one top-level directory named after the |
| 51 | +// repo and ref. Return it so callers know where the tree lives. |
| 52 | +async function extractTarball(tarballPath, intoDir) { |
| 53 | + await fs.mkdir(intoDir, { recursive: true }); |
| 54 | + const result = spawnSync("tar", ["-xzf", tarballPath, "-C", intoDir], { |
| 55 | + stdio: "inherit", |
| 56 | + }); |
| 57 | + if (result.status !== 0) { |
| 58 | + throw new Error(`tar exited with status ${result.status}`); |
| 59 | + } |
| 60 | + const [topLevel] = await fs.readdir(intoDir); |
| 61 | + return path.join(intoDir, topLevel); |
| 62 | +} |
| 63 | + |
| 64 | +async function copyPath(fromDir, toDir, relativePath) { |
| 65 | + const from = path.join(fromDir, relativePath); |
| 66 | + const to = path.join(toDir, relativePath); |
| 67 | + if (!(await fileExists(from))) { |
| 68 | + throw new Error(`path missing in upstream tarball: ${relativePath}`); |
| 69 | + } |
| 70 | + await fs.rm(to, { recursive: true, force: true }); |
| 71 | + await fs.mkdir(path.dirname(to), { recursive: true }); |
| 72 | + await fs.cp(from, to, { recursive: true }); |
| 73 | + console.log(` ${relativePath} -> ${path.relative(repoRoot, to)}`); |
| 74 | +} |
| 75 | + |
| 76 | +async function main() { |
| 77 | + const vendor = JSON.parse(await fs.readFile(vendorFile, "utf8")); |
| 78 | + const { repo, pin, paths } = vendor; |
| 79 | + if (!repo || !pin || !Array.isArray(paths) || paths.length === 0) { |
| 80 | + throw new Error(`${vendorFile} must define repo, pin, and a non-empty paths array`); |
| 81 | + } |
| 82 | + |
| 83 | + console.log(`Syncing ${repo}@${pin}`); |
| 84 | + const workDir = await fs.mkdtemp(path.join(tmpdir(), "coder-skills-sync-")); |
| 85 | + try { |
| 86 | + const tarball = path.join(workDir, "upstream.tar.gz"); |
| 87 | + await downloadTarball(repo, pin, tarball); |
| 88 | + const extracted = await extractTarball(tarball, path.join(workDir, "extract")); |
| 89 | + for (const relativePath of paths) { |
| 90 | + await copyPath(extracted, pluginDir, relativePath); |
| 91 | + } |
| 92 | + } finally { |
| 93 | + await fs.rm(workDir, { recursive: true, force: true }); |
| 94 | + } |
| 95 | + console.log("Done. Review the diff, bump the plugin version, and commit."); |
| 96 | +} |
| 97 | + |
| 98 | +main().catch((error) => { |
| 99 | + console.error(error.message); |
| 100 | + process.exit(1); |
| 101 | +}); |
0 commit comments