config-runtime.ts
This file
Path
apps/cli/src/local/config-runtime.ts
131 lines · 3.9 KiB · TypeScript
Last changed by
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
lines 1–2why, one step back1// The compiled binary has no bun on the PATH, and a `bun` child can't read its embedded `/$bunfs/`,2// so `build.ts` embeds both modules bundled whole and the binary unpacks them and runs itself as bun.Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 3–26why, one step back3import { Brand } from "@repo/brand";4import { RulesFailed } from "@repo/rules/rules-evaluator";5import { Context, Effect, FileSystem, Option, Path, Schema } from "effect";6import type { Scope } from "effect";78export interface ConfigModules {9 readonly sdk: string;10 readonly entry: Option.Option<string>;11 readonly packages: Option.Option<string>;12}1314export interface ConfigBun {15 readonly bun: string;16 readonly env: Readonly<Record<string, string>>;17 readonly modules: Effect.Effect<18 ConfigModules,19 RulesFailed,20 FileSystem.FileSystem | Path.Path | Scope.Scope21 >;22}2324const loadFailed = (detail: string) =>25 new RulesFailed({ detail, reason: "load" });26Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 27why, one step back27/** Looked up only when a TypeScript folder is bundled: a build with no copy on disk still reads `policy.json`. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 28–80why, one step back28const ownCopyOf = (path: Path.Path, specifier: string) =>29 Effect.try({30 catch: () =>31 loadFailed(32 `this build of ${Brand.cli} carries no ${specifier} to bundle the config folder with`33 ),34 try: () => new URL(import.meta.resolve(specifier)),35 }).pipe(36 Effect.flatMap((url) =>37 path38 .fromFileUrl(url)39 .pipe(40 Effect.mapError(() =>41 loadFailed(42 `this build of ${Brand.cli} keeps its ${specifier} where bun build cannot read it`43 )44 )45 )46 )47 );4849const NODE_MODULES = "/node_modules";5051const packagesOf = (file: string) => {52 const at = file.lastIndexOf(`${NODE_MODULES}/`);5354 return at === -155 ? Option.none()56 : Option.some(file.slice(0, at + NODE_MODULES.length));57};5859export const ConfigRuntimeOwn: ConfigBun = {60 bun: "bun",61 env: {},62 modules: Effect.gen(function* () {63 const path = yield* Path.Path;64 const sdk = yield* ownCopyOf(path, "@repo/sdk");65 const effect = yield* ownCopyOf(path, "effect");6667 return { entry: Option.none(), packages: packagesOf(effect), sdk };68 }),69};7071export const EMBEDDED_KEY = "__configRuntime";7273const Embedded = Schema.Struct({74 [EMBEDDED_KEY]: Schema.Struct({75 entry: Schema.NonEmptyString,76 sdk: Schema.NonEmptyString,77 }),78 process: Schema.Struct({ execPath: Schema.NonEmptyString }),79});80Merge build/comments-cli-protocol-checks into mainoscabrielbefore adoptionc86204f
line 81why, one step back81/** `bun build` and a `bun` child read only real files, so the embedded modules are copied to a temp folder. */Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 82–131why, one step back82export const configRuntimeEmbedded = (embedded: {83 readonly execPath: string;84 readonly entry: string;85 readonly sdk: string;86}): ConfigBun => ({87 bun: embedded.execPath,88 env: { BUN_BE_BUN: "1" },89 modules: Effect.gen(function* () {90 const fs = yield* FileSystem.FileSystem;91 const path = yield* Path.Path;9293 const unpacked = Effect.gen(function* () {94 const dir = yield* fs.makeTempDirectoryScoped({95 prefix: "config-runtime-",96 });9798 const sdk = path.join(dir, "sdk.js");99 const entry = path.join(dir, "entry.js");100101 yield* fs.writeFile(sdk, yield* fs.readFile(embedded.sdk));102 yield* fs.writeFile(entry, yield* fs.readFile(embedded.entry));103104 return { entry: Option.some(entry), packages: Option.none(), sdk };105 });106107 return yield* unpacked.pipe(108 Effect.mapError((failure) =>109 loadFailed(110 `could not unpack this ${Brand.cli}'s sdk to bundle the config folder with: ${failure.message}`111 )112 )113 );114 }),115});116117export const ConfigRuntime = Context.Reference<ConfigBun>("cli/ConfigRuntime", {118 defaultValue: () => {119 // Unknown on purpose: the runtime is whatever started this process.120 const scope: unknown = globalThis;121122 return Option.match(Schema.decodeUnknownOption(Embedded)(scope), {123 onNone: () => ConfigRuntimeOwn,124 onSome: (found) =>125 configRuntimeEmbedded({126 ...found[EMBEDDED_KEY],127 execPath: found.process.execPath,128 }),129 });130 },131});