vvvfs 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +51 -39
- package/dist/vvvfs.min.js +3 -3
- package/esbuild.config.js +2 -1
- package/index.html +1 -1
- package/index.ts +153 -6
- package/package.json +1 -1
package/esbuild.config.js
CHANGED
|
@@ -9,10 +9,11 @@ build({
|
|
|
9
9
|
outfile: 'dist/vvvfs.min.js',
|
|
10
10
|
format: 'iife',
|
|
11
11
|
define: {
|
|
12
|
-
'process.env.PACKAGE_VERSION': JSON.stringify(pkg.version),
|
|
12
|
+
'process.env.PACKAGE_VERSION': JSON.stringify(pkg.version),
|
|
13
13
|
},
|
|
14
14
|
minify: true,
|
|
15
15
|
platform: 'browser',
|
|
16
16
|
target: ["chrome80", "firefox70", "safari13", "edge80"],
|
|
17
17
|
logLevel: 'info',
|
|
18
|
+
drop: ['console', 'debugger'],
|
|
18
19
|
}).catch(() => process.exit(1));
|
package/index.html
CHANGED
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
console.log("文件是否存在", await vvvfs.exists("/test2.txt"));
|
|
23
23
|
console.log("读取文件", await vvvfs.readText("/test.txt"));
|
|
24
24
|
console.log("复制文件", await vvvfs.copy("/test.txt", "/test2.txt"));
|
|
25
|
-
console.log("搜索文件", await vvvfs.search("
|
|
25
|
+
console.log("搜索文件", await vvvfs.search("/", "t"));
|
|
26
26
|
console.log("移动文件", await vvvfs.move("/test2.txt", "/test3.txt"));
|
|
27
27
|
console.log("删除文件", await vvvfs.delete("/test.txt"));
|
|
28
28
|
} catch (error) {
|
package/index.ts
CHANGED
|
@@ -54,6 +54,146 @@ class VVVFSError extends Error {
|
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
/**
|
|
58
|
+
* 虚拟文件系统文件类
|
|
59
|
+
* @param path 文件路径
|
|
60
|
+
*/
|
|
61
|
+
class VVVFSFile {
|
|
62
|
+
/**
|
|
63
|
+
* 文件路径
|
|
64
|
+
*/
|
|
65
|
+
private _path: string;
|
|
66
|
+
/**
|
|
67
|
+
* 虚拟文件系统实例
|
|
68
|
+
*/
|
|
69
|
+
private _vvvfs: VVVFS = new VVVFS();
|
|
70
|
+
/**
|
|
71
|
+
* 获取文件路径
|
|
72
|
+
*/
|
|
73
|
+
get path() {
|
|
74
|
+
return this._path;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* 设置文件路径
|
|
78
|
+
* @param path 文件路径
|
|
79
|
+
*/
|
|
80
|
+
set path(path: string) {
|
|
81
|
+
this._path = joinPath(path);
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* 构造函数
|
|
85
|
+
* @param paths 文件路径
|
|
86
|
+
*/
|
|
87
|
+
constructor(...paths: string[]) {
|
|
88
|
+
this._path = joinPath(...paths);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* 读取文件
|
|
92
|
+
*/
|
|
93
|
+
async read() {
|
|
94
|
+
return await this._vvvfs.read(this._path);
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* 读取文件文本内容
|
|
98
|
+
*/
|
|
99
|
+
async readText() {
|
|
100
|
+
return await this._vvvfs.readText(this._path);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* 读取文件JSON内容
|
|
104
|
+
*/
|
|
105
|
+
async readJSON() {
|
|
106
|
+
return await this._vvvfs.readJson(this._path);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 写入文件
|
|
110
|
+
* @param file 文件对象
|
|
111
|
+
*/
|
|
112
|
+
async write(file: Blob) {
|
|
113
|
+
return await this._vvvfs.write(this._path, file);
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 写入文件文本内容
|
|
117
|
+
* @param text 文件文本内容
|
|
118
|
+
*/
|
|
119
|
+
async writeText(text: string) {
|
|
120
|
+
return await this._vvvfs.writeText(this._path, text);
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* 写入文件JSON内容
|
|
124
|
+
* @param json 文件JSON内容
|
|
125
|
+
*/
|
|
126
|
+
async writeJSON(json: Record<string, any>, format: boolean = true) {
|
|
127
|
+
return await this._vvvfs.writeJson(this._path, json, format);
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 创建文件
|
|
131
|
+
*/
|
|
132
|
+
async createFile() {
|
|
133
|
+
return await this._vvvfs.createFile(this._path);
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* 创建文件夹
|
|
137
|
+
*/
|
|
138
|
+
async createDir() {
|
|
139
|
+
return await this._vvvfs.createDir(this._path);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* 删除文件
|
|
143
|
+
*/
|
|
144
|
+
async delete() {
|
|
145
|
+
return await this._vvvfs.delete(this._path);
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* 判断文件是否存在
|
|
149
|
+
*/
|
|
150
|
+
async exists() {
|
|
151
|
+
return await this._vvvfs.exists(this._path);
|
|
152
|
+
}
|
|
153
|
+
/**
|
|
154
|
+
* 获取文件大小
|
|
155
|
+
*/
|
|
156
|
+
async isFile() {
|
|
157
|
+
return await this._vvvfs.isFile(this._path);
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* 判断文件是否是文件夹
|
|
161
|
+
*/
|
|
162
|
+
async isDir() {
|
|
163
|
+
return await this._vvvfs.isDir(this._path);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 列出文件
|
|
167
|
+
*/
|
|
168
|
+
async list() {
|
|
169
|
+
return await this._vvvfs.list(this._path);
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* 重命名文件
|
|
173
|
+
*/
|
|
174
|
+
async rename(newName: string) {
|
|
175
|
+
return await this._vvvfs.rename(this._path, newName);
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* 移动文件
|
|
179
|
+
*/
|
|
180
|
+
async move(newPath: string) {
|
|
181
|
+
return await this._vvvfs.move(this._path, newPath);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* 复制文件
|
|
185
|
+
*/
|
|
186
|
+
async copy(newPath: string) {
|
|
187
|
+
return await this._vvvfs.copy(this._path, newPath);
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 搜索文件
|
|
191
|
+
*/
|
|
192
|
+
async search(query: string) {
|
|
193
|
+
return await this._vvvfs.search(this._path, query);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
57
197
|
const version = packageJson.version;
|
|
58
198
|
class VVVFS {
|
|
59
199
|
private db: VVVFSDatabase;
|
|
@@ -62,6 +202,10 @@ class VVVFS {
|
|
|
62
202
|
* 虚拟文件系统版本
|
|
63
203
|
*/
|
|
64
204
|
static version: string;
|
|
205
|
+
/**
|
|
206
|
+
* 虚拟文件系统文件类
|
|
207
|
+
*/
|
|
208
|
+
static File = VVVFSFile;
|
|
65
209
|
/**
|
|
66
210
|
* 创建虚拟文件系统
|
|
67
211
|
* @param name 虚拟文件系统名称
|
|
@@ -77,7 +221,7 @@ class VVVFS {
|
|
|
77
221
|
try {
|
|
78
222
|
this.db = new Dexie(name) as VVVFSDatabase;
|
|
79
223
|
this.db.version(1).stores({
|
|
80
|
-
files: "++id, name, path, type, file",
|
|
224
|
+
files: "++id, name, path, type, file, [name+path+type]",
|
|
81
225
|
});
|
|
82
226
|
} catch (error) {
|
|
83
227
|
console.error("创建数据库失败", error);
|
|
@@ -228,7 +372,7 @@ class VVVFS {
|
|
|
228
372
|
await this.db.delete();
|
|
229
373
|
this.db = new Dexie(this.db.name) as VVVFSDatabase;
|
|
230
374
|
this.db.version(1).stores({
|
|
231
|
-
files: "++id, name, path, type, file",
|
|
375
|
+
files: "++id, name, path, type, file, [name+path+type]",
|
|
232
376
|
});
|
|
233
377
|
} catch (error) {
|
|
234
378
|
console.error("重置数据库失败", error);
|
|
@@ -404,7 +548,7 @@ class VVVFS {
|
|
|
404
548
|
* @param content JSON内容
|
|
405
549
|
* @param format 是否格式化
|
|
406
550
|
*/
|
|
407
|
-
async writeJson(path: string, content: Record<string, any>, format
|
|
551
|
+
async writeJson(path: string, content: Record<string, any>, format: boolean = true) {
|
|
408
552
|
try {
|
|
409
553
|
return await this.writeText(
|
|
410
554
|
path,
|
|
@@ -532,7 +676,9 @@ class VVVFS {
|
|
|
532
676
|
async list(path: string) {
|
|
533
677
|
try {
|
|
534
678
|
const { name, parent } = parsePath(path);
|
|
535
|
-
return (await this.db.files.where({ path: parent }).toArray())
|
|
679
|
+
return (await this.db.files.where({ path: parent }).toArray())
|
|
680
|
+
.map((file) => file.name)
|
|
681
|
+
.filter((item) => item != "");
|
|
536
682
|
} catch (error) {
|
|
537
683
|
console.error("列出目录下的文件失败", error);
|
|
538
684
|
if (this.options.throwError) {
|
|
@@ -729,8 +875,9 @@ class VVVFS {
|
|
|
729
875
|
await that.list(joinPath(parent, file)),
|
|
730
876
|
)),
|
|
731
877
|
);
|
|
732
|
-
} else {
|
|
878
|
+
} else if (await that.isFile(joinPath(parent, file))) {
|
|
733
879
|
if (file.includes(query)) {
|
|
880
|
+
console.log(joinPath(parent, file));
|
|
734
881
|
result.push(joinPath(parent, file));
|
|
735
882
|
}
|
|
736
883
|
}
|
|
@@ -782,7 +929,7 @@ function joinPath(...paths: string[]) {
|
|
|
782
929
|
if (isAbsolute) {
|
|
783
930
|
result = "/" + result;
|
|
784
931
|
}
|
|
785
|
-
return result || (isAbsolute ? "/" : ".");
|
|
932
|
+
return result.startsWith("/") ? result : "/" + result || (isAbsolute ? "/" : ".");
|
|
786
933
|
}
|
|
787
934
|
Object.defineProperty(VVVFS, "version", {
|
|
788
935
|
value: version,
|