1- import { availableParallelism , totalmem } from "node:os"
2-
31import type { PlatformError } from "@effect/platform/Error"
42import type * as FileSystem from "@effect/platform/FileSystem"
53import type * as Path from "@effect/platform/Path"
@@ -14,6 +12,28 @@ import { resolveBaseDir } from "./paths.js"
1412const ensureParentDir = ( path : Path . Path , fs : FileSystem . FileSystem , filePath : string ) =>
1513 fs . makeDirectory ( path . dirname ( filePath ) , { recursive : true } )
1614
15+ const fallbackHostResources = {
16+ cpuCount : 1 ,
17+ totalMemoryBytes : 1024 ** 3
18+ }
19+
20+ const loadHostResources = ( ) : Effect . Effect <
21+ { readonly cpuCount : number ; readonly totalMemoryBytes : number }
22+ > =>
23+ Effect . tryPromise ( {
24+ try : ( ) =>
25+ import ( "node:os" ) . then ( ( os ) => ( {
26+ cpuCount : os . availableParallelism ( ) ,
27+ totalMemoryBytes : os . totalmem ( )
28+ } ) ) ,
29+ catch : ( error ) => new Error ( String ( error ) )
30+ } ) . pipe (
31+ Effect . match ( {
32+ onFailure : ( ) => fallbackHostResources ,
33+ onSuccess : ( value ) => value
34+ } )
35+ )
36+
1737const isFileSpec = ( spec : FileSpec ) : spec is Extract < FileSpec , { readonly _tag : "File" } > => spec . _tag === "File"
1838
1939const resolveSpecPath = (
@@ -108,10 +128,8 @@ export const writeProjectFiles = (
108128 yield * _ ( fs . makeDirectory ( baseDir , { recursive : true } ) )
109129
110130 const normalizedConfig = withDefaultResourceLimitIntent ( config )
111- const composeResourceLimits = resolveComposeResourceLimits ( normalizedConfig , {
112- cpuCount : availableParallelism ( ) ,
113- totalMemoryBytes : totalmem ( )
114- } )
131+ const hostResources = yield * _ ( loadHostResources ( ) )
132+ const composeResourceLimits = resolveComposeResourceLimits ( normalizedConfig , hostResources )
115133 const specs = planFiles ( normalizedConfig , composeResourceLimits )
116134 const created : Array < string > = [ ]
117135 const existingFilePaths = force ? [ ] : yield * _ ( collectExistingFilePaths ( fs , path , baseDir , specs ) )
0 commit comments