diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2020-07-08 15:38:22 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-08 09:38:22 -0400 |
commit | 231899695d410d8d8c14c0936682a90e98bc6fd3 (patch) | |
tree | 4703344ac42cfdee4344e55e71824fb1743b4ff1 /cli/js | |
parent | 82aabb657a8fbaf107e58214490fdd129db3ae6b (diff) |
feat(cli): Add WriteFileOptions to writeTextFile & writeTextFileSync (#6280)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/lib.deno.ns.d.ts | 9 | ||||
-rw-r--r-- | cli/js/write_text_file.ts | 25 |
2 files changed, 18 insertions, 16 deletions
diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 2137ce336..4a3417b5d 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -1524,7 +1524,11 @@ declare namespace Deno { * * Requires `allow-write` permission, and `allow-read` if `options.create` is `false`. */ - export function writeTextFileSync(path: string | URL, data: string): void; + export function writeTextFileSync( + path: string | URL, + data: string, + options?: WriteFileOptions + ): void; /** Asynchronously write string `data` to the given `path`, by default creating a new file if needed, * else overwriting. @@ -1537,7 +1541,8 @@ declare namespace Deno { */ export function writeTextFile( path: string | URL, - data: string + data: string, + options?: WriteFileOptions ): Promise<void>; /** Synchronously truncates or extends the specified file, to reach the diff --git a/cli/js/write_text_file.ts b/cli/js/write_text_file.ts index 615813460..ca7646c75 100644 --- a/cli/js/write_text_file.ts +++ b/cli/js/write_text_file.ts @@ -1,23 +1,20 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. +import { writeFileSync, writeFile, WriteFileOptions } from "./write_file.ts"; -import { open, openSync } from "./files.ts"; -import { writeAll, writeAllSync } from "./buffer.ts"; - -export function writeTextFileSync(path: string | URL, data: string): void { - const file = openSync(path, { write: true, create: true, truncate: true }); +export function writeTextFileSync( + path: string | URL, + data: string, + options: WriteFileOptions = {} +): void { const encoder = new TextEncoder(); - const contents = encoder.encode(data); - writeAllSync(file, contents); - file.close(); + return writeFileSync(path, encoder.encode(data), options); } -export async function writeTextFile( +export function writeTextFile( path: string | URL, - data: string + data: string, + options: WriteFileOptions = {} ): Promise<void> { - const file = await open(path, { write: true, create: true, truncate: true }); const encoder = new TextEncoder(); - const contents = encoder.encode(data); - await writeAll(file, contents); - file.close(); + return writeFile(path, encoder.encode(data), options); } |