2020-07-06 21:45:39 -04:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
2020-04-28 01:35:20 -04:00
|
|
|
import { open, openSync } from "./files.ts";
|
|
|
|
import { readAll, readAllSync } from "./buffer.ts";
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
export function readTextFileSync(path: string | URL): string {
|
2020-04-28 01:35:20 -04:00
|
|
|
const file = openSync(path);
|
2020-07-06 21:45:39 -04:00
|
|
|
const contents = readAllSync(file);
|
2020-04-28 01:35:20 -04:00
|
|
|
file.close();
|
2020-07-06 21:45:39 -04:00
|
|
|
const decoder = new TextDecoder();
|
|
|
|
return decoder.decode(contents);
|
2020-04-28 01:35:20 -04:00
|
|
|
}
|
|
|
|
|
2020-06-11 12:36:20 -04:00
|
|
|
export async function readTextFile(path: string | URL): Promise<string> {
|
2020-04-28 01:35:20 -04:00
|
|
|
const file = await open(path);
|
2020-07-06 21:45:39 -04:00
|
|
|
const contents = await readAll(file);
|
2020-04-28 01:35:20 -04:00
|
|
|
file.close();
|
2020-07-06 21:45:39 -04:00
|
|
|
const decoder = new TextDecoder();
|
|
|
|
return decoder.decode(contents);
|
2020-04-28 01:35:20 -04:00
|
|
|
}
|