Skip to content

Commit 333be04

Browse files
authored
Add suffix option to FileSystem.makeTempFile (#5481)
1 parent f60dce6 commit 333be04

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

.changeset/empty-cougars-count.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@effect/platform": patch
3+
"@effect/platform-node-shared": patch
4+
---
5+
6+
Allow user to set extension of file created using `FileSystem.makeTempFile`

packages/platform-node-shared/src/internal/fileSystem.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ const makeTempFileFactory = (method: string) => {
382382
return (options?: FileSystem.MakeTempFileOptions) =>
383383
pipe(
384384
Effect.zip(makeDirectory(options), randomHexString(6)),
385-
Effect.map(([directory, random]) => Path.join(directory, random)),
385+
Effect.map(([directory, random]) => Path.join(directory, random + (options?.suffix ?? ""))),
386386
Effect.tap((path) => Effect.scoped(open(path, { flag: "w+" })))
387387
)
388388
}

packages/platform-node-shared/test/FileSystem.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,44 @@ describe("FileSystem", () => {
5353
assert(error._tag === "SystemError" && error.reason === "NotFound")
5454
})))
5555

56+
it("makeTempFile", () =>
57+
runPromise(Effect.gen(function*() {
58+
const fs = yield* (Fs.FileSystem)
59+
let file = ""
60+
yield* pipe(
61+
Effect.gen(function*() {
62+
file = yield* fs.makeTempFile({
63+
"suffix": ".txt"
64+
})
65+
expect(file.endsWith(".txt")).toBe(true)
66+
const stat = yield* fs.stat(file)
67+
expect(stat.type).toEqual("File")
68+
}),
69+
Effect.scoped
70+
)
71+
const stat = yield* fs.stat(file)
72+
expect(stat.type).toEqual("File")
73+
})))
74+
75+
it("makeTempFileScoped", () =>
76+
runPromise(Effect.gen(function*() {
77+
const fs = yield* (Fs.FileSystem)
78+
let file = ""
79+
yield* pipe(
80+
Effect.gen(function*() {
81+
file = yield* fs.makeTempFileScoped({
82+
"suffix": ".txt"
83+
})
84+
expect(file.endsWith(".txt")).toBe(true)
85+
const stat = yield* fs.stat(file)
86+
expect(stat.type).toEqual("File")
87+
}),
88+
Effect.scoped
89+
)
90+
const error = yield* Effect.flip(fs.stat(file))
91+
assert(error._tag === "SystemError" && error.reason === "NotFound")
92+
})))
93+
5694
it("truncate", () =>
5795
runPromise(Effect.gen(function*() {
5896
const fs = yield* Fs.FileSystem

packages/platform/src/FileSystem.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ export interface MakeTempDirectoryOptions {
369369
export interface MakeTempFileOptions {
370370
readonly directory?: string
371371
readonly prefix?: string
372+
readonly suffix?: string
372373
}
373374

374375
/**

0 commit comments

Comments
 (0)