From b298496911cd5acad9301a6444d4d5fbc7da93b9 Mon Sep 17 00:00:00 2001 From: illusia <64828020+EchoSixHIYA@users.noreply.github.com> Date: Fri, 4 Sep 2026 08:27:28 +0800 Subject: [PATCH] fix: make sdk release preparation deterministic --- .github/workflows/build-release.yml | 10 ++++-- .github/workflows/ci.yml | 4 ++- Dockerfile | 5 ++- package.json | 3 +- scripts/prepare-sdk.mjs | 48 +++++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 scripts/prepare-sdk.mjs diff --git a/.github/workflows/build-release.yml b/.github/workflows/build-release.yml index aa39e53..60ae5fb 100644 --- a/.github/workflows/build-release.yml +++ b/.github/workflows/build-release.yml @@ -34,7 +34,11 @@ jobs: run: | $ErrorActionPreference = "Stop" $releaseVersion = $env:RELEASE_REF -replace '^v', '' - npm ci --no-audit --no-fund + npm ci --ignore-scripts --no-audit --no-fund + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + npm run prepare:sdk + if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } + npm rebuild @discordjs/opus --foreground-scripts --no-audit --no-fund if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } npm --prefix web ci --no-audit --no-fund if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE } @@ -130,7 +134,9 @@ jobs: run: | set -euo pipefail release_version="${RELEASE_REF#v}" - npm ci --no-audit --no-fund + npm ci --ignore-scripts --no-audit --no-fund + npm run prepare:sdk + npm rebuild @discordjs/opus --foreground-scripts --no-audit --no-fund npm --prefix web ci --no-audit --no-fund current_version="$(node -p "require('./package.json').version")" if [ "$current_version" != "$release_version" ]; then diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 49cc7b6..ef3dcfc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -22,7 +22,9 @@ jobs: web/package-lock.json - name: Install dependencies run: | - npm ci --no-audit --no-fund + npm ci --ignore-scripts --no-audit --no-fund + npm run prepare:sdk + npm rebuild @discordjs/opus --foreground-scripts --no-audit --no-fund npm --prefix web ci --no-audit --no-fund - name: Test and build run: | diff --git a/Dockerfile b/Dockerfile index 8a3d10a..f071707 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,7 +7,10 @@ RUN apt-get update \ COPY package.json package-lock.json ./ COPY web/package.json web/package-lock.json ./web/ -RUN npm ci +RUN npm ci --ignore-scripts --no-audit --no-fund +COPY scripts/prepare-sdk.mjs ./scripts/prepare-sdk.mjs +RUN npm run prepare:sdk +RUN npm rebuild @discordjs/opus --foreground-scripts --no-audit --no-fund RUN npm --prefix web ci COPY . . diff --git a/package.json b/package.json index 451d9ed..9c8e762 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,8 @@ "test": "tsx --test \"test/**/*.test.ts\"", "start": "node dist/index.js", "web:dev": "cd web && npx vite --host", - "web:build": "cd web && npx vite build" + "web:build": "cd web && npx vite build", + "prepare:sdk": "node scripts/prepare-sdk.mjs" }, "dependencies": { "@discordjs/opus": "^0.10.0", diff --git a/scripts/prepare-sdk.mjs b/scripts/prepare-sdk.mjs new file mode 100644 index 0000000..e2f46ee --- /dev/null +++ b/scripts/prepare-sdk.mjs @@ -0,0 +1,48 @@ +import { cpSync, existsSync, mkdtempSync, mkdirSync, readdirSync, readFileSync, rmSync } from "node:fs"; +import { execFileSync } from "node:child_process"; +import { tmpdir } from "node:os"; +import { dirname, join } from "node:path"; +import process from "node:process"; + +const root = process.cwd(); +const sdkPackage = "@echosixhiya/teamspeak-client"; +const packageJson = JSON.parse(readFileSync(join(root, "package.json"), "utf8")); +const dependency = packageJson.dependencies?.[sdkPackage]; +const match = typeof dependency === "string" ? /^git\+(.+?)#(.+)$/.exec(dependency) : null; + +if (!match) { + throw new Error(`${sdkPackage} must be pinned to a Git repository commit`); +} + +const [, repository, commit] = match; +const temporaryRoot = mkdtempSync(join(tmpdir(), "webspeak-sdk-build-")); +const destination = join(root, "node_modules", "@echosixhiya", "teamspeak-client", "dist"); + +try { + execFileSync("git", ["clone", "--no-checkout", repository, temporaryRoot], { stdio: "inherit" }); + execFileSync("git", ["checkout", "--detach", commit], { cwd: temporaryRoot, stdio: "inherit" }); + const npmOptions = { cwd: temporaryRoot, stdio: "inherit" }; + const runNpm = (args) => { + if (process.platform === "win32") { + execFileSync(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", `npm.cmd ${args.join(" ")}`], npmOptions); + } else { + execFileSync("npm", args, npmOptions); + } + }; + runNpm(["install", "--ignore-scripts", "--no-audit", "--no-fund"]); + runNpm(["run", "build"]); + + const source = join(temporaryRoot, "dist"); + if (!existsSync(join(source, "index.mjs"))) { + throw new Error(`SDK build did not produce ${join(source, "index.mjs")}`); + } + + mkdirSync(dirname(destination), { recursive: true }); + mkdirSync(destination, { recursive: true }); + for (const entry of readdirSync(source)) { + cpSync(join(source, entry), join(destination, entry), { recursive: true, force: true }); + } + console.log(`Prepared ${sdkPackage} from ${commit}`); +} finally { + rmSync(temporaryRoot, { recursive: true, force: true }); +}