0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-02-08 15:21:26 -05:00
denoland-deno/cli/js/write_text_file.ts

24 lines
762 B
TypeScript
Raw Normal View History

2020-07-07 04:45:39 +03:00
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
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 });
2020-07-07 04:45:39 +03:00
const encoder = new TextEncoder();
const contents = encoder.encode(data);
writeAllSync(file, contents);
file.close();
}
export async function writeTextFile(
path: string | URL,
data: string
): Promise<void> {
const file = await open(path, { write: true, create: true, truncate: true });
2020-07-07 04:45:39 +03:00
const encoder = new TextEncoder();
const contents = encoder.encode(data);
await writeAll(file, contents);
file.close();
}