| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823 |
- #!/usr/bin/env node
- // zcode-site:ZCode Sites 的命令行客户端(技术方案 4.2、4.3、5.3)。
- // deploy 的输入是 Agent 准备好的 stage 目录,打包与契约检查、以及本地命令 check 在 stage.mjs;其余只和云端交互,不探测项目。
- // 命令、参数、输出和错误码的完整说明只写在 HELP 里(`zcode-site help`),site-deploy skill 引用它,不再重复。
- //
- // 输出契约:成功时 stdout 输出 JSON,退出码 0;deploy 过程中每个阶段和每条日志各输出一行 JSON(--quiet 只输出终态)。
- // 失败时 stderr 输出一行 {"error":{code,message,hint}},退出码 1=参数或本地文件错误、2=未登录、3=超出额度或限额、4=部署失败或服务端错误。
- //
- // 鉴权(技术方案 5.8):CLI 不持有任何登录凭证。请求发给 ZCode agent 侧的官方命令端点,端点按
- // session token 认出会话,补上 ZCode 身份头和 X-ZCode-App-Version 后转发给 zcode-server。
- // 端点地址和 token 由 ZCode 注入当前会话的 Bash,在普通终端里运行会按未登录退出:
- // ZCODE_OFFICIAL_ENDPOINT 端点地址(macOS / Linux 是 UDS 路径,Windows 是命名管道)
- // ZCODE_OFFICIAL_ENDPOINT_TOKEN 本会话的 token
- import { realpathSync } from "node:fs";
- import { request as httpRequest } from "node:http";
- import { readFile, writeFile } from "node:fs/promises";
- import { basename, join } from "node:path";
- import { pathToFileURL } from "node:url";
- import { parseArgs } from "node:util";
- import { checkStage, packStage } from "./stage.mjs";
- const BINDING_FILE = ".zcode-site.json";
- const POLL_INTERVAL_MS = 2000;
- // 服务端兜底清理会把 publishing / verifying 超 10 分钟的部署置为 failed,这里多等一点再放弃。
- const POLL_TIMEOUT_MS = 15 * 60 * 1000;
- const UPLOAD_ATTEMPTS = 3;
- const IN_FLIGHT = new Set(["preparing", "uploading", "publishing", "verifying"]);
- const RESUMABLE = new Set(["preparing", "uploading"]);
- // 与 api/openapi.yaml 的 x-cli-exit-code 一致。
- const EXIT_BY_CODE = {
- 3001: 1,
- 3002: 3,
- 3101: 2,
- 3601: 1,
- 3602: 1,
- 3603: 4,
- 3604: 4,
- 3605: 3,
- 3607: 3,
- 3608: 3,
- 3609: 1,
- 3610: 3,
- 3611: 1,
- 3612: 3,
- 2001: 4,
- 2005: 4,
- 2007: 4,
- };
- // 错误体不带提示文案(技术方案 5.8),面向 Agent 的 message / hint 在这里按 code 生成。
- const MESSAGES = {
- 3001: ["Invalid request", "Check the command arguments."],
- 3002: [
- "Too many deployments in the last hour",
- "Wait and retry later; the limit is 10 deployments per hour.",
- ],
- // MaaS JWT 过期时 zcode-server 不按 free 放行,以免付费用户被降级(契约 0.13.0)。
- 3101: [
- "The ZCode coding plan sign-in has expired",
- "Ask the user to sign in to ZCode again, then retry.",
- ],
- 3601: ["Site not found", "Run `zcode-site list` to see your sites, then pass --site <id>."],
- 3602: [
- "Deployment not found",
- "Run `zcode-site status` to see the latest deployment of this site.",
- ],
- 3603: [
- "The deployment cannot do this in its current state",
- "Start a new deployment with `zcode-site deploy`.",
- ],
- 3604: [
- "The package upload is incomplete",
- "Run `zcode-site deploy` again to re-upload the package.",
- ],
- 3605: [
- "Account quota exceeded",
- "New deployments are paused until the next period. Tell the user that upgrading their ZCode plan raises the limits; the Sites panel in ZCode desktop shows the usage.",
- ],
- 3607: [
- "The package is larger than 200MB",
- "Exclude build caches, source maps and dev dependencies from the stage, then deploy again.",
- ],
- 3608: [
- "The current plan cannot hide the ZCode badge",
- "Free plans always show the badge; tell the user to upgrade to a paid ZCode plan, then run `zcode-site badge hide` again.",
- ],
- 3609: ["This ZCode version is too old for Sites", "Ask the user to update ZCode."],
- 3610: [
- "Another deployment is in progress",
- "Wait for it to finish (`zcode-site status --site <siteId>`), then retry.",
- ],
- // 名称和域名前缀在服务端过内容安全机审(契约 0.15.0);只有名称不通过才报错,前缀不通过时服务端换成 site。
- 3611: [
- "The site name did not pass content review",
- "Pick a different display name (--name for a new site, `zcode-site rename` for an existing one) and retry; nothing was created or renamed.",
- ],
- // 部署上线后过内容审核(契约 0.16.0):人审驳回当前线上版本时站点被下线(offlineReason=review_rejected),用户不能自己改回上线。
- 3612: [
- "The live version of this site was rejected by content review",
- "The site was taken offline and can't be brought back online with this version. Change the content and deploy again; a successful deploy puts the site back online.",
- ],
- 2001: ["Sites service error", "Retry later."],
- 2005: ["Sites storage error", "Retry later."],
- 2007: ["Sites upstream error", "Retry later."],
- };
- class CliError extends Error {
- constructor(code, message, hint, exitCode, data) {
- super(message);
- this.code = code;
- this.hint = hint;
- this.exitCode = exitCode;
- this.data = data;
- }
- }
- function apiError(status, body) {
- if (status === 401) {
- return new CliError(
- "UNAUTHENTICATED",
- "Not signed in to ZCode",
- "Ask the user to sign in to ZCode, then retry.",
- 2,
- );
- }
- const code = body?.code;
- const [message, hint] = MESSAGES[code] ?? [`Sites API error (HTTP ${status})`, "Retry later."];
- return new CliError(code ?? `HTTP_${status}`, message, hint, EXIT_BY_CODE[code] ?? 4, body?.data);
- }
- function localError(code, message, hint) {
- return new CliError(code, message, hint, 1);
- }
- // ---------- 鉴权与 API ----------
- const ENDPOINT_ORIGIN = "http://zcode-official-endpoint";
- // 端点自己产生的错误经这个响应头说明类型;network 表示端点连不上 zcode-server。
- const ENDPOINT_ERROR_HEADER = "x-zcode-official-endpoint-error";
- function loadEndpoint(env) {
- const address = env.ZCODE_OFFICIAL_ENDPOINT?.trim();
- const token = env.ZCODE_OFFICIAL_ENDPOINT_TOKEN?.trim();
- if (!address || !token) {
- throw new CliError(
- "UNAUTHENTICATED",
- "ZCode credentials are not available",
- "Run zcode-site from a ZCode session (the Sites plugin must be enabled), then retry.",
- 2,
- );
- }
- return { address, token };
- }
- // 经 UDS / 命名管道发 HTTP 请求,返回标准 Response,和 fetch 同形状(测试注入 fetchImpl 替换它)。
- export function createEndpointFetch(address) {
- return (url, init = {}) =>
- new Promise((resolve, reject) => {
- const { pathname, search } = new URL(url);
- const headers = { ...init.headers };
- if (init.body !== undefined) headers["Content-Length"] = String(Buffer.byteLength(init.body));
- const req = httpRequest(
- { socketPath: address, path: pathname + search, method: init.method ?? "GET", headers },
- (res) => {
- const chunks = [];
- res.on("data", (chunk) => chunks.push(chunk));
- res.on("error", reject);
- res.on("aborted", () =>
- reject(new Error("the connection was closed before the response ended")),
- );
- res.on("end", () => {
- if (!res.complete)
- return reject(new Error("the connection was closed before the response ended"));
- const responseHeaders = new Headers();
- for (const [name, value] of Object.entries(res.headers)) {
- if (value !== undefined)
- responseHeaders.set(name, Array.isArray(value) ? value.join(", ") : value);
- }
- const status = res.statusCode ?? 502;
- const nullBody = status === 204 || status === 205 || status === 304;
- resolve(
- new Response(nullBody ? null : Buffer.concat(chunks), {
- status,
- headers: responseHeaders,
- }),
- );
- });
- },
- );
- req.on("error", reject);
- req.end(init.body);
- });
- }
- function createClient(endpoint, fetchImpl) {
- // body 是 JSON 对象;Buffer 原样作为请求体发送(产物包),按长度带上 Content-Length。
- async function call(method, path, body) {
- const headers = { Authorization: `Bearer ${endpoint.token}` };
- const raw = Buffer.isBuffer(body);
- if (body !== undefined) headers["Content-Type"] = raw ? "application/gzip" : "application/json";
- let res;
- try {
- res = await fetchImpl(ENDPOINT_ORIGIN + path, {
- method,
- headers,
- body: body === undefined || raw ? body : JSON.stringify(body),
- });
- } catch (err) {
- throw new CliError(
- "NETWORK_ERROR",
- `Cannot reach the Sites API: ${err.message}`,
- "Check the network connection to ZCode and retry.",
- 4,
- );
- }
- if (res.headers.get(ENDPOINT_ERROR_HEADER) === "network") {
- throw new CliError(
- "NETWORK_ERROR",
- "ZCode cannot reach the Sites API",
- "Check the network connection and retry.",
- 4,
- );
- }
- const text = await res.text();
- let parsed;
- try {
- parsed = text ? JSON.parse(text) : undefined;
- } catch {
- parsed = undefined;
- }
- if (!res.ok || parsed?.code !== 0) throw apiError(res.status, parsed);
- return parsed.data;
- }
- return {
- listSites: (cursor) =>
- call(
- "GET",
- `/api/v1/sites?limit=100${cursor ? `&cursor=${encodeURIComponent(cursor)}` : ""}`,
- ),
- // domainPrefix 不传时服务端按 name 规范化(openapi createSite)。
- createSite: (name, domainPrefix) =>
- call("POST", "/api/v1/sites", domainPrefix === undefined ? { name } : { name, domainPrefix }),
- getSite: (id) => call("GET", `/api/v1/sites/${id}`),
- updateSite: (id, patch) => call("PATCH", `/api/v1/sites/${id}`, patch),
- createDeployment: (siteId, artifactSize) =>
- call("POST", `/api/v1/sites/${siteId}/deployments`, { artifactSize }),
- getDeployment: (siteId, id) => call("GET", `/api/v1/sites/${siteId}/deployments/${id}`),
- uploadPackage: (siteId, id, body) =>
- call("PUT", `/api/v1/sites/${siteId}/deployments/${id}/package`, body),
- logs: (siteId, id, offset) =>
- call("GET", `/api/v1/sites/${siteId}/deployments/${id}/logs?offset=${offset}`),
- usage: () => call("GET", "/api/v1/sites/usage"),
- };
- }
- // ---------- 站点绑定(技术方案 4.3) ----------
- async function readBinding(cwd) {
- let text;
- try {
- text = await readFile(join(cwd, BINDING_FILE), "utf8");
- } catch (err) {
- if (err.code === "ENOENT") return undefined;
- throw err;
- }
- try {
- const b = JSON.parse(text);
- if (typeof b.siteId === "string" && b.siteId) return b;
- } catch {
- // 落到下面统一报错
- }
- throw localError(
- "INVALID_BINDING",
- `${BINDING_FILE} is not valid`,
- `Fix or delete ${BINDING_FILE}, or pass --site <id>.`,
- );
- }
- async function writeBinding(cwd, site) {
- await writeFile(
- join(cwd, BINDING_FILE),
- `${JSON.stringify({ siteId: site.id, domainLabel: site.domainLabel }, null, 2)}\n`,
- );
- }
- async function allSites(client) {
- const items = [];
- let cursor;
- do {
- const page = await client.listSites(cursor);
- items.push(...page.items);
- cursor = page.nextCursor;
- } while (cursor);
- return items;
- }
- function candidates(sites) {
- return sites.map((s) => ({ id: s.id, name: s.name, url: s.url, status: s.status }));
- }
- // resolveSite 的规则:--site > 绑定文件 > 账户下没有站点时(只限 deploy)新建;已有站点时报错并给出候选,不按名字匹配,也不自动新建。
- async function resolveSite(ctx, { allowCreate }) {
- const { client, cwd, opts } = ctx;
- if (opts.site) return { site: await client.getSite(opts.site), bound: false };
- const binding = await readBinding(cwd);
- if (binding) return { site: await client.getSite(binding.siteId), bound: true };
- if (allowCreate && opts.new)
- return {
- site: await client.createSite(siteName(ctx), opts.prefix),
- bound: false,
- created: true,
- };
- const sites = await allSites(client);
- if (allowCreate && sites.length === 0)
- return {
- site: await client.createSite(siteName(ctx), opts.prefix),
- bound: false,
- created: true,
- };
- throw new CliError(
- "SITE_NOT_SELECTED",
- sites.length ? "This project is not linked to a site" : "You have no sites yet",
- allowCreate
- ? "Ask the user which site to update, then retry with --site <id>, or with --new to create a new site."
- : "Pass --site <id>, or deploy first.",
- 1,
- { candidates: candidates(sites) },
- );
- }
- function siteName(ctx) {
- return (ctx.opts.name ?? basename(ctx.cwd)).trim() || "site";
- }
- // normalizePrefix 与服务端 site.NormalizePrefix 同一规则(技术方案 5.4),只用来提前告诉 Agent 实际会得到的前缀;
- // 发给服务端的仍是原值,以服务端规范化的结果为准。
- export function normalizePrefix(prefix) {
- let s = prefix
- .toLowerCase()
- .replace(/[^a-z0-9]+/g, "-")
- .replace(/^-+|-+$/g, "");
- if (s.length > 30) s = s.slice(0, 30).replace(/-+$/, "");
- return s.length < 4 ? "site" : s;
- }
- // --prefix 只在本次新建站点时生效;不合规时提示规范化结果,但不拦截,交给服务端兜底。
- // 前缀(没有 --prefix 时是名称)没有通过服务端机审时,服务端换成 site,按返回的 domainLabel 提示(契约 0.15.0)。
- function prefixNotice(opts, created, site) {
- if (!created)
- return opts.prefix === undefined
- ? undefined
- : "--prefix is ignored: the site already exists and its domain never changes.";
- const expected = normalizePrefix(opts.prefix ?? site.name);
- if (expected !== "site" && !site.domainLabel.startsWith(`${expected}-`))
- return `The domain prefix ${JSON.stringify(expected)} did not pass content review, so the server used "site": the domain is ${site.url} and never changes.`;
- if (opts.prefix === undefined) return undefined;
- const normalized = expected;
- if (normalized === opts.prefix) return undefined;
- return normalized === "site"
- ? `--prefix ${JSON.stringify(opts.prefix)} has fewer than 4 usable [a-z0-9-] characters; the server falls back to "site".`
- : `--prefix ${JSON.stringify(opts.prefix)} is normalized to ${JSON.stringify(normalized)}.`;
- }
- // ---------- deploy ----------
- async function deploy(ctx, stageDir) {
- const { client, out, opts } = ctx;
- if (!stageDir)
- throw localError(
- "MISSING_STAGE",
- "Missing stage directory",
- "Usage: zcode-site deploy <stage-dir> [--new | --site <id>]",
- );
- if (opts.site && opts.new)
- throw localError(
- "INVALID_ARGS",
- "--site and --new cannot be used together",
- "Pass only one of them.",
- );
- // 先打包再选站点:契约检查不过时不建站、不建部署。
- const packed = await packStage(stageDir, {
- cwd: ctx.cwd,
- allowForeignBinaries: opts["allow-foreign-binaries"],
- });
- if (packed.error)
- throw localError(packed.error, packed.message, "Pass the stage directory that holds start.sh.");
- if (packed.problems)
- throw new CliError(
- "PACKAGE_CHECK_FAILED",
- `The stage does not meet the package contract:\n${packed.problems.map((p) => ` - ${p}`).join("\n")}`,
- "Fix every listed problem in the stage (see the site-deploy skill references), then deploy again.",
- 1,
- { problems: packed.problems },
- );
- out.event({ event: "packed", ...packed.result });
- for (const message of packed.warnings) out.event({ event: "warning", message });
- const { site, bound, created } = await resolveSite(ctx, { allowCreate: true });
- if (!bound) await writeBinding(ctx.cwd, site);
- if (created) out.event({ event: "site_created", siteId: site.id, url: site.url });
- // 已绑定时 --new 不生效:首次部署统一带 --new,后续部署即使误带也只会更新已绑定的站点,不会重复建站。
- if (bound && opts.new)
- out.event({
- event: "warning",
- message: `--new is ignored: this project is linked to site ${site.id} (${BINDING_FILE}).`,
- });
- const notice = prefixNotice(opts, created, site);
- if (notice) out.event({ event: "warning", message: notice });
- const deployment = await startDeployment(client, site.id, packed.body.length);
- out.event({
- event: "deployment_started",
- siteId: site.id,
- deploymentId: deployment.id,
- resumed: deployment.resumed,
- });
- await uploadPackage(ctx, site.id, deployment.id, packed.body);
- const final = await watch(ctx, site.id, deployment.id);
- const result = {
- siteId: site.id,
- deploymentId: final.id,
- status: final.status,
- url: site.url,
- failure: final.failure,
- };
- if (final.status !== "success") {
- throw new CliError(
- final.failure?.code ?? "DEPLOYMENT_FAILED",
- `Deployment failed at ${final.failure?.stage ?? "unknown"}: ${final.failure?.message ?? final.status}`,
- deployHint(final.failure?.code),
- 4,
- result,
- );
- }
- return result;
- }
- // 同一用户同时只能有一个进行中的部署。3610 指向本站点、且还没开始发布时,多半是上一次请求成功了但响应丢了,直接续传(技术方案 5.3)。
- async function startDeployment(client, siteId, size) {
- try {
- return { ...(await client.createDeployment(siteId, size)), resumed: false };
- } catch (err) {
- const p = err.data?.inProgress;
- if (err.code === 3610 && p && p.siteId === siteId && RESUMABLE.has(p.status)) {
- const d = await client.getDeployment(siteId, p.deploymentId);
- if (d.artifactSize === size) return { ...d, resumed: true };
- }
- if (err.code === 3610 && p)
- err.hint = `Deployment ${p.deploymentId} of site ${p.siteId} is ${p.status}. Wait for it to finish, then retry.`;
- throw err;
- }
- }
- // 包经 zcode-server 转发给控制面,由控制面写 OSS 并进入 publishing(openapi uploadPackage),CLI 不直接访问 OSS。
- // 断网或 3604(传输中断、长度不符)时用同一个请求整包重传;3603 说明上一次其实已经传完、只是响应丢了,直接进入轮询。
- async function uploadPackage(ctx, siteId, deploymentId, body) {
- const { client, out } = ctx;
- for (let attempt = 1; ; attempt++) {
- out.event({ event: "uploading", attempt, bytes: body.length });
- try {
- return await client.uploadPackage(siteId, deploymentId, body);
- } catch (err) {
- if (err.code === 3603) {
- const d = await client.getDeployment(siteId, deploymentId);
- if (!RESUMABLE.has(d.status)) return d;
- throw err;
- }
- const retryable = err.code === 3604 || err.code === "NETWORK_ERROR";
- if (!retryable || attempt >= UPLOAD_ATTEMPTS) throw err;
- }
- }
- }
- async function watch(ctx, siteId, deploymentId) {
- const { client, out, sleep, now } = ctx;
- const deadline = now() + POLL_TIMEOUT_MS;
- let offset = 0;
- let lastStatus;
- const drainLogs = async () => {
- const logs = await client.logs(siteId, deploymentId, offset);
- for (const e of logs.entries)
- out.event({ event: "log", stage: e.stage, level: e.level, message: e.message, ts: e.ts });
- offset = logs.nextOffset;
- };
- for (;;) {
- const d = await client.getDeployment(siteId, deploymentId);
- // 先读状态再读日志:状态已到终态时,这一轮读到的日志一定是全部。
- await drainLogs();
- if (d.status !== lastStatus) {
- out.event({ event: "status", status: d.status });
- lastStatus = d.status;
- }
- if (!IN_FLIGHT.has(d.status)) return d;
- if (now() > deadline) {
- throw new CliError(
- "WATCH_TIMEOUT",
- "Timed out waiting for the deployment to finish",
- `It is still ${d.status}; check later with \`zcode-site status --site ${siteId}\`.`,
- 4,
- { siteId, deploymentId },
- );
- }
- await sleep(POLL_INTERVAL_MS);
- }
- }
- function deployHint(code) {
- switch (code) {
- case "PACKAGE_INVALID":
- return "Fix the stage as described in the message (see the site-deploy skill's packaging reference) and deploy again.";
- case "WARMUP_FAILED":
- return "start.sh did not answer GET / on 0.0.0.0:3000 with a status below 500 in time. Read `zcode-site logs <deploymentId>`, run `zcode-site check <stage-dir>`, fix it and deploy again.";
- case "UPLOAD_TIMEOUT":
- return "The upload did not finish in time. Run `zcode-site deploy` again.";
- default:
- return "Run `zcode-site logs <deploymentId>` for details, then retry. The previous live version keeps serving.";
- }
- }
- // ---------- 其他命令 ----------
- function summarize(site) {
- return {
- id: site.id,
- name: site.name,
- url: site.url,
- status: site.status,
- ...(site.offlineReason && { offlineReason: site.offlineReason }),
- region: site.region,
- badgeHidden: site.badgeHidden,
- badgeVisible: site.badgeVisible,
- currentDeployment: site.currentDeployment && pickDeployment(site.currentDeployment),
- latestDeployment: site.latestDeployment && pickDeployment(site.latestDeployment),
- };
- }
- function pickDeployment(d) {
- return {
- id: d.id,
- status: d.status,
- createdAt: d.createdAt,
- finishedAt: d.finishedAt,
- failure: d.failure,
- };
- }
- async function logsCommand(ctx, deploymentId) {
- if (!deploymentId)
- throw localError(
- "MISSING_DEPLOYMENT",
- "Missing deployment id",
- "Usage: zcode-site logs <deploymentId> [--site <id>] [--offset <n>]",
- );
- const { site } = await resolveSite(ctx, { allowCreate: false });
- const offset = ctx.opts.offset === undefined ? 0 : Number(ctx.opts.offset);
- if (!Number.isInteger(offset) || offset < 0)
- throw localError(
- "INVALID_ARGS",
- "--offset must be a non-negative integer",
- "Pass the nextOffset from the previous call.",
- );
- return ctx.client.logs(site.id, deploymentId, offset);
- }
- async function setStatus(ctx, status) {
- const { site } = await resolveSite(ctx, { allowCreate: false });
- return summarize(await ctx.client.updateSite(site.id, { status }));
- }
- async function setBadge(ctx, action) {
- if (action !== "hide" && action !== "show")
- throw localError(
- "INVALID_ARGS",
- "Expected hide or show",
- "Usage: zcode-site badge hide|show [--site <id>]",
- );
- const { site } = await resolveSite(ctx, { allowCreate: false });
- return summarize(await ctx.client.updateSite(site.id, { badgeHidden: action === "hide" }));
- }
- async function rename(ctx, name) {
- if (!name?.trim())
- throw localError(
- "MISSING_NAME",
- "Missing site name",
- "Usage: zcode-site rename [--site <id>] <name>",
- );
- const { site } = await resolveSite(ctx, { allowCreate: false });
- return summarize(await ctx.client.updateSite(site.id, { name: name.trim() }));
- }
- const HELP = `zcode-site: deploy and manage ZCode Sites. Run it from the project root.
- Output
- stdout: JSON, one object per line. deploy prints progress events first; the last line is the result.
- stderr on failure: one line {"error":{"code","message","hint","data"?}}. Follow hint.
- Exit codes
- 0 success
- 1 bad arguments, a local file or stage problem, or SITE_NOT_SELECTED
- 2 not signed in to ZCode, sign-in expired, or not run inside a ZCode session
- 3 quota, rate or size limit, or another deployment in progress
- 4 the deployment failed, or a server error
- Local command (no sign-in, nothing is uploaded)
- check <stage-dir> [--port <n>]
- Run "bash start.sh" in the stage with PORT (default 13000) and a temporary DATA_DIR, wait
- up to 90 s for GET / below 500, then stop it.
- Result: {status: "live", httpStatus, log} - the site should answer 200.
- {status: "skipped", reason} - the stage can't run on this machine (Linux binaries on
- macOS/Windows, or no bash); run the app from its normal local environment instead.
- START_FAILED: data {reason, httpStatus?, log}.
- Site commands
- deploy <stage-dir> [--new | --site <id>] [--name <name>] [--prefix <prefix>]
- [--allow-foreign-binaries] [--quiet]
- Pack the stage directory (its root becomes /app) and publish it. Packing runs first and
- checks the package contract: root start.sh with LF line endings, size, file count, path
- depth, reserved /.__zcode/, broken symlinks, .env files, dependencies present, and every
- native binary linux x86-64. It skips .git, .DS_Store, __pycache__ and .zcode-site.json.
- PACKAGE_CHECK_FAILED (exit 1): data.problems lists everything to fix; nothing was created
- or uploaded. Fix all of them, then deploy again.
- --allow-foreign-binaries: accept non-linux binaries; only after confirming each listed file
- is never loaded at runtime (e.g. prebuilt binaries for other platforms next to
- the linux-x64 one).
- Target site: --site <id>, else the site in .zcode-site.json, else with --new a new site.
- A new site is linked by writing .zcode-site.json; keep it and don't put it in the stage.
- --new is ignored (with a warning) when the project is already linked.
- Without --new or --site on an unlinked project: SITE_NOT_SELECTED if the account has sites,
- data.candidates [{id, name, url, status}]; ask the user, never pick one yourself.
- --name display name of a new site, any language (default: directory name). A name that
- fails the server's content review gets 3611 and nothing is created.
- --prefix domain prefix of a new site: 4-30 chars, lowercase a-z 0-9 and -. The domain is
- {prefix}-{4 random chars}. The server lowercases it, turns other characters into
- -, cuts it to 30 and falls back to "site" below 4 or when the prefix fails content
- review; a warning event says so.
- Ignored for an existing site; the domain never changes.
- --quiet print only the result.
- Events: {event: "packed", packageBytes, unpackedBytes, files, linuxBinaries}, then
- {event: "site_created" | "warning" | "deployment_started" | "uploading" | "log" | "status", ...}
- Result: {siteId, deploymentId, status: "success", url}
- Failure (exit 4): code is the failed stage reason, data has {siteId, deploymentId, failure}.
- PACKAGE_INVALID server preflight rejected the package; message names the file.
- WARMUP_FAILED GET / got no answer below 500 within 90 s; read logs, run check.
- UPLOAD_TIMEOUT deploy again.
- PUBLISH_FAILED, PUBLISH_TIMEOUT, INTERNAL platform side; retry later.
- The previous live version keeps serving after a failed deployment.
- status [--site <id>]
- Result: {id, name, url, status, offlineReason?, region, badgeHidden, badgeVisible, currentDeployment,
- latestDeployment} (deployments: {id, status, createdAt, finishedAt, failure}).
- status: pending = never deployed successfully, the URL shows nothing yet; online = the URL
- serves currentDeployment; offline = taken offline. offlineReason review_rejected means
- content review rejected the live version and took the site offline: online gets 3612,
- change the content and deploy again. A redeploy keeps the site online
- on the old version. Deployment progress is latestDeployment.status: preparing,
- uploading, publishing or verifying means still running (it ends within about 10
- minutes) - wait and check again, don't redeploy.
- logs <deploymentId> [--site <id>] [--offset <n>]
- Result: {entries: [{stage, level, message, ts}], nextOffset}; pass nextOffset to read on.
- offline [--site <id>] take the site offline; domain and records are kept. Result: as status.
- online [--site <id>] bring it back; a site never deployed successfully stays pending, and one whose
- live version was rejected by content review gets 3612. Result: as status.
- rename [--site <id>] <name> change the display name only; 3611 if it fails content review.
- Result: as status.
- badge hide|show [--site <id>] hide or show the "ZCode" badge on the site. Only paid plans can
- hide it (usage.badgeRemovable); free plans get 3608. badgeHidden is the user's choice,
- badgeVisible whether the badge actually shows. Result: as status.
- list Result: {sites: [{id, name, url, status}]}
- usage Account traffic, requests and storage against the plan; canDeploy and exceededReasons.
- help this text.
- Commands without --site use the site in .zcode-site.json. Numeric error codes come from the Sites
- API (e.g. 3605 quota exceeded, 3610 another deployment in progress); their hint says what to do.`;
- // ---------- 入口 ----------
- export async function main(
- argv,
- {
- env = process.env,
- cwd = process.cwd(),
- fetchImpl,
- stdout = process.stdout,
- stderr = process.stderr,
- sleep = (ms) => new Promise((r) => setTimeout(r, ms)),
- now = Date.now,
- } = {},
- ) {
- let parsed;
- try {
- parsed = parseArgs({
- args: argv,
- allowPositionals: true,
- options: {
- site: { type: "string" },
- new: { type: "boolean" },
- name: { type: "string" },
- prefix: { type: "string" },
- offset: { type: "string" },
- port: { type: "string" },
- "allow-foreign-binaries": { type: "boolean" },
- quiet: { type: "boolean" },
- help: { type: "boolean", short: "h" },
- },
- });
- } catch (err) {
- return fail(stderr, localError("INVALID_ARGS", err.message, "Run `zcode-site help`."));
- }
- const [command, ...args] = parsed.positionals;
- if (!command || command === "help" || parsed.values.help) {
- stdout.write(`${HELP}\n`);
- return command || parsed.values.help ? 0 : 1;
- }
- if (command === "check") {
- try {
- const result = await check(cwd, args, parsed.values);
- stdout.write(`${JSON.stringify(result)}\n`);
- return 0;
- } catch (err) {
- return fail(stderr, err instanceof CliError ? err : internalError(err));
- }
- }
- const quiet = parsed.values.quiet;
- const out = { event: (e) => quiet || stdout.write(`${JSON.stringify(e)}\n`) };
- try {
- const endpoint = loadEndpoint(env);
- const client = createClient(endpoint, fetchImpl ?? createEndpointFetch(endpoint.address));
- const ctx = { client, cwd, opts: parsed.values, out, sleep, now };
- let result;
- switch (command) {
- case "deploy":
- result = await deploy(ctx, args[0]);
- break;
- case "status":
- result = summarize((await resolveSite(ctx, { allowCreate: false })).site);
- break;
- case "logs":
- result = await logsCommand(ctx, args[0]);
- break;
- case "offline":
- result = await setStatus(ctx, "offline");
- break;
- case "online":
- result = await setStatus(ctx, "online");
- break;
- case "usage":
- result = await client.usage();
- break;
- case "list":
- result = { sites: candidates(await allSites(client)) };
- break;
- case "rename":
- result = await rename(ctx, args.join(" "));
- break;
- case "badge":
- result = await setBadge(ctx, args[0]);
- break;
- default:
- throw localError(
- "UNKNOWN_COMMAND",
- `Unknown command: ${command}`,
- "Run `zcode-site help`.",
- );
- }
- stdout.write(`${JSON.stringify(result)}\n`);
- return 0;
- } catch (err) {
- return fail(stderr, err instanceof CliError ? err : internalError(err));
- }
- }
- function internalError(err) {
- return new CliError(
- "INTERNAL",
- err.message,
- "This is a bug in zcode-site; report it with the command you ran.",
- 4,
- );
- }
- async function check(cwd, [stageDir], opts) {
- if (!stageDir)
- throw localError(
- "INVALID_ARGS",
- "Missing stage directory",
- "Usage: zcode-site check <stage-dir> [--port <n>]",
- );
- const port = opts.port === undefined ? undefined : Number(opts.port);
- if (port !== undefined && (!Number.isInteger(port) || port < 1 || port > 65535))
- throw localError("INVALID_ARGS", "--port must be a port number", "Pass e.g. --port 13000.");
- const checked = await checkStage(stageDir, { cwd, ...(port ? { port } : {}) });
- if (checked.error)
- throw localError(
- checked.error,
- checked.message,
- "Pass the stage directory that holds start.sh.",
- );
- if (checked.failed)
- throw new CliError(
- "START_FAILED",
- checked.failed.reason,
- "Read data.log and the site-deploy skill's troubleshooting reference, fix the stage, then check again.",
- 1,
- checked.failed,
- );
- return checked.result;
- }
- function fail(stderr, err) {
- const error = { code: err.code, message: err.message, hint: err.hint };
- if (err.data !== undefined) error.data = err.data;
- stderr.write(`${JSON.stringify({ error })}\n`);
- return err.exitCode;
- }
- if (process.argv[1] && import.meta.url === pathToFileURL(realpathSync(process.argv[1])).href) {
- process.exitCode = await main(process.argv.slice(2));
- }
|