/
fileWatcher.ts
199 lines (189 loc) · 5.88 KB
/
fileWatcher.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {
DEngineClientV2,
DNodeUtilsV2,
DVault,
NotePropsV2,
NoteUtilsV2,
} from "@dendronhq/common-all";
import { file2Note, string2Note } from "@dendronhq/common-server";
import { HistoryService, ParserUtilsV2 } from "@dendronhq/engine-server";
import _ from "lodash";
import path from "path";
import * as vscode from "vscode";
import { Logger } from "./logger";
import { DendronWorkspace } from "./workspace";
export class VaultWatcher {
public watchers: { vault: DVault; watcher: vscode.FileSystemWatcher }[];
/**
* Should watching be paused
*/
public pause: boolean;
public L = Logger;
public ws: DendronWorkspace;
public engine: DEngineClientV2;
constructor({ vaults }: { vaults: DVault[] }) {
this.watchers = vaults.map((vault) => {
const rootFolder = vault.fsPath;
const pattern = new vscode.RelativePattern(rootFolder, "*.md");
const watcher = vscode.workspace.createFileSystemWatcher(
pattern,
false,
false,
false
);
return { vault, watcher };
});
this.ws = DendronWorkspace.instance();
this.engine = this.ws.getEngine();
this.pause = false;
}
activate(context: vscode.ExtensionContext) {
this.watchers.forEach(({ watcher }) => {
context.subscriptions.push(watcher.onDidCreate(this.onDidCreate, this));
context.subscriptions.push(watcher.onDidDelete(this.onDidDelete, this));
context.subscriptions.push(
watcher.onDidChange(_.debounce(this.onDidChange, 500), this)
);
});
}
async onDidChange(uri: vscode.Uri) {
const ctx = "VaultWatcher:onDidChange";
this.L.debug({ ctx, uri: uri.fsPath });
if (this.pause) {
return;
}
this.L.info({ ctx, uri });
const eclient = DendronWorkspace.instance().getEngine();
const fname = path.basename(uri.fsPath, ".md");
const doc = await vscode.workspace.openTextDocument(uri);
const content = doc.getText();
const vault = DNodeUtilsV2.getVaultByDir({
vaults: eclient.vaultsv3,
dirPath: path.dirname(uri.fsPath),
});
let note = string2Note({ content, fname, vault });
const noteHydrated = NoteUtilsV2.getNoteByFnameV4({
fname,
vault,
notes: eclient.notes,
}) as NotePropsV2;
note = NoteUtilsV2.hydrate({ noteRaw: note, noteHydrated });
const links = ParserUtilsV2.findLinks({ note });
note.links = links;
await eclient.updateNote(note);
this.L.info({ ctx, fname, msg: "exit" });
}
async onDidCreate(uri: vscode.Uri): Promise<NotePropsV2 | undefined> {
const ctx = "VaultWatcher:onDidCreate";
if (this.pause) {
this.L.info({ ctx, uri, msg: "paused" });
return;
}
this.L.info({ ctx, uri });
const fname = path.basename(uri.fsPath, ".md");
// check if ignore
const recentEvents = HistoryService.instance().lookBack();
this.L.debug({ ctx, recentEvents, fname });
let note: NotePropsV2 | undefined;
try {
if (
_.find(recentEvents, (event) => {
return _.every([
event?.uri?.fsPath === uri.fsPath,
event.source === "engine",
event.action === "create",
]);
})
) {
this.L.debug({ ctx, uri, msg: "create by engine, ignoring" });
return;
}
try {
this.L.debug({ ctx, uri, msg: "pre-add-to-engine" });
const vault = DNodeUtilsV2.getVaultByDir({
dirPath: path.dirname(uri.fsPath),
vaults: this.engine.vaultsv3,
});
note = file2Note(uri.fsPath, vault);
const maybeNote = NoteUtilsV2.getNoteByFnameV4({
fname,
vault,
notes: this.engine.notes,
}) as NotePropsV2;
if (maybeNote) {
note = {
...note,
stub: false,
schemaStub: false,
..._.pick(maybeNote, ["children", "parent"]),
} as NotePropsV2;
}
await this.engine.updateNote(note as NotePropsV2, {
newNode: true,
});
this.L.debug({ ctx, uri, msg: "post-add-to-engine", note });
return note;
} catch (err) {
this.L.error({ ctx, err });
return note;
}
} finally {
this.L.debug({ ctx, uri, msg: "refreshTree" });
VaultWatcher.refreshTree();
return note;
}
}
async onDidDelete(uri: vscode.Uri) {
const ctx = "VaultWatcher:onDidDelete";
if (this.pause) {
return;
}
try {
this.L.info({ ctx, uri });
const fname = path.basename(uri.fsPath, ".md");
// check if we should ignore
const recentEvents = HistoryService.instance().lookBack(5);
this.L.debug({ ctx, recentEvents, fname });
if (
_.find(recentEvents, (event) => {
return _.every([
event?.uri?.fsPath === uri.fsPath,
event.source === "engine",
_.includes(["delete", "rename"], event.action),
]);
})
) {
this.L.debug({
ctx,
uri,
msg: "recent action by engine, ignoring",
});
return;
}
try {
this.L.debug({ ctx, uri, msg: "preparing to delete" });
const nodeToDelete = _.find(this.engine.notes, { fname });
if (_.isUndefined(nodeToDelete)) {
throw `${fname} not found`;
}
await this.engine.deleteNote(nodeToDelete.id, { metaOnly: true });
await HistoryService.instance().add({
action: "delete",
source: "watcher",
uri: uri,
});
} catch (err) {
this.L.info({ ctx, uri, err });
// NOTE: ignore, many legitimate reasons why this might happen
// this.L.error({ ctx, err: JSON.stringify(err) });
}
} finally {
VaultWatcher.refreshTree();
}
}
static refreshTree = _.debounce(() => {
const ctx = "refreshTree";
Logger.info({ ctx });
DendronWorkspace.instance().dendronTreeView?.treeProvider.refresh();
}, 100);
}