( path: string | URL, data: string | ReadableStream<string>, options?: WriteFileOptions, )
| 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 |
no test coverage detected