This repository was archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatcher.ts
More file actions
48 lines (43 loc) · 1.53 KB
/
watcher.ts
File metadata and controls
48 lines (43 loc) · 1.53 KB
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
import chokidar = require('chokidar');
import fs = require('fs-extra');
import { ChildProcess, spawn } from 'child_process';
import ignore from 'ignore';
import path = require('path');
const gitignoreContent = fs.readFileSync('.gitignore', 'utf8');
const ig = ignore().add(gitignoreContent);
const ignored = (testString: string) => {
if (testString.match(/^\.[^\/]+/)) { return true; }
const relativePath = path.relative(process.cwd(), testString);
if (!relativePath) { return false; }
return ig.ignores(relativePath);
};
const watcher = chokidar.watch('./', {
ignored,
persistent: true,
ignoreInitial: true,
awaitWriteFinish: true,
});
const slowWatcherEvents = ['add', 'change', 'unlink'];
slowWatcherEvents.forEach(event => watcher.on(event, path => {
console.log(`File ${event}: ${path}`);
if (path.endsWith('.css')) {
fs.copyFileSync(path, `test/${path}`);
} else {
rebuildSite();
}
}));
console.log(`Watcher is watching for changes... Press Ctrl+C to stop watching.`);
let currentBuildProcess: ChildProcess | null = null;
function rebuildSite() {
if (currentBuildProcess) {
console.log('Killing previous build process...');
currentBuildProcess.kill('SIGKILL');
currentBuildProcess = null;
}
console.log('Auto rebuilding...');
currentBuildProcess = spawn('npm', ['run', '_watch-do'], { stdio: 'inherit', shell: true, });
currentBuildProcess.on('exit', () => {
console.log('Auto rebuild done!');
currentBuildProcess = null;
});
}