-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·62 lines (52 loc) · 1.68 KB
/
index.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
const spawn = require('child_process').spawn;
const exec = require('child_process').exec;
const chokidar = require('chokidar');
const path = require('path');
const kill = require('tree-kill');
class Processing_Hotreloader {
constructor() {
this.__init__();
}
__init__ = () => {
this.args = process.argv;
this.fileName = this.args[2];
this.cwd = process.cwd();
this.watchPaths = [
path.join(this.cwd, "/**/*.pde")
];
this.ignoredPaths = "**/node_modules/*";
this.reload();
this.startWatching();
this.listeningEvents();
}
reload = () => {
if (this.nodeServer) {
kill(this.nodeServer.pid);
exec('killall java', (err, stdout, stderr) => {
this.nodeServer = spawn('processing-java', [`--sketch=${this.cwd}`, "--run"], { stdio: [process.stdin, process.stdout, process.stderr] });
})
} else
this.nodeServer = spawn('processing-java', [`--sketch=${this.cwd}`, "--run"], { stdio: [process.stdin, process.stdout, process.stderr] });
}
startWatching = () => {
chokidar.watch(this.watchPaths, {
ignored: this.ignoredPaths,
ignoreInitial: true
}).on('all', (event, path) => {
this.reload();
});
}
listeningEvents = () => {
// listening on CLI input
process.stdin.on("data", (chunk) => {
let cliInput = chunk.toString();
switch (cliInput) {
case 'rs\n':
this.reload();
break
}
});
}
}
new Processing_Hotreloader();