-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatchAndCopy.ts
More file actions
25 lines (23 loc) · 857 Bytes
/
watchAndCopy.ts
File metadata and controls
25 lines (23 loc) · 857 Bytes
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
import { debounce } from "https://deno.land/std@0.220.1/async/debounce.ts";
const dirToWatch = Deno.args[0];
const dirToCopyTo = Deno.args[1];
const watcher = Deno.watchFs(dirToWatch, { recursive: false });
const handleFileEvent = debounce((event: Deno.FsEvent) => {
for(const path of event.paths) {
switch(event.kind) {
case 'modify': {
const fileName = path.substring(path.lastIndexOf('/') + 1)
const destPath = [dirToCopyTo, fileName].join('/')
console.log('copy file', path, 'to', destPath)
Deno.copyFile(path, destPath)
break;
}
default:
console.log('unhandled event', event.kind, path)
break;
}
}
}, 200);
for await (const event of watcher) {
handleFileEvent(event)
}