-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.js
115 lines (111 loc) · 2.88 KB
/
config.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
/**
* config.js
*
* Utility module for configuring ContentReviewLog.
*/
/**
* Importing modules.
*/
import fs from 'fs';
import readline from 'readline';
/**
* Constants.
*/
const QUESTIONS = [
['username', 'Fandom username'],
['password', 'Fandom password'],
['interval', 'Update interval (in seconds)'],
['url', 'Webhook URL'],
['wiki', 'Fandom wiki URL']
], WIKI_REGEX = /^(?:https?:\/\/)?([a-z0-9-.]+)\.(wikia\.(?:com|org)|fandom\.com)(?:\/([a-z-]+))?\/?$/,
WEBHOOK_REGEX = /^https?:\/\/(?:canary\.|ptb\.)?discord(?:app)?\.com\/api\/webhooks\/(\d+)\/([a-zA-Z0-9-_]+)$/;
/**
* Class used for configuration of ContentReviewLog.
*/
class Configuration {
/**
* Class constructor.
* @constructor
*/
constructor() {
this._rl = readline.createInterface({
historySize: 0,
input: process.stdin,
output: process.stderr
});
this._next();
}
/**
* Asks the next question.
* @private
*/
_next() {
this._question = QUESTIONS.shift();
this._rl.question(`${this._question[1]}: `, this._callback.bind(this));
}
/**
* Callback after answering the question.
* @param {String} answer Answer to the question
* @private
*/
_callback(answer) {
this[`_${this._question[0]}`] = answer;
if (QUESTIONS.length) {
this._next();
} else {
this._finish();
}
}
/**
* After collecting question results.
* @private
*/
_finish() {
const config = {
interval: Number(this._interval) * 1000,
password: this._password,
username: this._username
};
const res = WEBHOOK_REGEX.exec(this._url),
res2 = WIKI_REGEX.exec(this._wiki);
if (isNaN(config.interval)) {
console.error('Invalid interval!');
return;
}
if (res) {
config.id = res[1];
config.token = res[2];
} else {
console.error('Webhook URL invalid!');
return;
}
if (res2) {
config.wiki = res2[1];
config.domain = res2[2];
config.lang = res2[3];
} else {
console.error('Wiki URL invalid!');
return;
}
fs.writeFile(
'config.json',
JSON.stringify(config, null, ' '),
this._write.bind(this)
);
}
/**
* After writing configuration to file.
* @param {Error} error Error that occurred
* @private
*/
_write(error) {
if (error) {
console.error('An error occurred while writing to file:', error);
} else {
console.log('Configuration successful, run `npm start`.');
process.exit();
}
}
}
const instance = new Configuration();
export default instance;