Skip to content

Commit 1dee498

Browse files
authored
feat(fs/unstable): add writeTextFile and writeTextFileSync (denoland#6463)
1 parent 6811835 commit 1dee498

File tree

5 files changed

+435
-0
lines changed

5 files changed

+435
-0
lines changed

_tools/check_docs.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ const ENTRY_POINTS = [
8282
"../fs/unstable_types.ts",
8383
"../fs/unstable_umask.ts",
8484
"../fs/unstable_utime.ts",
85+
"../fs/unstable_write_file.ts",
86+
"../fs/unstable_write_text_file.ts",
8587
"../html/mod.ts",
8688
"../html/unstable_is_valid_custom_element_name.ts",
8789
"../http/mod.ts",

_tools/node_test_runner/run_test.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import "../../fs/unstable_stat_test.ts";
6363
import "../../fs/unstable_symlink_test.ts";
6464
import "../../fs/unstable_truncate_test.ts";
6565
import "../../fs/unstable_write_file_test.ts";
66+
import "../../fs/unstable_write_text_file_test.ts";
6667
import "../../fs/unstable_lstat_test.ts";
6768
import "../../fs/unstable_chmod_test.ts";
6869
import "../../fs/unstable_umask_test.ts";

fs/deno.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"./unstable-types": "./unstable_types.ts",
3434
"./unstable-umask": "./unstable_umask.ts",
3535
"./unstable-write-file": "./unstable_write_file.ts",
36+
"./unstable-write-text-file": "./unstable_write_text_file.ts",
3637
"./walk": "./walk.ts"
3738
}
3839
}

fs/unstable_write_text_file.ts

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright 2018-2025 the Deno authors. MIT license.
2+
3+
import { getNodeFs, isDeno } from "./_utils.ts";
4+
import { getWriteFsFlag } from "./_get_fs_flag.ts";
5+
import { mapError } from "./_map_error.ts";
6+
import type { WriteFileOptions } from "./unstable_types.ts";
7+
8+
/**
9+
* Write string `data` to the given `path`, by default creating a new file if
10+
* needed, else overwriting.
11+
*
12+
* Requires `allow-write` permission, and `allow-read` if `options.create` is
13+
* `false`.
14+
*
15+
* @example Usage
16+
* ```ts ignore
17+
* import { writeTextFile } from "@std/fs/unstable-write-text-file";
18+
* await writeTextFile("hello1.txt", "Hello world\n"); // overwrite "hello1.txt" or create it
19+
* ```
20+
*
21+
* @tags allow-read, allow-write
22+
*
23+
* @param path The path of the file that `data` is written to.
24+
* @param data A UTF-8 string or a stream of UTF-8 strings.
25+
* @param options Options for writing files. See {@linkcode WriteFileOptions}.
26+
*/
27+
export async function writeTextFile(
28+
path: string | URL,
29+
data: string | ReadableStream<string>,
30+
options?: WriteFileOptions,
31+
): Promise<void> {
32+
if (isDeno) {
33+
await Deno.writeTextFile(path, data, { ...options });
34+
} else {
35+
const {
36+
append = false,
37+
create = true,
38+
createNew = false,
39+
mode,
40+
signal,
41+
} = options ?? {};
42+
43+
const flag = getWriteFsFlag({ append, create, createNew });
44+
try {
45+
await getNodeFs().promises.writeFile(path, data, {
46+
encoding: "utf-8",
47+
flag,
48+
signal,
49+
});
50+
if (mode != null) {
51+
await getNodeFs().promises.chmod(path, mode);
52+
}
53+
} catch (error) {
54+
throw mapError(error);
55+
}
56+
}
57+
}
58+
59+
/**
60+
* Synchronously write string `data` to the given `path`, by default creating
61+
* a new file if needed, else overwriting.
62+
*
63+
* Requires `allow-write` permission, and `allow-read` if `options.create` is
64+
* `false`.
65+
*
66+
* @example Usage
67+
* ```ts ignore
68+
* import { writeTextFileSync } from "@std/fs/unstable-write-text-file";
69+
* writeTextFileSync("hello1.txt", "Hello world\n"); // overwrite "hello1.txt" or create it
70+
* ```
71+
*
72+
* @tags allow-read, allow-write
73+
*
74+
* @param path The path of the file that `data` is written to.
75+
* @param data A UTF-8 string.
76+
* @param options Options for writing files. See {@linkcode WriteFileOptions}.
77+
*/
78+
export function writeTextFileSync(
79+
path: string | URL,
80+
data: string,
81+
options?: WriteFileOptions,
82+
): void {
83+
if (isDeno) {
84+
Deno.writeTextFileSync(path, data, { ...options });
85+
} else {
86+
const {
87+
append = false,
88+
create = true,
89+
createNew = false,
90+
mode,
91+
} = options ?? {};
92+
93+
const flag = getWriteFsFlag({ append, create, createNew });
94+
try {
95+
getNodeFs().writeFileSync(path, data, { encoding: "utf-8", flag });
96+
if (mode != null) {
97+
getNodeFs().chmodSync(path, mode);
98+
}
99+
} catch (error) {
100+
throw mapError(error);
101+
}
102+
}
103+
}

0 commit comments

Comments
 (0)