diff options
| author | Divya <hello@shortdiv.com> | 2020-04-28 00:35:20 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-28 01:35:20 -0400 |
| commit | de751e5221cd2eafbdd9cddfb58b0619d74c0504 (patch) | |
| tree | 10af8c5b70fc6b57f4a3247ec3367bf5847f195b /cli/js/write_text_file.ts | |
| parent | 2fc5878668d8f08ec6989d07e79125d640a73d43 (diff) | |
fix(#4769) Adds readTextFile, writeTextFile, with sync counterparts (#4901)
Diffstat (limited to 'cli/js/write_text_file.ts')
| -rw-r--r-- | cli/js/write_text_file.ts | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/cli/js/write_text_file.ts b/cli/js/write_text_file.ts new file mode 100644 index 000000000..d2a6575d7 --- /dev/null +++ b/cli/js/write_text_file.ts @@ -0,0 +1,18 @@ +import { open, openSync } from "./files.ts"; +import { writeAll, writeAllSync } from "./buffer.ts"; + +export function writeTextFileSync(path: string, data: string): void { + const file = openSync(path, { write: true, create: true, truncate: true }); + const enc = new TextEncoder(); + const contents = enc.encode(data); + writeAllSync(file, contents); + file.close(); +} + +export async function writeTextFile(path: string, data: string): Promise<void> { + const file = await open(path, { write: true, create: true, truncate: true }); + const enc = new TextEncoder(); + const contents = enc.encode(data); + await writeAll(file, contents); + file.close(); +} |
