summaryrefslogtreecommitdiff
path: root/cli/js/write_text_file.ts
blob: d2a6575d7f8d8b67d1070f58f8616f0d0b664ed9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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();
}