summaryrefslogtreecommitdiff
path: root/cli/js/ops/fs/read_dir.ts
blob: 29b8676ef29f2b20a58e45cd9e07d33633e1c3ab (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
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { sendSync, sendAsync } from "../dispatch_json.ts";
import { FileInfo, StatResponse, parseFileInfo } from "./stat.ts";

export interface DirEntry extends FileInfo {
  name: string;
}

interface ReadDirResponse {
  entries: StatResponse[];
}

function res(response: ReadDirResponse): DirEntry[] {
  return response.entries.map(
    (statRes: StatResponse): DirEntry => {
      return { ...parseFileInfo(statRes), name: statRes.name! };
    }
  );
}

export function readdirSync(path: string): Iterable<DirEntry> {
  return res(sendSync("op_read_dir", { path }))[Symbol.iterator]();
}

export function readdir(path: string): AsyncIterable<DirEntry> {
  const array = sendAsync("op_read_dir", { path }).then(res);
  return {
    async *[Symbol.asyncIterator](): AsyncIterableIterator<DirEntry> {
      yield* await array;
    },
  };
}