-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.ts
More file actions
101 lines (84 loc) · 2.91 KB
/
process.ts
File metadata and controls
101 lines (84 loc) · 2.91 KB
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
// import * as readline from 'readline';
import { exec } from 'child_process';
import { logError } from './log';
export const subProcesses = [];
export function addSubProcess(subProcess) {
subProcesses.push(subProcess);
}
export function catchExceptions(kill = false) {
process
.on('unhandledRejection', (reason, p) => {
console.log(reason, 'Unhandled Rejection at Promise', p);
logError(reason + '. Unhandled Rejection at Promise:' + p);
})
.on('uncaughtException', (err) => {
console.log('Uncaught Exception thrown', kill, err);
// logError(err + ". Uncaught Exception thrown" + err.stack)
if (kill) {
process.exit(1);
}
});
}
export function killSubProcesses() {
console.log('Killing', subProcesses.length, 'child processes');
for (const i in subProcesses) {
if (!subProcesses[i]) continue;
subProcesses[i].kill();
subProcesses[i] = undefined;
}
try {
const execPromise = require('util').promisify(exec);
execPromise(
"kill -9 `ps aux | grep /usr/bin/node | grep -v grep | awk '{ print $2 }'` && kill -9 `ps aux | grep RuneInfinite | grep -v grep | awk '{ print $2 }'` && pkill -f Infinite"
).catch(() => {});
} catch (e2) {
console.log(e2);
}
}
export function cleanExit() {
killSubProcesses();
process.kill(0);
}
/* exit(1) will not force a restart on nodemon */
export default async function (msg) {
console.log(`Exiting now... ${msg}`);
process.exit(1);
}
// process.on('exit', cleanExit)
// process.on('SIGINT', cleanExit) // catch ctrl-c
// process.on('SIGTERM', cleanExit) // catch kill
/**
* Prompts the user with a question and resolves or rejects based on the response.
*
* @param {string} q - The question to prompt the user. If empty, the promise resolves immediately.
* @param {string} [defaultAnswer='y'] - The default answer to resolve with if no question is provided.
* @returns {Promise<string>} - Resolves with the answer or rejects if the answer is not 'y'.
*/
export async function awaitEnter(question = '', defaultAnswer = 'y') {
const readline = require('readline');
// Create a readline interface for user input
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> ' + question,
});
// Return a promise that resolves or rejects based on user input
return new Promise((resolve, reject) => {
rl.on('error', (err) => {
rl.close();
reject(err);
});
rl.prompt();
rl.on('line', async (answer: string) => {
console.log(`You answered: ${answer}`); // Log the user's answer
rl.close(); // Close the readline interface
if (answer.trim().toLowerCase() === 'n') {
reject(new Error('Aborting.'));
} else if (!question || answer.trim().toLowerCase() === 'y') {
resolve(answer); // Resolve if the answer is 'y' or 'Y'
} else {
reject(new Error('Aborting.'));
}
});
});
}