vvvfs 0.0.9 → 0.1.1
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 +9 -0
- package/index.html +1 -0
- package/index.ts +67 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -48,6 +48,7 @@ const vvvfs = new VVVFS("vvvfs", {
|
|
|
48
48
|
});
|
|
49
49
|
await vvvfs.createDir("/home/user/Desktop"); // 创建目录,返回true和false
|
|
50
50
|
await vvvfs.writeText("/home/user/Desktop/test.txt", "Hello World!"); // 写入文本文件,写入文件还包括write(path: string, content: Blob)和writeJson(path: string, content: Record<string, any>)方法,返回true和false
|
|
51
|
+
await vvvfs.appendText("/home/user/Desktop/test.txt", "Hello World!"); // 追加文本文件,返回true和false
|
|
51
52
|
console.log(await vvvfs.readText("/home/user/Desktop/test.txt")); // 读取文本文件,读取文件还包括read(path: string): Blob | null和readJson(path: string): Record<string, any> | null方法
|
|
52
53
|
await vvvfs.delete("/home/user/Desktop/test.txt"); // 删除文件,返回true和false
|
|
53
54
|
if (await vvvfs.exists("/home/user/Desktop")) {
|
|
@@ -82,6 +83,14 @@ const vvvfs = new VVVFS("vvvfs", {
|
|
|
82
83
|
|
|
83
84
|
## 更新日志
|
|
84
85
|
|
|
86
|
+
### 0.1.1
|
|
87
|
+
|
|
88
|
+
- 新增 `append` 和 `appendText` 方法,用于追加内容
|
|
89
|
+
|
|
90
|
+
### 0.1.0
|
|
91
|
+
|
|
92
|
+
- 修复 `init` 重复初始化时重复写入已存在文件和目录
|
|
93
|
+
|
|
85
94
|
### 0.0.9
|
|
86
95
|
|
|
87
96
|
- 更新 `watch` 方法,监听时,返回 `true` 表示取消该操作
|
package/index.html
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
});
|
|
24
24
|
console.log("创建文件", await vvvfs.createFile("/test.txt"));
|
|
25
25
|
console.log("写入文件", await vvvfs.writeText("/test.txt", "Hello World"));
|
|
26
|
+
console.log("追加文件", await vvvfs.appendText("/test.txt", " - Appended"));
|
|
26
27
|
console.log("文件是否存在", await vvvfs.exists("/test.txt"));
|
|
27
28
|
console.log("文件是否存在", await vvvfs.exists("/test2.txt"));
|
|
28
29
|
console.log("读取文件", await vvvfs.readText("/test.txt"));
|
package/index.ts
CHANGED
|
@@ -150,6 +150,20 @@ class VVVFSFile {
|
|
|
150
150
|
async writeJSON(json: Record<string, any>, format: boolean = true) {
|
|
151
151
|
return await this._vvvfs.writeJson(this._path, json, format);
|
|
152
152
|
}
|
|
153
|
+
/**
|
|
154
|
+
* 追加文件内容
|
|
155
|
+
* @param file 文件对象
|
|
156
|
+
*/
|
|
157
|
+
async append(file: Blob) {
|
|
158
|
+
return await this._vvvfs.append(this._path, file);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* 追加文件文本内容
|
|
162
|
+
* @param text 文件文本内容
|
|
163
|
+
*/
|
|
164
|
+
async appendText(text: string) {
|
|
165
|
+
return await this._vvvfs.appendText(this._path, text);
|
|
166
|
+
}
|
|
153
167
|
/**
|
|
154
168
|
* 创建文件
|
|
155
169
|
*/
|
|
@@ -389,6 +403,7 @@ class VVVFS {
|
|
|
389
403
|
];
|
|
390
404
|
try {
|
|
391
405
|
for (const file of linuxInitFiles) {
|
|
406
|
+
if (await this.exists(file.path)) continue;
|
|
392
407
|
await this.db.files.put(file);
|
|
393
408
|
}
|
|
394
409
|
} catch (error) {
|
|
@@ -624,6 +639,58 @@ class VVVFS {
|
|
|
624
639
|
return false;
|
|
625
640
|
}
|
|
626
641
|
}
|
|
642
|
+
/**
|
|
643
|
+
* 追加内容
|
|
644
|
+
* @param path 文件路径
|
|
645
|
+
* @param content 追加内容
|
|
646
|
+
*/
|
|
647
|
+
async append(path: string, content: Blob) {
|
|
648
|
+
try {
|
|
649
|
+
const targetPath = joinPath(path);
|
|
650
|
+
if (this.watchers[targetPath]) {
|
|
651
|
+
for (const handler of this.watchers[targetPath]) {
|
|
652
|
+
if (await handler("append")) {
|
|
653
|
+
if (this.options.throwError) {
|
|
654
|
+
throw new VVVFSError("Append", "追加文件失败:监听器取消了操作");
|
|
655
|
+
}
|
|
656
|
+
return false;
|
|
657
|
+
}
|
|
658
|
+
}
|
|
659
|
+
}
|
|
660
|
+
const existingFile = await this.read(targetPath);
|
|
661
|
+
if (existingFile) {
|
|
662
|
+
const blob = new Blob([existingFile, content], {
|
|
663
|
+
type: "application/octet-stream",
|
|
664
|
+
});
|
|
665
|
+
return await this.write(targetPath, blob);
|
|
666
|
+
} else {
|
|
667
|
+
return await this.write(targetPath, content);
|
|
668
|
+
}
|
|
669
|
+
} catch (error) {
|
|
670
|
+
console.error("追加文件失败", error);
|
|
671
|
+
if (this.options.throwError) {
|
|
672
|
+
throw new VVVFSError("Append", "追加文件失败" + error);
|
|
673
|
+
}
|
|
674
|
+
return false;
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
/**
|
|
678
|
+
* 追加文本内容
|
|
679
|
+
* @param path 文件路径
|
|
680
|
+
* @param content 追加内容
|
|
681
|
+
*/
|
|
682
|
+
async appendText(path: string, content: string) {
|
|
683
|
+
try {
|
|
684
|
+
const blob = new Blob([content], { type: "text/plain" });
|
|
685
|
+
return await this.append(path, blob);
|
|
686
|
+
} catch (error) {
|
|
687
|
+
console.error("追加文件失败", error);
|
|
688
|
+
if (this.options.throwError) {
|
|
689
|
+
throw new VVVFSError("Append", "追加文件失败" + error);
|
|
690
|
+
}
|
|
691
|
+
return false;
|
|
692
|
+
}
|
|
693
|
+
}
|
|
627
694
|
/**
|
|
628
695
|
* 读取文件内容
|
|
629
696
|
* @param path 文件路径
|