| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as FileSystem from "effect/FileSystem"; |
| 3 | import * as Path from "effect/Path"; |
| 4 | import * as ChildProcess from "effect/process/ChildProcess"; |
| 5 | import { sha256 } from "../../Util/sha256.ts"; |
| 6 | import type { WebsiteArtifactProps } from "./Artifact.ts"; |
| 7 | |
| 8 | /** Release staging, compression and validation buffers between deployments. */ |
| 9 | export const packageWebsiteInChild = Effect.fn( |
| 10 | function* (props: WebsiteArtifactProps) { |
| 11 | const fs = yield* FileSystem.FileSystem; |
| 12 | const path = yield* Path.Path; |
| 13 | const directory = yield* fs.makeTempDirectoryScoped({ |
| 14 | prefix: "alchemy-neon-package-", |
| 15 | }); |
| 16 | const inputPath = path.join(directory, "input.json"); |
| 17 | const outputPath = path.join(directory, "output.zip"); |
| 18 | yield* fs.writeFileString(inputPath, JSON.stringify(props)); |
| 19 | const runner = yield* path.fromFileUrl( |
| 20 | new URL( |
| 21 | import.meta.url.endsWith(".ts") |
| 22 | ? "./PackageRunner.ts" |
| 23 | : "./PackageRunner.js", |
| 24 | import.meta.url, |
| 25 | ), |
| 26 | ); |
| 27 | const child = yield* ChildProcess.make( |
| 28 | "node", |
| 29 | [ |
| 30 | "--max-old-space-size=1536", |
| 31 | ...(runner.endsWith(".ts") |
| 32 | ? [ |
| 33 | "--experimental-transform-types", |
| 34 | "--no-warnings=ExperimentalWarning", |
| 35 | ] |
| 36 | : []), |
| 37 | runner, |
| 38 | inputPath, |
| 39 | outputPath, |
| 40 | ], |
| 41 | { stdin: "ignore", stdout: "ignore", stderr: "inherit" }, |
| 42 | ); |
| 43 | const code = yield* child.exitCode; |
| 44 | if (code !== 0) |
| 45 | return yield* Effect.fail(new Error(`Website packaging exited ${code}`)); |
| 46 | const archive = yield* fs.readFile(outputPath); |
| 47 | return { archive, hash: yield* sha256(archive) }; |
| 48 | }, |
| 49 | Effect.scoped, |
| 50 | Effect.timeout("90 seconds"), |
| 51 | ); |