feat: added file watcher
mellbacon committed
May 29, 2023 at 18:37 UTC
995476efe9d513432e3558fc17b4619a0d6852b4
6 files changed
+132
-2
package.json
+2
-1
@@ -17,7 +17,8 @@
17
"codemirror": "^6.0.1",
18
"sass": "^1.62.0",
19
"sortablejs": "^1.15.0",
20
- "svelte-splitpanes": "^0.7.13"
20
+ "svelte-splitpanes": "^0.7.13",
21
+ "tauri-plugin-fs-watch-api": "https://github.com/tauri-apps/tauri-plugin-fs-watch"
22
},
23
"devDependencies": {
24
"@sveltejs/vite-plugin-svelte": "^2.0.4",
src-tauri/Cargo.lock
+106
@@ -636,6 +636,15 @@ dependencies = [
636
"percent-encoding",
637
]
638
639
+[[package]]
640
+name = "fsevent-sys"
641
+version = "4.1.0"
642
+source = "registry+https://github.com/rust-lang/crates.io-index"
643
+checksum = "76ee7a02da4d231650c7cea31349b889be2f45ddb3ef3032d2ec8185f6313fd2"
644
+dependencies = [
645
+ "libc",
646
+]
647
+
648
[[package]]
649
name = "futf"
650
version = "0.1.5"
@@ -1138,6 +1147,26 @@ dependencies = [
1147
"cfb",
1148
]
1149
1150
+[[package]]
1151
+name = "inotify"
1152
+version = "0.9.6"
1153
+source = "registry+https://github.com/rust-lang/crates.io-index"
1154
+checksum = "f8069d3ec154eb856955c1c0fbffefbf5f3c40a104ec912d4797314c1801abff"
1155
+dependencies = [
1156
+ "bitflags",
1157
+ "inotify-sys",
1158
+ "libc",
1159
+]
1160
+
1161
+[[package]]
1162
+name = "inotify-sys"
1163
+version = "0.1.5"
1164
+source = "registry+https://github.com/rust-lang/crates.io-index"
1165
+checksum = "e05c02b5e89bff3b946cedeca278abc628fe811e604f027c45a8aa3cf793d0eb"
1166
+dependencies = [
1167
+ "libc",
1168
+]
1169
+
1170
[[package]]
1171
name = "instant"
1172
version = "0.1.12"
@@ -1233,6 +1262,26 @@ dependencies = [
1262
"treediff",
1263
]
1264
1265
+[[package]]
1266
+name = "kqueue"
1267
+version = "1.0.7"
1268
+source = "registry+https://github.com/rust-lang/crates.io-index"
1269
+checksum = "2c8fc60ba15bf51257aa9807a48a61013db043fcf3a78cb0d916e8e396dcad98"
1270
+dependencies = [
1271
+ "kqueue-sys",
1272
+ "libc",
1273
+]
1274
+
1275
+[[package]]
1276
+name = "kqueue-sys"
1277
+version = "1.0.3"
1278
+source = "registry+https://github.com/rust-lang/crates.io-index"
1279
+checksum = "8367585489f01bc55dd27404dcf56b95e6da061a256a666ab23be9ba96a2e587"
1280
+dependencies = [
1281
+ "bitflags",
1282
+ "libc",
1283
+]
1284
+
1285
[[package]]
1286
name = "kuchiki"
1287
version = "0.8.1"
@@ -1384,6 +1433,18 @@ dependencies = [
1433
"simd-adler32",
1434
]
1435
1436
+[[package]]
1437
+name = "mio"
1438
+version = "0.8.7"
1439
+source = "registry+https://github.com/rust-lang/crates.io-index"
1440
+checksum = "eebffdb73fe72e917997fad08bdbf31ac50b0fa91cec93e69a0662e4264d454c"
1441
+dependencies = [
1442
+ "libc",
1443
+ "log",
1444
+ "wasi 0.11.0+wasi-snapshot-preview1",
1445
+ "windows-sys 0.48.0",
1446
+]
1447
+
1448
[[package]]
1449
name = "ndk"
1450
version = "0.6.0"
@@ -1424,6 +1485,36 @@ version = "0.1.14"
1485
source = "registry+https://github.com/rust-lang/crates.io-index"
1486
checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
1487
1488
+[[package]]
1489
+name = "notify"
1490
+version = "6.0.0"
1491
+source = "registry+https://github.com/rust-lang/crates.io-index"
1492
+checksum = "4d9ba6c734de18ca27c8cef5cd7058aa4ac9f63596131e4c7e41e579319032a2"
1493
+dependencies = [
1494
+ "bitflags",
1495
+ "crossbeam-channel",
1496
+ "filetime",
1497
+ "fsevent-sys",
1498
+ "inotify",
1499
+ "kqueue",
1500
+ "libc",
1501
+ "mio",
1502
+ "serde",
1503
+ "walkdir",
1504
+ "windows-sys 0.45.0",
1505
+]
1506
+
1507
+[[package]]
1508
+name = "notify-debouncer-mini"
1509
+version = "0.3.0"
1510
+source = "registry+https://github.com/rust-lang/crates.io-index"
1511
+checksum = "e55ee272914f4563a2f8b8553eb6811f3c0caea81c756346bad15b7e3ef969f0"
1512
+dependencies = [
1513
+ "crossbeam-channel",
1514
+ "notify",
1515
+ "serde",
1516
+]
1517
+
1518
[[package]]
1519
name = "nu-ansi-term"
1520
version = "0.46.0"
@@ -1442,6 +1533,7 @@ dependencies = [
1533
"serde_json",
1534
"tauri",
1535
"tauri-build",
1536
+ "tauri-plugin-fs-watch",
1537
"windows 0.32.0",
1538
]
1539
@@ -2570,6 +2662,20 @@ dependencies = [
2662
"tauri-utils",
2663
]
2664
2665
+[[package]]
2666
+name = "tauri-plugin-fs-watch"
2667
+version = "0.0.0"
2668
+source = "git+https://github.com/tauri-apps/plugins-workspace?branch=v1#07406f1c9d7817c95dc9ebf4bfe2cdb1ea352045"
2669
+dependencies = [
2670
+ "log",
2671
+ "notify",
2672
+ "notify-debouncer-mini",
2673
+ "serde",
2674
+ "serde_json",
2675
+ "tauri",
2676
+ "thiserror",
2677
+]
2678
+
2679
[[package]]
2680
name = "tauri-runtime"
2681
version = "0.12.2"
src-tauri/Cargo.toml
+1
@@ -16,6 +16,7 @@ tauri-build = { version = "1.2.1", features = [] }
16
serde_json = "1.0"
17
serde = { version = "1.0", features = ["derive"] }
18
tauri = { version = "1.2.5", features = ["clipboard-write-text", "dialog-ask", "dialog-confirm", "dialog-open", "dialog-save", "fs-read-dir", "fs-read-file", "fs-rename-file", "fs-write-file", "path-all", "shell-execute", "shell-open", "window-center", "window-close", "window-create", "window-hide", "window-maximize", "window-minimize", "window-print", "window-set-decorations", "window-set-focus", "window-set-fullscreen", "window-set-icon", "window-show", "window-start-dragging", "window-unmaximize", "window-unminimize"] }
19
+tauri-plugin-fs-watch = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "v1" }
20
21
[dev-dependencies]
22
windows = "0.32.0"
src-tauri/src/main.rs
+1
@@ -6,6 +6,7 @@
6
fn main() {
7
tauri::Builder::default()
8
.invoke_handler(tauri::generate_handler![])
9
+ .plugin(tauri_plugin_fs_watch::init())
10
.run(tauri::generate_context!())
11
.expect("error while running tauri application");
12
}
src/lib/File.ts
+16
-1
@@ -3,6 +3,7 @@ import { get, writable } from 'svelte/store';
3
import { tabs, addEditorTab } from "./EditorTabList.svelte";
4
import { filetree } from "./FileTree.svelte";
5
import { Command } from '@tauri-apps/api/shell';
6
+import { watch } from "tauri-plugin-fs-watch-api";
7
8
export async function openFile() {
9
let newPath = await dialog.open() as string;
@@ -16,6 +17,20 @@ export async function openFolder() {
17
let directory = await dialog.open({directory: true}) as string;
18
if (!directory) return;
19
20
+ const directoryName = await updateTree(directory);
21
+ workspaceName.set(directoryName);
22
+
23
+ // load file watcher
24
+ await watch(
25
+ directory,
26
+ async () => {
27
+ await updateTree(directory);
28
+ },
29
+ { recursive: true }
30
+ )
31
+}
32
+
33
+async function updateTree(directory) {
34
let tree: any = await fs.readDir(directory, {recursive: true});
35
if (!tree) {
36
console.error("Cannot load directory");
@@ -24,8 +39,8 @@ export async function openFolder() {
39
let directoryName = directory.split(path.sep).pop();
40
tree = [{id: -1, name: directoryName, path: directory, children: buildTree(sortTree(tree))}];
41
filetree.set(tree);
27
- workspaceName.set(directoryName);
42
id = 0;
43
+ return directoryName;
44
}
45
46
// Need to add ids to each node for svelte to iterate over them properly
yarn.lock
+6
@@ -1216,6 +1216,12 @@ svelte@^3.58.0:
1216
resolved "https://registry.yarnpkg.com/svelte/-/svelte-3.58.0.tgz#d3e6f103efd6129e51c7d709225ad3b4c052b64e"
1217
integrity sha512-brIBNNB76mXFmU/Kerm4wFnkskBbluBDCjx/8TcpYRb298Yh2dztS2kQ6bhtjMcvUhd5ynClfwpz5h2gnzdQ1A==
1218
1219
+"tauri-plugin-fs-watch-api@https://github.com/tauri-apps/tauri-plugin-fs-watch":
1220
+ version "0.0.0"
1221
+ resolved "https://github.com/tauri-apps/tauri-plugin-fs-watch#5876048ff3d3d23c8863a37a0f0e91ed90553026"
1222
+ dependencies:
1223
+ "@tauri-apps/api" "^1.2.0"
1224
+
1225
to-regex-range@^5.0.1:
1226
version "5.0.1"
1227
resolved "https://registry.yarnpkg.com/to-regex-range/-/to-regex-range-5.0.1.tgz#1648c44aae7c8d988a326018ed72f5b4dd0392e4"