-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.js
More file actions
29 lines (26 loc) · 1019 Bytes
/
Copy pathwatch.js
File metadata and controls
29 lines (26 loc) · 1019 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
26
27
28
29
/* eslint-disable no-console */
import {watch} from 'fs';
import {bundleJs, minifyHTML, compileSass} from './build.js';
import {projectPaths} from './build.config.js';
async function watchFiles() {
const watchList = [
{path: projectPaths.styles.srcDir, action: compileSass, label: 'SASS'},
{path: projectPaths.html.index, action: minifyHTML, label: 'HTML'},
{path: projectPaths.js.src, action: bundleJs, label: 'JS'}
];
const lastEvent = new Map();
watchList.forEach(({path: watchPath, action, label}) => {
watch(watchPath, {recursive: true}, async (eventType, filename) => {
const now = Date.now();
if (lastEvent.has(label) && now - lastEvent.get(label) < 100) {return;}
if (!filename || eventType !== 'change' || filename.includes('~')) {return;}
lastEvent.set(label, now);
try {
await action(filename);
} catch (err) {
console.error(`WATCH ${label} ERR:`, err);
}
});
});
}
watchFiles().catch((error) => console.log(error));