|
1 | | -import { Context, Data, Effect, Layer, Array as Arr, Option, pipe } from 'effect'; |
| 1 | +import { Context, Data, Effect, Layer, Array as Arr, Option, pipe, Schema } from 'effect'; |
2 | 2 | import { FileSystem, Path } from '@effect/platform'; |
3 | 3 | import fg from 'fast-glob'; |
4 | 4 | import YAML from 'yaml'; |
5 | 5 | import type { PackageInfo, WorkspaceType } from './schemas.js'; |
6 | 6 | import { WorkspaceNotFoundError } from './errors.js'; |
7 | 7 |
|
| 8 | +const JsonRecord = Schema.parseJson(Schema.Record({ key: Schema.String, value: Schema.Unknown })); |
| 9 | + |
8 | 10 | // ─── Result type ────────────────────────────────────────────────────────────── |
9 | 11 |
|
10 | 12 | export interface WorkspaceResult { |
@@ -74,16 +76,12 @@ const readPackageInfo = ( |
74 | 76 | if (!exists) return Effect.succeed(null); |
75 | 77 | return pipe( |
76 | 78 | fs.readFileString(pkgPath, 'utf-8'), |
77 | | - Effect.map((contents): PackageInfo | null => { |
78 | | - try { |
79 | | - const parsed = JSON.parse(contents) as Record<string, unknown>; |
80 | | - const name = |
81 | | - typeof parsed['name'] === 'string' ? parsed['name'] : pathSvc.basename(pkgDir); |
82 | | - const entryPoints = extractEntryPoints(parsed); |
83 | | - return { name, root: pkgDir, entryPoints: [...entryPoints] } as PackageInfo; |
84 | | - } catch { |
85 | | - return null; |
86 | | - } |
| 79 | + Effect.flatMap((contents) => Schema.decodeUnknown(JsonRecord)(contents)), |
| 80 | + Effect.map((parsed): PackageInfo | null => { |
| 81 | + const name = |
| 82 | + typeof parsed['name'] === 'string' ? parsed['name'] : pathSvc.basename(pkgDir); |
| 83 | + const entryPoints = extractEntryPoints(parsed); |
| 84 | + return { name, root: pkgDir, entryPoints: [...entryPoints] } as PackageInfo; |
87 | 85 | }), |
88 | 86 | Effect.catchAll(() => Effect.succeed(null)), |
89 | 87 | ); |
@@ -306,10 +304,10 @@ export const WorkspaceDetectorLive = Layer.effect( |
306 | 304 | fs.readFileString(pathSvc.join(cwd, 'package.json'), 'utf-8'), |
307 | 305 | Effect.orDie, |
308 | 306 | Effect.flatMap((raw) => |
309 | | - Effect.try({ |
310 | | - try: () => JSON.parse(raw) as Record<string, unknown>, |
311 | | - catch: () => new WorkspaceNotFoundError({ cwd }), |
312 | | - }), |
| 307 | + pipe( |
| 308 | + Schema.decodeUnknown(JsonRecord)(raw), |
| 309 | + Effect.mapError(() => new WorkspaceNotFoundError({ cwd })), |
| 310 | + ), |
313 | 311 | ), |
314 | 312 | Effect.flatMap((rootPkg) => |
315 | 313 | pipe( |
|
0 commit comments