Repository navigation
Expand file tree
/
Copy pathgenerate-functions-bootstrap.ts
More file actions
76 lines (70 loc) · 2.77 KB
/
Copy pathgenerate-functions-bootstrap.ts
File metadata and controls
76 lines (70 loc) · 2.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { NodeRuntime, NodeServices } from "@effect/platform-node";
import { build, stop } from "esbuild";
import { Data, Effect, FileSystem, Path } from "effect";
class FunctionsBootstrapBundleError extends Data.TaggedError("FunctionsBootstrapBundleError")<{
readonly message: string;
readonly cause?: unknown;
}> {}
const packageRoot = Effect.gen(function* () {
const path = yield* Path.Path;
return path.dirname(path.dirname(yield* path.fromFileUrl(new URL(import.meta.url))));
});
/** Location of the committed default Functions bootstrap module. */
export const generatedModulePath = Effect.gen(function* () {
const path = yield* Path.Path;
return path.join(yield* packageRoot, "src", "functions", "generated", "serve-main-bundle.ts");
});
const bundleServeMain = Effect.gen(function* () {
const path = yield* Path.Path;
const entrypoint = path.join(yield* packageRoot, "src", "functions", "serve.main.ts");
const result = yield* Effect.tryPromise({
try: () =>
build({
entryPoints: [entrypoint],
bundle: true,
format: "esm",
platform: "browser",
minify: true,
write: false,
legalComments: "none",
logLevel: "silent",
}),
catch: (cause) =>
new FunctionsBootstrapBundleError({ message: "Unable to bundle functions bootstrap", cause }),
});
const output = result.outputFiles[0]?.text;
if (output === undefined)
return yield* new FunctionsBootstrapBundleError({
message: "esbuild produced no functions bootstrap output",
});
return output;
}).pipe((program) =>
Effect.uninterruptibleMask((restore) =>
Effect.flatMap(Effect.exit(restore(program)), (result) =>
Effect.tryPromise({
try: () => stop(),
catch: (cause) =>
new FunctionsBootstrapBundleError({ message: "Unable to stop esbuild", cause }),
}).pipe(Effect.andThen(result)),
),
),
);
/** Renders the generated module that embeds the self-contained Edge Runtime main service. */
export const renderFunctionsBootstrapModule = Effect.map(
bundleServeMain,
(bundle) =>
"// Generated by packages/stack/scripts/generate-functions-bootstrap.ts; run `pnpm generate`.\n" +
"/** Bundled Edge Runtime main service used when a Functions creation has no bootstrap. */\n" +
`export const defaultFunctionsBootstrap = ${JSON.stringify(bundle)};\n`,
);
if (import.meta.main)
NodeRuntime.runMain(
Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const target = yield* generatedModulePath;
const source = yield* renderFunctionsBootstrapModule;
yield* fs.makeDirectory(path.dirname(target), { recursive: true });
yield* fs.writeFileString(target, source);
}).pipe(Effect.provide(NodeServices.layer)),
);