Rewrite listFiles to use std lib's "walk" + test
parent
8a27269d91
commit
cd5c2ff9c2
|
@ -9,6 +9,7 @@ import {
|
||||||
base64DecodeDataUrl,
|
base64DecodeDataUrl,
|
||||||
base64EncodedDataUrl,
|
base64EncodedDataUrl,
|
||||||
} from "../../plugos/asset_bundle/base64.ts";
|
} from "../../plugos/asset_bundle/base64.ts";
|
||||||
|
import { walk } from "../../plugos/deps.ts";
|
||||||
|
|
||||||
function lookupContentType(path: string): string {
|
function lookupContentType(path: string): string {
|
||||||
return mime.getType(path) || "application/octet-stream";
|
return mime.getType(path) || "application/octet-stream";
|
||||||
|
@ -149,32 +150,26 @@ export class DiskSpacePrimitives implements SpacePrimitives {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchFileList(): Promise<FileMeta[]> {
|
async fetchFileList(): Promise<FileMeta[]> {
|
||||||
const fileList: FileMeta[] = [];
|
const allFiles: FileMeta[] = [];
|
||||||
|
for await (
|
||||||
const walkPath = async (dir: string) => {
|
const file of walk(this.rootPath, {
|
||||||
for await (const file of Deno.readDir(dir)) {
|
includeDirs: false,
|
||||||
if (file.name.startsWith(".")) {
|
// Exclude hidden directories
|
||||||
continue;
|
skip: [/^.*\/\..+$/],
|
||||||
}
|
})
|
||||||
const fullPath = path.join(dir, file.name);
|
) {
|
||||||
|
const fullPath = file.path;
|
||||||
const s = await Deno.stat(fullPath);
|
const s = await Deno.stat(fullPath);
|
||||||
if (file.isDirectory) {
|
allFiles.push({
|
||||||
await walkPath(fullPath);
|
name: fullPath.substring(this.rootPath.length + 1),
|
||||||
} else {
|
|
||||||
if (!file.name.startsWith(".")) {
|
|
||||||
fileList.push({
|
|
||||||
name: this.pathToFilename(fullPath),
|
|
||||||
size: s.size,
|
|
||||||
contentType: lookupContentType(fullPath),
|
|
||||||
lastModified: s.mtime!.getTime(),
|
lastModified: s.mtime!.getTime(),
|
||||||
|
contentType: mime.getType(fullPath) || "application/octet-stream",
|
||||||
|
size: s.size,
|
||||||
perm: "rw",
|
perm: "rw",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
return allFiles;
|
||||||
};
|
|
||||||
await walkPath(this.rootPath);
|
|
||||||
return fileList;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Plugs
|
// Plugs
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
import { assert } from "../../test_deps.ts";
|
||||||
|
import { FileMeta } from "../../common/types.ts";
|
||||||
|
import { path } from "../deps.ts";
|
||||||
|
import fileSystemSyscalls from "./fs.deno.ts";
|
||||||
|
|
||||||
|
const fakeCtx = {} as any;
|
||||||
|
|
||||||
|
Deno.test("Test FS operations", async () => {
|
||||||
|
const thisFolder = path.dirname(new URL(import.meta.url).pathname);
|
||||||
|
const syscalls = fileSystemSyscalls(thisFolder);
|
||||||
|
const allFiles: FileMeta[] = await syscalls["fs.listFiles"](
|
||||||
|
fakeCtx,
|
||||||
|
thisFolder,
|
||||||
|
true,
|
||||||
|
);
|
||||||
|
assert(allFiles.find((f) => f.name === "fs.deno.test.ts"));
|
||||||
|
});
|
|
@ -1,5 +1,5 @@
|
||||||
import type { SysCallMapping } from "../system.ts";
|
import type { SysCallMapping } from "../system.ts";
|
||||||
import { mime, path } from "../deps.ts";
|
import { mime, path, walk } from "../deps.ts";
|
||||||
import { base64DecodeDataUrl, base64Encode } from "../asset_bundle/base64.ts";
|
import { base64DecodeDataUrl, base64Encode } from "../asset_bundle/base64.ts";
|
||||||
import { FileMeta } from "../../common/types.ts";
|
import { FileMeta } from "../../common/types.ts";
|
||||||
|
|
||||||
|
@ -72,15 +72,16 @@ export default function fileSystemSyscalls(root = "/"): SysCallMapping {
|
||||||
): Promise<FileMeta[]> => {
|
): Promise<FileMeta[]> => {
|
||||||
dirPath = resolvedPath(dirPath);
|
dirPath = resolvedPath(dirPath);
|
||||||
const allFiles: FileMeta[] = [];
|
const allFiles: FileMeta[] = [];
|
||||||
|
for await (
|
||||||
async function walkPath(dir: string) {
|
const file of walk(dirPath, {
|
||||||
const files = await Deno.readDir(dir);
|
includeDirs: false,
|
||||||
for await (const file of files) {
|
// Exclude hidden files
|
||||||
const fullPath = path.join(dir, file.name);
|
skip: [/^.*\/\..+$/],
|
||||||
|
maxDepth: recursive ? Infinity : 1,
|
||||||
|
})
|
||||||
|
) {
|
||||||
|
const fullPath = file.path;
|
||||||
const s = await Deno.stat(fullPath);
|
const s = await Deno.stat(fullPath);
|
||||||
if (s.isDirectory && recursive) {
|
|
||||||
await walkPath(fullPath);
|
|
||||||
} else {
|
|
||||||
allFiles.push({
|
allFiles.push({
|
||||||
name: fullPath.substring(dirPath.length + 1),
|
name: fullPath.substring(dirPath.length + 1),
|
||||||
lastModified: s.mtime!.getTime(),
|
lastModified: s.mtime!.getTime(),
|
||||||
|
@ -89,9 +90,6 @@ export default function fileSystemSyscalls(root = "/"): SysCallMapping {
|
||||||
perm: "rw",
|
perm: "rw",
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
await walkPath(dirPath);
|
|
||||||
return allFiles;
|
return allFiles;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue