| 37 | } |
| 38 | |
| 39 | export class VaultWatcher { |
| 40 | private watcher: FSWatcher | null = null |
| 41 | private root: string | null = null |
| 42 | |
| 43 | start(root: string, onEvent: (ev: VaultChangeEvent) => void): void { |
| 44 | this.stop() |
| 45 | this.root = root |
| 46 | this.watcher = chokidar.watch(root, { |
| 47 | ignoreInitial: true, |
| 48 | persistent: true, |
| 49 | ignored: (p: string) => { |
| 50 | if (this.root && isVaultSettingsPath(this.root, p)) return false |
| 51 | if (this.root && relativeVaultPath(this.root, p) === INTERNAL_VAULT_DIR) return false |
| 52 | const base = path.basename(p) |
| 53 | return base.startsWith('.') || base === 'node_modules' |
| 54 | }, |
| 55 | awaitWriteFinish: { |
| 56 | stabilityThreshold: 120, |
| 57 | pollInterval: 40 |
| 58 | } |
| 59 | }) |
| 60 | |
| 61 | const handler = (kind: VaultChangeKind) => (absPath: string) => { |
| 62 | const base = path.basename(absPath) |
| 63 | if (!this.root) return |
| 64 | if (isVaultSettingsPath(this.root, absPath)) { |
| 65 | onEvent({ |
| 66 | kind, |
| 67 | path: VAULT_SETTINGS_RELATIVE_PATH, |
| 68 | folder: 'inbox', |
| 69 | scope: 'vault-settings' |
| 70 | }) |
| 71 | return |
| 72 | } |
| 73 | const commentsPath = commentsNotePath(this.root, absPath) |
| 74 | if (commentsPath) { |
| 75 | onEvent({ |
| 76 | kind, |
| 77 | path: commentsPath, |
| 78 | folder: folderForRelativePath(commentsPath) ?? 'inbox', |
| 79 | scope: 'comments' |
| 80 | }) |
| 81 | return |
| 82 | } |
| 83 | // Any database file — `<Name>.base/data.csv` or `schema.json` (or a legacy |
| 84 | // loose `.csv`/sidecar) — normalizes to the canonical `data.csv` path so |
| 85 | // the renderer re-hydrates the right database. (Record-page `.md` notes in |
| 86 | // a `.base` folder return null here and ride the normal note path below.) |
| 87 | const dbCsvPath = databaseCsvPathFor(toPosix(path.relative(this.root, absPath))) |
| 88 | if (dbCsvPath) { |
| 89 | onEvent({ |
| 90 | kind, |
| 91 | path: dbCsvPath, |
| 92 | folder: folderForRelativePath(dbCsvPath) ?? 'inbox', |
| 93 | scope: 'database' |
| 94 | }) |
| 95 | return |
| 96 | } |
nothing calls this directly
no outgoing calls
no test coverage detected