-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatch.js
More file actions
26 lines (24 loc) · 720 Bytes
/
watch.js
File metadata and controls
26 lines (24 loc) · 720 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
import fs from 'fs';
fs.watchFile('./output/result.html', async () => {
console.log('Change file');
try {
await fs.promises.mkdir('./tmp', { recursive: true });
const data = await fs.promises.readFile('./output/result.html', 'utf-8');
const fileName = `./tmp/result_${Math.floor(
Math.random() * 1000000000,
)}.html`;
await fs.promises.writeFile(fileName, data);
} catch (err) {
console.log(err);
}
});
process.on('SIGINT', async () => {
console.log('exiting');
try {
await fs.promises.rmdir('./tmp', { recursive: true });
process.exit();
} catch (err) {
console.log(err);
process.exit();
}
});