|
| 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