import { createHash } from "node:crypto"; import { cp, mkdir, readFile, rm, writeFile } from "node:fs/promises"; import path from "node:path"; import { spawnSync } from "node:child_process"; const root = path.resolve(import.meta.dirname, "../.."); const source = path.join(root, "apps/admin/dist"); const output = path.join(root, "dist/release"); function run(command, args, environment = {}) { const result = spawnSync(command, args, { cwd: root, stdio: "inherit", env: { ...process.env, ...environment }, }); if (result.status !== 0) process.exit(result.status ?? 1); } const requiredFiles = ["index.html", "template-preview/index.html"]; const manifest = { createdAt: new Date().toISOString(), files: [] }; for (const file of requiredFiles) { await readFile(path.join(source, file)); } run("pnpm", ["--filter", "@personal-brand/api", "db:migrate:deploy"]); await rm(output, { recursive: true, force: true }); await mkdir(path.join(output, "api"), { recursive: true }); await cp(source, path.join(output, "admin"), { recursive: true }); await cp(path.join(root, "apps/api/dist"), path.join(output, "api"), { recursive: true }); for (const relativePath of [ "api/main.js", "admin/index.html", "admin/template-preview/index.html", ]) { const file = path.join(output, relativePath); const content = await readFile(file); manifest.files.push({ path: relativePath, bytes: content.byteLength, sha256: createHash("sha256").update(content).digest("hex"), }); } await writeFile(path.join(output, "manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`); console.log( `release package: ${path.relative(root, output)} (${manifest.files.length} checked files)`, );