import { WorkflowEntrypoint } from "cloudflare:workers";
import type { WorkflowEvent, WorkflowStep } from "cloudflare:workers";
import { first, run } from "../db";
import { loadRepoById } from "../repos";
import { PAGES_LIMITS } from "../pages/config";
import { SCRATCH_LIMITS } from "./config";
import { DeployError, extractArtifact, planGitFiles, totalBytes, uploadGitBatch } from "../pages/collect";
import type { CollectedFiles, PlannedFile } from "../pages/collect";
import { activateSiteDeployment, failSiteDeployment, loadSiteById, loadSiteDeployment } from "./deploy";
export type SitesDeployParams = { deploymentId: string };
type Plan =
| { kind: "branch"; artifactsName: string; sha: string; folder: string; maxFiles: number; maxBytes: number }
| { kind: "artifact"; r2Key: string; folder: string }
| { kind: "skip"; error: string | null };
type Outcome<T> = { ok: true; value: T } | { ok: false; error: string };
async function attempt<T>(work: () => Promise<T>): Promise<Outcome<T>> {
try {
return { ok: true, value: await work() };
} catch (error) {
if (error instanceof DeployError) return { ok: false, error: error.message };
throw error;
}
}
async function start(deploymentId: string): Promise<Plan> {
const deployment = await loadSiteDeployment(deploymentId);
if (!deployment || deployment.status !== "queued") return { kind: "skip", error: null };
const site = await loadSiteById(deployment.site_id);
const repo = site?.repo_id ? await loadRepoById(site.repo_id) : null;
if (!site || !repo) return { kind: "skip", error: "The site is no longer linked to a repository" };
await run("UPDATE site_deployments SET status = 'building' WHERE id = ?", deploymentId);
if (!deployment.artifact_id) {
const scratch = site.scratch === 1;
return {
kind: "branch",
artifactsName: repo.artifacts_name,