-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserve.mjs
More file actions
36 lines (30 loc) · 931 Bytes
/
Copy pathserve.mjs
File metadata and controls
36 lines (30 loc) · 931 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
30
31
32
33
34
35
36
import chokidar from 'chokidar';
import { exec } from 'child_process';
import { createServer } from 'http-server';
const directoryToWatch = 'src';
const commandToRun = 'npm run build';
// Create a file watcher instance
const watcher = chokidar.watch(directoryToWatch, {
ignored: [
'build/**',
'node_modules/**'
],
persistent: true
});
watcher.on('change', (filePath) => {
console.log(`File ${filePath} has changed. Running command: ${commandToRun}`);
exec(commandToRun, (error, stdout, stderr) => {
if (error) {
console.error(`Error executing command: ${error.message}`);
return;
}
console.log(`Command output:\n${stdout}`);
});
});
// Create an HTTP server using http-server
const server = createServer({ root: './' });
// Start the HTTP server
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});