Skip to content

Commit 0ad8a04

Browse files
committed
fix: avoid direct node imports in shell resource probe
1 parent 6bea8da commit 0ad8a04

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

packages/lib/src/shell/files.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { availableParallelism, totalmem } from "node:os"
2-
31
import type { PlatformError } from "@effect/platform/Error"
42
import type * as FileSystem from "@effect/platform/FileSystem"
53
import type * as Path from "@effect/platform/Path"
@@ -14,6 +12,28 @@ import { resolveBaseDir } from "./paths.js"
1412
const 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+
1737
const isFileSpec = (spec: FileSpec): spec is Extract<FileSpec, { readonly _tag: "File" }> => spec._tag === "File"
1838

1939
const 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

Comments
 (0)