-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconfig_helper.js
79 lines (65 loc) · 2.19 KB
/
config_helper.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
const fs = require("fs");
const path = require("path");
const readline = require("readline");
const exampleConfig = require("./config/example.config.json");
exports.buildDataPath = () =>
{
const config = require("./config/config.json");
if (config.dataPath.startsWith(".") === true)
config.dataPath = path.resolve(__dirname, config.dataPath);
return config;
};
exports.hasConfig = () => fs.existsSync("./config/config.json");
exports.askConfigQuestions = () =>
{
const config = Object.assign({}, exampleConfig);
return _promisifiedQuestion("Input bot's login token: ", (answer) =>
{
config.loginToken = answer;
})
.then(() => _promisifiedQuestion("Input root path to bot data (Enter for default): ", (answer) =>
{
if (answer === "")
return Promise.resolve();
if (fs.existsSync(answer) === false)
return Promise.reject("Path does not exist: ");
config.dataPath = answer;
}))
.then(() => _promisifiedQuestion("Input Dom5 exe path: ", (answer) =>
{
if (fs.existsSync(answer) === false)
return Promise.reject("Path does not exist.");
config.pathToDom5Exe = answer;
}))
.then(() => _promisifiedQuestion("Input Dom6 exe path: ", (answer) =>
{
if (fs.existsSync(answer) === false)
return Promise.reject("Path does not exist.");
config.pathToDom6Exe = answer;
}))
.then(() => fs.writeFileSync("./config/config.json", JSON.stringify(config, null, 2)));
};
function _promisifiedQuestion(question, onAnswerHandler)
{
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
return new Promise((resolve) =>
{
(function _askQuestion()
{
rl.question(question, (answer) =>
{
Promise.resolve(onAnswerHandler(answer))
.then(() => resolve())
.catch((err) =>
{
const log = require("./logger.js");
log.error(log.getLeanLevel(), `CONFIG QUESTION ERROR`, err);
_askQuestion();
});
});
})();
});
}