-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.js
62 lines (53 loc) · 1.61 KB
/
cli.js
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
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env node
import chokidar from 'chokidar'
import { exec } from 'child_process'
import * as fs from 'fs'
import process from 'process'
const chokidarPaths = [
'index.html',
'src/is-main.mts',
'src/main.ts',
'src/preload/**/*.mts',
'src/renderer/**/*.ts',
'src/renderer/**/*.mts',
'src/**/*'
];
const chokidarPathsIgnore = [
'src/**/*.css'
]
const chokidarDev = () => {
fs.open('.hmr_pid.txt', 'w', (err, fd) => {
if(!err) {
fs.close(fd, (err) => {
if(err) {
throw console.error(err);
}
})
} else {
throw console.error(err);
}
})
console.log("HMR is active");
chokidar.watch(chokidarPaths, { ignored: chokidarPathsIgnore }).on('all', (event, path) => {
if(event === 'change' && path !== null && path !== undefined) {
console.log("File changed: " + path);
console.log("Rebuilding files...");
try {
exec('npm run build').on('exit', () => {
fs.readFile('.hmr_pid.txt', { encoding: 'utf-8' }, (err, data) => {
if(!err) {
process.kill(data);
} else {
throw console.error(err);
}
})
console.log("Restarting Electron process...");
exec('npm run electron');
});
} catch(e) {
throw console.error(e);
}
}
})
}
chokidarDev();