forked from SE-JE/venida
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch.ts
101 lines (71 loc) · 3.24 KB
/
watch.ts
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
let chokidar = require('chokidar');
const { exec } = require('child_process');
let listAllowedWatching = [
'./app', './config', './routes', './system'
];
let watcher = chokidar.watch(listAllowedWatching, {ignore: /^\./, persistent: true});
console.log('[Venida: Child Process] Starting watch file');
exec(`osascript -e 'display notification \"Staring Watch File\"'`);
let argument = process.argv[2];
/**
* Watcher event handler
*/
watcher
.on('change', async (path: string) => {
let splitedPath = path.split('/');
let fileName = splitedPath[splitedPath.length - 1];
let fileExtension = fileName.split('.')[fileName.split('.').length - 1];
switch (fileExtension) {
case 'ts': {
exec(`osascript -e 'display notification \"Waiting compile at ${fileName}\ file."'`);
exec(`ts-node compiler.ts ${path}`, (error: any, stdout: any, stderr: any) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
exec(`osascript -e 'display notification \"Compiled file ${fileName}\ successfully completed."'`);
if (argument?.toLowerCase() == 'server') {
exec('pm2 start venida.server.yaml', (error: any, stdout: any, stderr: any) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
exec(`osascript -e 'display notification \"Server restarted successfully!\"'`);
});
}
});
} break;
case 'json': {
exec(`osascript -e 'display notification \"Waiting copy ${fileName}\ file to ./build."'`);
let mainPath = (splitedPath.length == 1) ? './'.concat(path) : path;
exec(`cp ${mainPath} ./build/${path}`, (error: any, stdout: any, stderr: any) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
exec(`osascript -e 'display notification \"Copied file ${fileName}\ successfully completed."'`);
});
} break;
default: {
// do something..
}
}
})
.on('error', (error: any) => {
console.error(`Something wrong: ${error}`);
});