-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cjs
More file actions
36 lines (32 loc) · 1.18 KB
/
config.cjs
File metadata and controls
36 lines (32 loc) · 1.18 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
const fs = require('fs');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
const configFile = fs.readFileSync('configs.json');
const configData = JSON.parse(configFile);
let coicheString = "Node.js - Please select a server:\n";
for (let i in configData) {
coicheString = coicheString.concat(`[${i}]: ${configData[i].name}\n`);
}
coicheString = coicheString.concat(`Your Choice: `);
function selectConfig() {
//return configData[4];
return new Promise((resolve, reject) => {
readline.question(coicheString, choice => {
const selectedIndex = parseInt(choice);
if (isNaN(selectedIndex) || selectedIndex < 0 || selectedIndex >= configData.length) {
console.log(`Input not valid, insert a number between 0 and ${configData.length - 1}. Try again :)`);
// Chiamata ricorsiva, la Promise esterna attenderà questa nuova Promise
selectConfig().then(resolve).catch(reject);
} else {
console.log(`Ok, we'll use '${configData[selectedIndex].name}' as server!`);
readline.close();
resolve(configData[selectedIndex]);
}
});
});
}
module.exports = {
selectConfig
};