Open source CapCut alternative.
TypeScript
fix(ci): pull Vercel settings with project-scoped access
David Le22c909df7c0a
diff --git a/.github/workflows/deploy-site.yml b/.github/workflows/deploy-site.ymlindex d30babe4..d1a5ae38 100644--- a/.github/workflows/deploy-site.yml+++ b/.github/workflows/deploy-site.yml@@ -68,7 +68,7 @@ jobs:- name: Pull production settingsenv:VERCEL_TOKEN: ${{ secrets.VERCEL_TOKEN }}- run: npx --yes [email protected] pull --yes --environment=production --token="$VERCEL_TOKEN"+ run: node site/scripts/pull-vercel-build.mjs- name: Prepare build credentialsenv:diff --git a/site/scripts/prepare-vercel-build.mjs b/site/scripts/prepare-vercel-build.mjsindex e712ee58..a5e1eca3 100644--- a/site/scripts/prepare-vercel-build.mjs+++ b/site/scripts/prepare-vercel-build.mjs@@ -28,6 +28,10 @@ export function prepareBuildEnvironment(pulled, credentials) {}delete values[key];}+ return serializeEnvironment(values);+}++export function serializeEnvironment(values) {return Object.entries(values).map(([key, value]) => {// dotenv preserves literal backslashes inside single quotes and backticks.const quote = ["'", "`"].find(candidate => !value.includes(candidate));@@ -40,6 +44,13 @@ if (process.argv[1] === fileURLToPath(import.meta.url)) {const file = new URL("../../.vercel/.env.production.local", import.meta.url);const credentials = JSON.parse(process.env.VERCEL_BUILD_ENV ?? "null");const output = prepareBuildEnvironment(readFileSync(file, "utf8"), credentials);+ if (process.env.GITHUB_ACTIONS === "true") {+ for (const value of Object.values(parse(output))) {+ if (!value) continue;+ const escaped = value.replaceAll("%", "%25").replaceAll("\r", "%0D").replaceAll("\n", "%0A");+ console.log(`::add-mask::${escaped}`);+ }+ }chmodSync(file, 0o600);writeFileSync(file, output, { mode: 0o600 });}diff --git a/site/scripts/pull-vercel-build.mjs b/site/scripts/pull-vercel-build.mjsnew file mode 100644index 00000000..4b217d7c--- /dev/null+++ b/site/scripts/pull-vercel-build.mjs@@ -0,0 +1,43 @@+import { mkdirSync, writeFileSync } from "node:fs";+import { serializeEnvironment } from "./prepare-vercel-build.mjs";++const { VERCEL_TOKEN: token, VERCEL_PROJECT_ID: projectId, VERCEL_ORG_ID: orgId } = process.env;+if (!token || !projectId || !orgId) throw new Error("Missing Vercel deployment credentials");++async function readVercel(path) {+ const url = new URL(path, "https://api.vercel.com");+ url.searchParams.set("teamId", orgId);+ const response = await fetch(url, {+ headers: { Authorization: `Bearer ${token}` },+ signal: AbortSignal.timeout(30_000),+ });+ if (!response.ok) throw new Error(`Vercel ${path} returned HTTP ${response.status}`);+ return response.json();+}++// Project-scoped tokens can read these endpoints. CLI pull also requests the+// team record, which fails: https://github.com/vercel/vercel/issues/17506.+const project = await readVercel(`/v9/projects/${encodeURIComponent(projectId)}`);+if (project.id !== projectId || project.accountId !== orgId || project.rootDirectory !== "site") {+ throw new Error("Vercel project must belong to the configured team and use site/ as its root");+}+const { env } = await readVercel(`/v3/env/pull/${encodeURIComponent(projectId)}/production`);+if (!env || typeof env !== "object" || Object.values(env).some(value => typeof value !== "string")) {+ throw new Error("Vercel returned an invalid production environment");+}++// Match the settings consumed by the pinned Vercel CLI's build command.+const settings = Object.fromEntries([+ "createdAt", "framework", "devCommand", "installCommand", "buildCommand",+ "outputDirectory", "rootDirectory", "directoryListing", "nodeVersion",+].map(key => [key, project[key]]));+if (project.analytics?.id && (!project.analytics.disabledAt || project.analytics.enabledAt > project.analytics.disabledAt)) {+ settings.analyticsId = project.analytics.id;+}+const directory = new URL("../../.vercel/", import.meta.url);+mkdirSync(directory, { recursive: true });+writeFileSync(new URL("project.json", directory), JSON.stringify({+ projectId, orgId, projectName: project.name, settings,+}, null, 2));+writeFileSync(new URL(".env.production.local", directory), serializeEnvironment(env), { mode: 0o600 });+console.log("Downloaded Donkey production build settings");