From de751e5221cd2eafbdd9cddfb58b0619d74c0504 Mon Sep 17 00:00:00 2001 From: Divya Date: Tue, 28 Apr 2020 00:35:20 -0500 Subject: fix(#4769) Adds readTextFile, writeTextFile, with sync counterparts (#4901) --- cli/js/write_text_file.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 cli/js/write_text_file.ts (limited to 'cli/js/write_text_file.ts') 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 { + 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(); +} -- cgit v1.2.3