Files
docs/scripts/cf-pages-deploy.ts
zooqueenandHanzo Dev 685c94fd09
Hanzo CI/CD / test (linux-amd64) (push) Skipped
Hanzo CI/CD / test (linux-arm64) (push) Skipped
Hanzo CI/CD / test-advisory (push) Skipped
Hanzo CI/CD / test-amd64 (push) Successful in 8m7s
Hanzo CI/CD / cicd (push) Skipped
Hanzo CI/CD / verify (push) Skipped
CI/CD / cicd (push) Successful in 8m9s
refactor(ts): standardize scripts, helpers, and Next configs to .ts
Co-authored-by: Hanzo Dev <[email protected]>
2026-11-10 18:49:10 -08:00

202 lines
6.0 KiB
JavaScript

#!/usr/bin/env node
/**
* Custom CF Pages deployment script that handles large file counts
* by batching uploads in smaller chunks to avoid EPIPE errors.
*/
import { createHash } from 'crypto';
import { readdir, readFile } from 'fs/promises';
import { join, relative } from 'path';
interface FileHashInfo {
path: string;
hash: string;
content: Buffer;
size: number;
}
interface FileDeployInfo extends FileHashInfo {
relativePath: string;
}
interface CfDeployment {
id: string;
url: string;
jwt?: string;
required_hashes?: string[];
}
interface CfApiResponse<T> {
success: boolean;
errors?: unknown[];
result: T;
}
const ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID || '94a3e3f299092abb1feda2a7481ea845';
const PROJECT_NAME = process.env.CF_PAGES_PROJECT || 'hanzo-docs';
// Never hard-code the token. CI injects CLOUDFLARE_API_TOKEN from KMS
// (kms.hanzo.ai → the docs deploy service account). Fail fast if absent.
const API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;
if (!API_TOKEN) {
console.error(
'CLOUDFLARE_API_TOKEN is required. It is provisioned from KMS (kms.hanzo.ai) in CI; ' +
'for a local deploy export it from your KMS session. Refusing to run without it.',
);
process.exit(1);
}
const BASE_URL = `https://api.cloudflare.com/client/v4/accounts/${ACCOUNT_ID}/pages/projects/${PROJECT_NAME}`;
const BATCH_SIZE = 50; // files per upload batch
const MAX_RETRIES = 3;
async function getAllFiles(dir: string): Promise<string[]> {
const files: string[] = [];
const entries = await readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
files.push(...await getAllFiles(fullPath));
} else {
files.push(fullPath);
}
}
return files;
}
async function hashFile(filePath: string): Promise<FileHashInfo> {
const content = await readFile(filePath);
const hash = createHash('sha256').update(content).digest('hex');
return { path: filePath, hash, content, size: content.length };
}
async function apiCall<T>(url: string, options: RequestInit = {}): Promise<CfApiResponse<T>> {
const resp = await fetch(url, {
...options,
headers: {
'Authorization': `Bearer ${API_TOKEN}`,
...options.headers,
},
});
const data = (await resp.json()) as CfApiResponse<T>;
if (!data.success) {
throw new Error(`CF API error: ${JSON.stringify(data.errors)}`);
}
return data;
}
async function createDeployment(hashes: FileDeployInfo[]): Promise<CfDeployment> {
// Step 1: Create deployment with manifest
const manifest: Record<string, string> = {};
for (const { relativePath, hash } of hashes) {
manifest[`/${relativePath}`] = hash;
}
const formData = new FormData();
formData.append('manifest', JSON.stringify(manifest));
formData.append('branch', process.env.CF_PAGES_BRANCH || 'main');
console.log(`Creating deployment with ${Object.keys(manifest).length} files...`);
const data = await apiCall<CfDeployment>(`${BASE_URL}/deployments`, {
method: 'POST',
body: formData,
});
return data.result;
}
async function uploadBatch(files: FileDeployInfo[], jwt: string): Promise<unknown> {
const formData = new FormData();
for (const file of files) {
formData.append(file.hash, new Blob([new Uint8Array(file.content)]), file.relativePath);
}
const resp = await fetch('https://api.cloudflare.com/client/v4/pages/assets/upload', {
method: 'POST',
headers: {
'Authorization': `Bearer ${jwt}`,
},
body: formData,
});
if (!resp.ok) {
const text = await resp.text();
throw new Error(`Upload failed (${resp.status}): ${text}`);
}
return resp.json();
}
async function main() {
const outDir = process.argv[2] || 'apps/docs/out';
console.log(`Scanning ${outDir}...`);
const allFiles = await getAllFiles(outDir);
console.log(`Found ${allFiles.length} files`);
// Hash all files
console.log('Hashing files...');
const fileInfos = await Promise.all(
allFiles.map(async (filePath) => {
const info = await hashFile(filePath);
return {
...info,
relativePath: relative(outDir, filePath),
};
})
);
// Create deployment (gets JWT token and list of missing files)
const deployment = await createDeployment(fileInfos);
console.log(`Deployment ID: ${deployment.id}`);
console.log(`URL: ${deployment.url}`);
const jwt = deployment.jwt;
if (!jwt) {
console.log('No JWT returned - all files already uploaded!');
return;
}
// Find which files need uploading
const missingHashes = new Set(deployment.required_hashes || []);
const filesToUpload = missingHashes.size > 0
? fileInfos.filter(f => missingHashes.has(f.hash))
: fileInfos; // upload all if no required_hashes returned
console.log(`Need to upload ${filesToUpload.length} files (${fileInfos.length - filesToUpload.length} already cached)`);
// Upload in batches
const batches: FileDeployInfo[][] = [];
for (let i = 0; i < filesToUpload.length; i += BATCH_SIZE) {
batches.push(filesToUpload.slice(i, i + BATCH_SIZE));
}
let uploaded = 0;
for (let i = 0; i < batches.length; i++) {
const batch = batches[i];
let retries = 0;
while (retries < MAX_RETRIES) {
try {
await uploadBatch(batch, jwt);
uploaded += batch.length;
process.stdout.write(`\rUploaded ${uploaded}/${filesToUpload.length} (batch ${i + 1}/${batches.length})`);
break;
} catch (err) {
retries++;
const message = err instanceof Error ? err.message : String(err);
if (retries >= MAX_RETRIES) {
console.error(`\nFailed batch ${i + 1} after ${MAX_RETRIES} retries: ${message}`);
throw err;
}
console.warn(`\nRetrying batch ${i + 1} (attempt ${retries + 1})...`);
await new Promise(r => setTimeout(r, 1000 * retries));
}
}
}
console.log(`\nDeployment complete: ${deployment.url}`);
}
main().catch((err: unknown) => {
const message = err instanceof Error ? err.message : String(err);
console.error('Deploy failed:', message);
process.exit(1);
});