-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathcommand.js
More file actions
60 lines (52 loc) · 1.19 KB
/
command.js
File metadata and controls
60 lines (52 loc) · 1.19 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
const { exec } = require('child_process');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
function executeCommand(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(`Error: ${error.message}`);
return;
}
if (stderr) {
reject(`Error: ${stderr}`);
return;
}
resolve(stdout);
});
});
}
async function runCommandLine() {
try {
console.log('Command Line Interface Started');
console.log('Type "exit" to quit');
while (true) {
const command = await new Promise(resolve => {
rl.question('> ', (answer) => {
resolve(answer);
});
});
if (command.toLowerCase() === 'exit') {
console.log('Exiting...');
break;
}
try {
const result = await executeCommand(command);
console.log(result);
} catch (error) {
console.error(error);
}
}
} finally {
rl.close();
}
}
if (require.main === module) {
runCommandLine();
}
module.exports = {
executeCommand
};