-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·211 lines (185 loc) · 4.81 KB
/
app.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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env node
const { spawn } = require('child_process');
const path = require('path');
const readline = require('readline')
const commands = {
dev: [
// {
// id: 'admin',
// // cwd: path.join(__dirname, 'admin'),
// env: {
// API_URL: "http://localhost:9999/",
// BUILD_PREFIXED: "admin",
// PART: "admin",
// },
// command: 'node_modules/.bin/vue-cli-service',
// args: ['serve', 'admin/src/main.js'],
// },
{
id: 'frontend',
cwd: path.join(__dirname, 'frontend'),
env: {
// API_URL: "http://localhost:9999/",
PART: "frontend",
FORCE_COLOR: "1",
},
command: path.join(__dirname, "node_modules/.bin/vite"),
args: ['serve', '--config', 'vite.config.mjs', '--port', '8090'],
},
// {
// id: 'backend',
// cwd: path.join(__dirname, 'backend'),
// env: {
// PORT: "9999",
// },
// command: '../node_modules/.bin/nodemon',
// args: ['app.js'],
// },
],
build: [
// {
// id: 'admin',
// // cwd: path.join(__dirname, 'admin'),
// env: {
// BUILD_PREFIXED: "admin",
// API_URL: "/",
// PART: "admin",
// NODE_ENV: "production"
// },
// command: 'node_modules/.bin/vue-cli-service',
// args: ['build', 'admin/src/main.js', '--mode production'],
// },
{
id: 'frontend',
cwd: path.join(__dirname, 'frontend'),
env: {
BUILD_PREFIXED: "",
API_URL: "/",
PART: "frontend",
NODE_ENV: "production",
FORCE_COLOR: "1",
},
command: path.join(__dirname, 'node_modules/.bin/vite'),
args: ['build', '--config', 'vite.config.mjs'],
},
],
run: [
{
id: 'backend',
cwd: path.join(__dirname, 'backend'),
env: {
PORT: "9999",
FORCE_COLOR: "1",
},
command: 'node',
args: ['app.js'],
},
],
};
function clearConsole() {
if (process.stdout.isTTY) {
const blank = '\n'.repeat(process.stdout.rows);
console.log(blank);
readline.cursorTo(process.stdout, 0, 0);
readline.clearScreenDown(process.stdout);
}
}
const out = {};
let clearConsoleTimeout = null;
function doOutput() {
clearConsole();
for (let processId in out) {
const toDisplay = out[processId].slice(-15);
for (let line of toDisplay) {
// console.log(new TextEncoder().encode(line))
// console.log(`${processId}: ${line}`);
console.log(`${processId}: ${line}`);
}
console.log('');
console.log('');
}
}
function log(processId, str) {
if (!out[processId]) {
out[processId] = [];
}
let data = (''+str).trim();
const clearLineStrings = [String.fromCharCode(27)+"[2K", String.fromCharCode(27)+"[1A", String.fromCharCode(27)+"[G"];
for (const clearLineString of clearLineStrings) {
data = data.split(clearLineString).join('');
}
data = data.split("\r").join("\n").split("\n\n").join('').split("\n");
for (const line of data) {
if (line && (!out[processId].length || out[processId][out[processId].length - 1] != line)) {
out[processId].push(line);
console.log(`${processId}: ${line}`);
}
}
// if (data) {
// out[processId].push(data);
// console.log(`${processId}: ${data}`);
// }
clearTimeout(clearConsoleTimeout);
clearConsoleTimeout = setTimeout(()=>{
doOutput();
}, 2000);
}
const spawned = [];
async function doSpawn(settings) {
const envCopy = {...process.env};
Object.assign(envCopy, settings.env);
const options = {
env: envCopy,
cwd: settings.cwd,
// stdio: 'inherit',
};
const proc = spawn(settings.command, settings.args, options);
spawned.push(proc);
return await new Promise((res)=>{
proc.stdout.on('data', (data) => {
log(settings.id, data);
// console.log(`${settings.id}: ${(''+data).trim()}`);
});
proc.stderr.on('data', (data) => {
log(settings.id, data);
// console.log(`${settings.id}: ${(''+data).trim()}`);
});
proc.on('close', () => {
// console.log(`child process ${settings.id} exited with code ${code}`);
res(true);
});
proc.on('error', (data) => {
log(settings.id, 'error: '+data);
// console.log(`${settings.id}: error: ${(''+data).trim()}`);
res(false);
// console.log(`child process ${settings.id} exited with code ${err}`);
});
});
}
function cleanUpServer() {
console.log('Finishing sub processes...');
for (let proc of spawned) {
proc.kill('SIGHUP');
}
console.log('Done.');
}
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach((eventType) => {
process.on(eventType, cleanUpServer.bind(null, eventType));
});
async function run(what) {
if (!commands[what]) {
console.error('Invalid task name: '+what);
console.error('Available commands are: '+Object.keys(commands).join(', '));
return false;
}
const settings = commands[what];
const promises = [];
for (let commandSetting of settings) {
promises.push( doSpawn(commandSetting) );
}
return await Promise.all(promises);
}
const args = process.argv.slice(2);
run(args[0]).then(()=>{
console.log('');
});