import fs from "node:fs/promises"; import path from "node:path"; import { spawn } from "node:child_process"; const root = process.cwd(); const nodeModulesPath = path.join(root, "node_modules"); const walletPath = path.join(root, "wallet.json"); async function pathExists(targetPath) { try { await fs.access(targetPath); return true; } catch { return false; } } function runCommand(command, args) { return new Promise((resolve, reject) => { const child = spawn(command, args, { cwd: root, stdio: ["ignore", "pipe", "pipe"] }); let stdout = ""; let stderr = ""; child.stdout.on("data", (chunk) => { const text = chunk.toString(); stdout += text; process.stdout.write(text); }); child.stderr.on("data", (chunk) => { const text = chunk.toString(); stderr += text; process.stderr.write(text); }); child.on("error", reject); child.on("close", (code) => { if (code !== 0) { reject(new Error(`${command} ${args.join(" ")} exited with code ${code}`)); return; } resolve({ stdout, stderr }); }); }); } function extractLink(output, label) { const regex = new RegExp(`${label}:\\s*(https?:\\/\\/\\S+)`); const match = output.match(regex); return match?.[1] || ""; } async function ensureDependencies() { if (await pathExists(nodeModulesPath)) { console.log("Dependencies present: skipping npm install."); return; } console.log("Dependencies missing: running npm install..."); await runCommand("npm", ["install"]); } async function ensureWallet() { if (await pathExists(walletPath)) { console.log("wallet.json present: skipping wallet:new."); return; } console.log("wallet.json missing: running npm run wallet:new..."); await runCommand("npm", ["run", "wallet:new"]); } async function main() { await ensureDependencies(); await ensureWallet(); console.log("Running deploy pipeline: npm run deploy:up"); const deployResult = await runCommand("npm", ["run", "deploy:up"]); const appUrl = extractLink(deployResult.stdout, "App URL"); const codeArchiveUrl = extractLink(deployResult.stdout, "Code Archive URL"); if (!appUrl || !codeArchiveUrl) { throw new Error( "Deploy step did not emit required links (App URL and Code Archive URL)." ); } console.log(""); console.log("Ship completed."); console.log(`App URL: ${appUrl}`); console.log(`Code Archive URL: ${codeArchiveUrl}`); } main().catch((error) => { console.error(error?.message || String(error)); process.exit(1); });