2018-10-03 17:56:56 -04:00
|
|
|
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
|
2018-10-03 21:18:23 -04:00
|
|
|
import * as msg from "gen/msg_generated";
|
2018-10-03 17:56:56 -04:00
|
|
|
import { flatbuffers } from "flatbuffers";
|
|
|
|
import * as dispatch from "./dispatch";
|
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-14 16:29:50 -04:00
|
|
|
/** Reads the directory given by path and returns a list of file info
|
|
|
|
* synchronously.
|
2018-10-03 17:56:56 -04:00
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* import { readDirSync } from "deno";
|
|
|
|
* const files = readDirSync("/");
|
2018-10-03 17:56:56 -04:00
|
|
|
*/
|
|
|
|
export function readDirSync(path: string): FileInfo[] {
|
|
|
|
return res(dispatch.sendSync(...req(path)));
|
|
|
|
}
|
|
|
|
|
2018-10-14 16:29:50 -04:00
|
|
|
/** Reads the directory given by path and returns a list of file info.
|
2018-10-03 17:56:56 -04:00
|
|
|
*
|
2018-10-14 16:29:50 -04:00
|
|
|
* import { readDir } from "deno";
|
|
|
|
* const files = await readDir("/");
|
2018-10-03 17:56:56 -04:00
|
|
|
*/
|
|
|
|
export async function readDir(path: string): Promise<FileInfo[]> {
|
|
|
|
return res(await dispatch.sendAsync(...req(path)));
|
|
|
|
}
|
|
|
|
|
2018-10-03 21:18:23 -04:00
|
|
|
function req(path: string): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
|
2018-10-03 17:56:56 -04:00
|
|
|
const builder = new flatbuffers.Builder();
|
|
|
|
const path_ = builder.createString(path);
|
2018-10-03 21:18:23 -04:00
|
|
|
msg.ReadDir.startReadDir(builder);
|
|
|
|
msg.ReadDir.addPath(builder, path_);
|
|
|
|
const inner = msg.ReadDir.endReadDir(builder);
|
|
|
|
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;
|
|
|
|
}
|