-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatchData.ts
51 lines (43 loc) · 1.52 KB
/
watchData.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
// import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url';
import chokidar from 'chokidar';
import buildSiteData from './generateProjects';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const folderPath = path.join(__dirname, 'work');
//check for "build" in command line args and run buildSiteData if found
const args = process.argv.slice(2);
if(args.includes('build')){
buildSiteData();
}else{
const watcher = chokidar.watch(folderPath, {
ignored: /(^|[/\\])\../, // ignore dotfiles
persistent: true,
});
watcher
.on('add', async (path) => {
// console.log(`File ${path} has been added`);
if(path.includes('!')) return;
await executeCode(`File ${path} has been added`);
})
.on('change', async (path) => {
// console.log(`File ${path} has been changed`);
if(path.includes('!')) return;
await executeCode(`File ${path} has been changed`);
})
.on('unlink', async (path) => {
// console.log(`File ${path} has been removed`);
if(path.includes('!')) return;
await executeCode(`File ${path} has been removed`);
});
let dataBuild = setTimeout(()=>{},0);
async function executeCode(reason) {
clearTimeout(dataBuild)
dataBuild = setTimeout(async ()=>{
console.log('triggered rebuild because: '+ reason)
await buildSiteData();
},250);
}
console.log(`Watching folder: ${folderPath}`);
}