summaryrefslogtreecommitdiff
path: root/js/read_dir.ts
diff options
context:
space:
mode:
Diffstat (limited to 'js/read_dir.ts')
-rw-r--r--js/read_dir.ts49
1 files changed, 49 insertions, 0 deletions
diff --git a/js/read_dir.ts b/js/read_dir.ts
new file mode 100644
index 000000000..822177aa8
--- /dev/null
+++ b/js/read_dir.ts
@@ -0,0 +1,49 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import * as fbs from "gen/msg_generated";
+import { flatbuffers } from "flatbuffers";
+import * as dispatch from "./dispatch";
+import { FileInfo, FileInfoImpl } from "./fileinfo";
+import { assert } from "./util";
+
+/**
+ * Reads the directory given by path and returns
+ * a list of file info synchronously.
+ *
+ * import { readDirSync } from "deno";
+ * const files = readDirSync("/");
+ */
+export function readDirSync(path: string): FileInfo[] {
+ return res(dispatch.sendSync(...req(path)));
+}
+
+/**
+ * Reads the directory given by path and returns a list of file info.
+ *
+ * import { readDir } from "deno";
+ * const files = await readDir("/");
+ *
+ */
+export async function readDir(path: string): Promise<FileInfo[]> {
+ return res(await dispatch.sendAsync(...req(path)));
+}
+
+function req(path: string): [flatbuffers.Builder, fbs.Any, flatbuffers.Offset] {
+ const builder = new flatbuffers.Builder();
+ const path_ = builder.createString(path);
+ fbs.ReadDir.startReadDir(builder);
+ fbs.ReadDir.addPath(builder, path_);
+ const msg = fbs.ReadDir.endReadDir(builder);
+ return [builder, fbs.Any.ReadDir, msg];
+}
+
+function res(baseRes: null | fbs.Base): FileInfo[] {
+ assert(baseRes != null);
+ assert(fbs.Any.ReadDirRes === baseRes!.msgType());
+ const res = new fbs.ReadDirRes();
+ assert(baseRes!.msg(res) != null);
+ const fileInfos: FileInfo[] = [];
+ for (let i = 0; i < res.entriesLength(); i++) {
+ fileInfos.push(new FileInfoImpl(res.entries(i)!));
+ }
+ return fileInfos;
+}