MCPcopy Create free account
hub / github.com/Effect-TS/effect / path

Function path

packages/effect/src/unstable/cli/Primitive.ts:479–522  ·  view source on GitHub ↗
(
  pathType: PathType,
  mustExist?: boolean
)

Source from the content-addressed store, hash-verified

477 * @since 4.0.0
478 */
479export const path = (
480 pathType: PathType,
481 mustExist?: boolean
482): Primitive<string> => {
483 const primitive = makePrimitive(
484 "Path",
485 Effect.fnUntraced(function*(value) {
486 const fs = yield* FileSystem.FileSystem
487 const path = yield* Path.Path
488
489 // Resolve the path to absolute
490 const absolutePath = path.isAbsolute(value) ? value : path.resolve(value)
491
492 // Check if path exists
493 const exists = yield* Effect.mapError(
494 fs.exists(absolutePath),
495 (error) => `Failed to check path existence: ${error.message}`
496 )
497
498 // Validate existence requirements
499 if (mustExist === true && !exists) {
500 return yield* Effect.fail(`Path does not exist: ${absolutePath}`)
501 }
502
503 // Validate path type if it exists
504 if (exists && pathType !== "either") {
505 const stat = yield* Effect.mapError(
506 fs.stat(absolutePath),
507 (error) => `Failed to stat path: ${error.message}`
508 )
509
510 if (pathType === "file" && stat.type !== "File") {
511 return yield* Effect.fail(`Path is not a file: ${absolutePath}`)
512 }
513 if (pathType === "directory" && stat.type !== "Directory") {
514 return yield* Effect.fail(`Path is not a directory: ${absolutePath}`)
515 }
516 }
517
518 return absolutePath
519 })
520 )
521 return Object.assign(primitive, { pathType })
522}
523
524/**
525 * Creates a primitive that wraps string input in `Redacted`.

Callers

nothing calls this directly

Calls 4

assignMethod · 0.80
makePrimitiveFunction · 0.70
failMethod · 0.45
statMethod · 0.45

Tested by

no test coverage detected