summaryrefslogtreecommitdiff
path: root/std/fs/read_file_str.ts
blob: 9f87c933892d225fefec4e1a7d1cd18b18e909b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.

export interface ReadOptions {
  encoding?: string;
}

/**
 * Read file synchronously and output it as a string.
 *
 * @param filename File to read
 * @param opts Read options
 */
export function readFileStrSync(
  filename: string,
  opts: ReadOptions = {}
): string {
  const decoder = new TextDecoder(opts.encoding);
  return decoder.decode(Deno.readFileSync(filename));
}

/**
 * Read file and output it as a string.
 *
 * @param filename File to read
 * @param opts Read options
 */
export async function readFileStr(
  filename: string,
  opts: ReadOptions = {}
): Promise<string> {
  const decoder = new TextDecoder(opts.encoding);
  return decoder.decode(await Deno.readFile(filename));
}