forked from solidjs-community/solid-primitives
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadapter-node.ts
More file actions
24 lines (23 loc) · 1.06 KB
/
adapter-node.ts
File metadata and controls
24 lines (23 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import { limitPath } from "./tools.js";
import { isServer } from "solid-js/web";
export const makeNodeFileSystem = isServer
? async (basePath: string = "/") => {
const fs = await import("fs/promises");
const p = limitPath(basePath);
return {
async: true as const,
getType: (path: string) =>
fs
.stat(p(path))
.then((stat: { isDirectory: () => boolean }) => (stat.isDirectory() ? "dir" : "file"))
.catch(() => null),
mkdir: (path: string) => fs.mkdir(p(path), { recursive: true }).then(() => undefined),
readdir: (path: string) => fs.readdir(p(path)) as Promise<[] | [string, ...string[]]>,
readFile: (path: string) => fs.readFile(p(path), { encoding: "utf8" }),
rename: (previous: string, next: string) => fs.rename(p(previous), p(next)),
rm: (path: string) => fs.rm(p(path), { recursive: true }),
writeFile: (path: string, data: string) =>
fs.writeFile(p(path), data, { encoding: "utf8" }),
};
}
: () => Promise.resolve(null);