|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +const fs = require("node:fs/promises"); |
| 4 | +const path = require("node:path"); |
| 5 | +const { spawn } = require("node:child_process"); |
| 6 | + |
| 7 | +const mode = process.argv[2]; |
| 8 | + |
| 9 | +const commands = { |
| 10 | + export: |
| 11 | + 'tinacms build --local --skip-cloud-checks --skip-search-index -c "next build"', |
| 12 | + cloudflare: |
| 13 | + 'tinacms build --local --skip-cloud-checks --skip-search-index -c "next build && npx pagefind --site out --output-subdir pagefind"', |
| 14 | +}; |
| 15 | + |
| 16 | +const command = commands[mode]; |
| 17 | + |
| 18 | +if (!command) { |
| 19 | + console.error( |
| 20 | + `Unknown static export mode "${mode}". Use one of: ${Object.keys(commands).join(", ")}.` |
| 21 | + ); |
| 22 | + process.exit(1); |
| 23 | +} |
| 24 | + |
| 25 | +const projectRoot = process.cwd(); |
| 26 | +const appApiDir = path.join(projectRoot, "src", "app", "api"); |
| 27 | +const backupRoot = path.join(projectRoot, ".static-export-route-backup"); |
| 28 | + |
| 29 | +async function pathExists(target) { |
| 30 | + try { |
| 31 | + await fs.access(target); |
| 32 | + return true; |
| 33 | + } catch { |
| 34 | + return false; |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +async function collectRouteFiles(dir) { |
| 39 | + const files = []; |
| 40 | + const entries = await fs.readdir(dir, { withFileTypes: true }); |
| 41 | + |
| 42 | + for (const entry of entries) { |
| 43 | + const fullPath = path.join(dir, entry.name); |
| 44 | + if (entry.isDirectory()) { |
| 45 | + files.push(...(await collectRouteFiles(fullPath))); |
| 46 | + continue; |
| 47 | + } |
| 48 | + |
| 49 | + if (entry.isFile() && entry.name === "route.ts") { |
| 50 | + files.push(fullPath); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return files; |
| 55 | +} |
| 56 | + |
| 57 | +async function moveRouteFilesOut() { |
| 58 | + if (!(await pathExists(appApiDir))) return []; |
| 59 | + |
| 60 | + const routeFiles = await collectRouteFiles(appApiDir); |
| 61 | + |
| 62 | + await fs.rm(backupRoot, { recursive: true, force: true }); |
| 63 | + |
| 64 | + for (const routeFile of routeFiles) { |
| 65 | + const relativePath = path.relative(projectRoot, routeFile); |
| 66 | + const backupPath = path.join(backupRoot, relativePath); |
| 67 | + await fs.mkdir(path.dirname(backupPath), { recursive: true }); |
| 68 | + await fs.rename(routeFile, backupPath); |
| 69 | + } |
| 70 | + |
| 71 | + return routeFiles.map((routeFile) => path.relative(projectRoot, routeFile)); |
| 72 | +} |
| 73 | + |
| 74 | +async function restoreRouteFiles(relativePaths) { |
| 75 | + for (const relativePath of relativePaths) { |
| 76 | + const backupPath = path.join(backupRoot, relativePath); |
| 77 | + const targetPath = path.join(projectRoot, relativePath); |
| 78 | + |
| 79 | + if (!(await pathExists(backupPath))) continue; |
| 80 | + |
| 81 | + await fs.mkdir(path.dirname(targetPath), { recursive: true }); |
| 82 | + await fs.rename(backupPath, targetPath); |
| 83 | + } |
| 84 | + |
| 85 | + await fs.rm(backupRoot, { recursive: true, force: true }); |
| 86 | +} |
| 87 | + |
| 88 | +async function run() { |
| 89 | + const movedRouteFiles = await moveRouteFilesOut(); |
| 90 | + let exitCode = 1; |
| 91 | + |
| 92 | + try { |
| 93 | + const child = spawn(command, { |
| 94 | + cwd: projectRoot, |
| 95 | + env: { |
| 96 | + ...process.env, |
| 97 | + NODE_ENV: "production", |
| 98 | + EXPORT_MODE: "static", |
| 99 | + UNOPTIMIZED_IMAGES: "true", |
| 100 | + NEXT_PUBLIC_STATIC_EXPORT: "true", |
| 101 | + ...(mode === "cloudflare" |
| 102 | + ? { NEXT_PUBLIC_PAGEFIND_PATH: "/pagefind" } |
| 103 | + : {}), |
| 104 | + }, |
| 105 | + shell: true, |
| 106 | + stdio: "inherit", |
| 107 | + }); |
| 108 | + |
| 109 | + exitCode = await new Promise((resolve, reject) => { |
| 110 | + child.on("error", reject); |
| 111 | + child.on("exit", resolve); |
| 112 | + }); |
| 113 | + } finally { |
| 114 | + await restoreRouteFiles(movedRouteFiles); |
| 115 | + } |
| 116 | + |
| 117 | + process.exit(exitCode ?? 1); |
| 118 | +} |
| 119 | + |
| 120 | +run().catch((error) => { |
| 121 | + console.error(error); |
| 122 | + process.exit(1); |
| 123 | +}); |
0 commit comments