Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

DenoPath.ts

103 lines2.5 KB

1/**
2 * Path service layers backed by the Deno Standard Library.
3 *
4 * **Example** (Using Deno path operations)
5 *
6 * ```ts import.meta.vitest
7 * import { Effect, Path } from "effect"
8 * import { DenoPath } from "@effect/platform-deno"
9 *
10 * const program = Effect.gen(function*() {
11 * const path = yield* Path.Path
12 * return path.extname("file.txt")
13 * }).pipe(Effect.provide(DenoPath.layer))
14 *
15 * Effect.runSync(program) // => ".txt"
16 * ```
17 *
18 * @stability unstable
19 * @since 4.0.0
20 */
21​
22import * as DenoPath from "@std/path"
23import * as DenoPathPosix from "@std/path/posix"
24import * as DenoPathWin from "@std/path/windows"
25import * as Effect from "effect/Effect"
26import * as Layer from "effect/Layer"
27import * as Path from "effect/Path"
28import * as PlatformError from "effect/PlatformError"
29​
30const fileUrlOps = (impl: {
31 readonly fromFileUrl: typeof DenoPath.fromFileUrl
32 readonly toFileUrl: typeof DenoPath.toFileUrl
33}) => ({
34 fromFileUrl: (url: URL): Effect.Effect<string, PlatformError.BadArgument> =>
35 Effect.try({
36 try: () => impl.fromFileUrl(url),
37 catch: (cause) =>
38 new PlatformError.BadArgument({
39 module: "Path",
40 method: "fromFileUrl",
41 cause
42 })
43 }),
44 toFileUrl: (path: string): Effect.Effect<URL, PlatformError.BadArgument> =>
45 Effect.try({
46 try: (): URL => impl.toFileUrl(path),
47 catch: (cause): PlatformError.BadArgument =>
48 new PlatformError.BadArgument({
49 module: "Path",
50 method: "toFileUrl",
51 cause
52 })
53 })
54})
55​
56/**
57 * A {@linkplain Layer.Layer | layer} that provides POSIX path operations.
58 *
59 * @stability unstable
60 * @category layers
61 * @since 4.0.0
62 */
63export const layerPosix: Layer.Layer<Path.Path> = Layer.succeed(Path.Path)(
64 Path.make({
65 ...DenoPathPosix,
66 sep: DenoPathPosix.SEPARATOR,
67 ...fileUrlOps(DenoPathPosix)
68 })
70​
71/**
72 * A {@linkplain Layer.Layer | layer} that provides Windows path operations.
73 *
74 * @stability unstable
75 * @category layers
76 * @since 4.0.0
77 */
78export const layerWin32: Layer.Layer<Path.Path> = Layer.succeed(
79 Path.Path
80)(
81 Path.make({
82 ...DenoPathWin,
83 sep: DenoPathWin.SEPARATOR,
84 ...fileUrlOps(DenoPathWin)
85 })
87​
88/**
89 * A {@linkplain Layer.Layer | layer} that provides OS-agnostic path operations.
90 *
91 * @stability unstable
92 * @category layers
93 * @since 4.0.0
94 */
95export const layer: Layer.Layer<Path.Path> = Layer.succeed(
96 Path.Path
97)(
98 Path.make({
99 ...DenoPath,
100 sep: DenoPath.SEPARATOR,
101 ...fileUrlOps(DenoPath)
102 })