()
| 59 | |
| 60 | async function generate(): Promise<string> { |
| 61 | const ast = await openapiTS(new URL(URL_FROM_ENV)); |
| 62 | |
| 63 | return BANNER + astToString(ast); |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Strip the banner before drift comparison: the banner embeds the source |
| 68 | * URL, which varies by environment (dev :7330, alt port for CI smoke, |
| 69 | * staging URL, etc). Two regenerations that hit different URLs but the |
| 70 | * same spec produce identical bodies but different banners: that is |
| 71 | * not real drift and must not fail the check. |
| 72 | */ |
| 73 | const stripBanner = (source: string): string => |
| 74 | source.replace(/^\/\*\*[\s\S]*?\*\/\s*/, "").trimStart(); |
| 75 | |
| 76 | async function main(): Promise<void> { |
| 77 | const isCheckMode = process.argv.includes("--check"); |
| 78 | |
| 79 | if (!JSON_MODE) { |
| 80 | console.log(`[generate:api] Fetching ${URL_FROM_ENV}`); |
| 81 | } |
| 82 | |
| 83 | let generated: string; |
| 84 | |
| 85 | try { |
| 86 | generated = await generate(); |
| 87 | } catch (error) { |
| 88 | if (!JSON_MODE) { |
| 89 | console.error(`[generate:api] FAILED: ${getErrorMessage(error)}`); |
| 90 | } |
| 91 | |
| 92 | report("blocked", "openapi_unavailable_or_invalid"); |
| 93 | console.error( |
| 94 | "[generate:api] Hint: start apps/api (bun run dev from apps/api/)" + |
| 95 | " or set OPENAPI_URL to a reachable spec." |
| 96 | ); |
| 97 | process.exit(2); |
| 98 | } |
| 99 | |
| 100 | if (isCheckMode) { |
| 101 | if (!existsSync(OUTPUT)) { |
| 102 | console.error( |
| 103 | `[generate:api:check] ${OUTPUT} does not exist — run \`bun run generate:api\`` |
| 104 | ); |
| 105 | report("failed", "schema_missing"); |
| 106 | process.exit(1); |
no test coverage detected