2019-01-21 14:03:30 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-08-21 20:42:48 -04:00
|
|
|
import { sendSync, sendAsync, msg, flatbuffers } from "./dispatch_flatbuffers";
|
2018-10-03 21:41:59 -04:00
|
|
|
import { FileInfo, FileInfoImpl } from "./file_info";
|
2018-10-03 17:56:56 -04:00
|
|
|
import { assert } from "./util";
|
|
|
|
|
2018-10-03 21:18:23 -04:00
|
|
|
function req(path: string): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
|
2018-10-17 13:04:28 -04:00
|
|
|
const builder = flatbuffers.createBuilder();
|
2018-10-03 17:56:56 -04:00
|
|
|
const path_ = builder.createString(path);
|
2019-04-07 20:51:43 -04:00
|
|
|
const inner = msg.ReadDir.createReadDir(builder, path_);
|
2018-10-03 21:18:23 -04:00
|
|
|
return [builder, msg.Any.ReadDir, inner];
|
2018-10-03 17:56:56 -04:00
|
|
|
}
|
|
|
|
|
2018-10-03 21:18:23 -04:00
|
|
|
function res(baseRes: null | msg.Base): FileInfo[] {
|
2018-10-03 17:56:56 -04:00
|
|
|
assert(baseRes != null);
|
2018-10-03 21:18:23 -04:00
|
|
|
assert(msg.Any.ReadDirRes === baseRes!.innerType());
|
|
|
|
const res = new msg.ReadDirRes();
|
2018-10-03 21:12:23 -04:00
|
|
|
assert(baseRes!.inner(res) != null);
|
2018-10-03 17:56:56 -04:00
|
|
|
const fileInfos: FileInfo[] = [];
|
|
|
|
for (let i = 0; i < res.entriesLength(); i++) {
|
|
|
|
fileInfos.push(new FileInfoImpl(res.entries(i)!));
|
|
|
|
}
|
|
|
|
return fileInfos;
|
|
|
|
}
|
2019-03-09 12:30:38 -05:00
|
|
|
|
|
|
|
/** Reads the directory given by path and returns a list of file info
|
|
|
|
* synchronously.
|
|
|
|
*
|
|
|
|
* const files = Deno.readDirSync("/");
|
|
|
|
*/
|
|
|
|
export function readDirSync(path: string): FileInfo[] {
|
2019-08-21 20:42:48 -04:00
|
|
|
return res(sendSync(...req(path)));
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Reads the directory given by path and returns a list of file info.
|
|
|
|
*
|
|
|
|
* const files = await Deno.readDir("/");
|
|
|
|
*/
|
|
|
|
export async function readDir(path: string): Promise<FileInfo[]> {
|
2019-08-21 20:42:48 -04:00
|
|
|
return res(await sendAsync(...req(path)));
|
2019-03-09 12:30:38 -05:00
|
|
|
}
|