-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathfsaccess-mock.ts
142 lines (136 loc) · 4.02 KB
/
fsaccess-mock.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { ObjectFileSystem } from "../src/index.js";
export class FileSystemHandle {
get kind(): "file" | "directory" {
return this._kind;
}
constructor(
private _kind: "file" | "directory",
public name: string,
) {}
isSameEntry(entry: FileSystemHandle) {
return entry == this;
}
}
export class FileSystemFileHandle extends FileSystemHandle {
constructor(
public name: string,
private data: string,
) {
super("file", name);
}
createWritable() {
const file = this;
let content = "";
const writable = {
write: (data: string) => {
content += data;
return Promise.resolve();
},
close: () => {
file.data = content;
return Promise.resolve();
},
};
return Promise.resolve(writable);
}
getFile() {
const file = this;
return new Promise(resolve => setTimeout(() => resolve({ text: () => file.data }), 20));
}
}
export class FileSystemDirectoryHandle extends FileSystemHandle {
constructor(
public name: string,
private contents: [string, FileSystemFileHandle | FileSystemDirectoryHandle][],
) {
super("directory", name);
}
async getDirectoryHandle(name: string, options?: { create?: boolean }) {
const directory = this.contents.find(item => item[0] === name);
if (directory?.[1].kind === "file") {
throw new Error("TypeMismatchError");
}
if (directory) {
return Promise.resolve(directory[1] as FileSystemDirectoryHandle);
}
if (!options?.create) {
throw new Error("NotFoundError");
}
const newDir = new FileSystemDirectoryHandle(name, []);
this.contents.push([name, newDir]);
return Promise.resolve(newDir);
}
async getFileHandle(name: string, options?: { create?: boolean }) {
const file = this.contents.find(item => item[0] === name);
if (file?.[1].kind === "directory") {
throw new Error("TypeMismatchError");
}
if (file) {
return Promise.resolve(file[1] as FileSystemFileHandle);
}
if (!options?.create) {
throw new Error("NotFoundError");
}
const newFile = new FileSystemFileHandle(name, "");
this.contents.push([name, newFile]);
return Promise.resolve(newFile);
}
async *entries() {
const entries = this.contents.map(item => Promise.resolve(item));
for await (const entry of entries) {
yield entry;
}
}
async *keys() {
const keys = this.contents.map(item => Promise.resolve(item[0]));
for await (const key of keys) {
yield key;
}
}
async *values() {
const values = this.contents.map(item => Promise.resolve(item[1]));
for await (const value of values) {
yield value;
}
}
async removeEntry(name: string, options?: { recursive?: boolean }) {
const itemIndex = this.contents.findIndex(item => item[0] == name);
const item = this.contents[itemIndex];
const dir: FileSystemDirectoryHandle | false =
item?.[1].kind === "directory" && (item[1] as FileSystemDirectoryHandle);
if (options?.recursive && dir) {
for await (const entry of dir.keys()) {
await dir.removeEntry(entry, { recursive: true });
}
}
this.contents.splice(itemIndex, 1);
return Promise.resolve();
}
}
export const createFromObject = (
obj: ObjectFileSystem | string,
name?: string,
): FileSystemHandle => {
if (name === undefined) {
return createFromObject(obj, "filesystem");
} else if (typeof obj === "string") {
return new FileSystemFileHandle(name, obj);
} else {
return new FileSystemDirectoryHandle(
name,
Object.entries(obj).map(
([name, content]: [string, ObjectFileSystem | string]) =>
[name, createFromObject(content, name)] as [
string,
FileSystemFileHandle | FileSystemDirectoryHandle,
],
),
);
}
};
(globalThis as any).currentFileSystem = createFromObject({
src: { "index.ts": "// test" },
data: { "data.json": "[1, 2, 3]" },
});
(globalThis as any).showDirectoryPicker = () =>
Promise.resolve((globalThis as any).currentFileSystem);