This repository has been archived by the owner on Feb 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.js
288 lines (252 loc) · 6 KB
/
setup.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
const fs = require('fs');
const ask = require('./modules/ask');
const setupFile = require('./modules/setupFile');
const password = require('./modules/password');
const consoleColor = require('./modules/consoleColor');
const path = {
env: `${setupFile.resource.dir}/${setupFile.resource.file_env}`,
private: `${setupFile.resource.dir}/${setupFile.resource.file_private}`,
};
/**
* ask text
*
* @param {String} message
* @param {Boolean} required
* @param {String} errorMessage
* @return {Promise}
*/
function askText(message, required=false, errorMessage=null)
{
return new Promise(function(resolve) {
ask(`${message}${required ? ' (required)' : ''}: `, function(str) {
if (!str && required)
{
if (errorMessage) printConsole(true, errorMessage);
askText(message, required, errorMessage).then(resolve);
}
else
{
resolve(str);
}
});
});
}
/**
* ask Yes & No
*
* @param {String} message
* @param {Boolean} active
*/
function askBoolean(message='Question?', active=null)
{
return new Promise(function(resolve) {
ask(`${message} (${active === true ? 'Y' : 'y'}/${active === false ? 'N' : 'n'}) : `, function(str) {
if (str)
{
str = str.toLowerCase();
}
else
{
str = (active === null) ? null : (active ? 'y' : 'n');
}
switch(str)
{
case 'y':
resolve(true);
break;
case 'n':
resolve(false);
break;
default:
askBoolean(message, active).then(resolve);
break;
}
})
});
}
/**
* print console
*
* @param {Boolean} err
* @param {String} message
*/
function printConsole(err, message)
{
if (err)
{
console.error(message);
}
else
{
console.log(consoleColor.green, message, '\x1b[0m', consoleColor.reset);
}
}
/**
* exit setup
*/
function exit()
{
console.log(consoleColor.green, 'exit setup', consoleColor.reset);
process.exit();
}
/**
* check config file
*
* @return {Boolean}
*/
function checkConfigFile()
{
return (
fs.existsSync(path.env) &&
fs.existsSync(path.private)
);
}
/**
* write setup file
*
* @param {String} type
* @param {Object} data
* @return {Promise}
*/
async function writeConfigFile(type='env', data)
{
try
{
let str = JSON.stringify(data, null, 2);
let result = await setupFile.create(type, str);
printConsole(!!result.error, result.message);
}
catch(e)
{
printConsole(true, `Error make "${path[type]}" file`);
}
return;
}
/**
* setup
*
* @return {Promise}
*/
async function setup()
{
if (!fs.existsSync(setupFile.resource.dir))
{
fs.mkdirSync(setupFile.resource.dir);
}
if (checkConfigFile())
{
// if exist `.env.json` file
let reinstall = await askBoolean(`Exist setup file. Reinstall?`, false);
if (reinstall)
{
// delete files
try { fs.unlinkSync(path.env); } catch(e) {}
try { fs.unlinkSync(path.private); } catch(e) {}
// play setup
setup().then();
}
else
{
exit();
}
}
else
{
// if not found config
let resultCreate = {};
let config = {};
// make config files
resultCreate.env = await setupFile.create('env', null);
resultCreate.private = await setupFile.create('private', null);
// print console
printConsole(!!resultCreate.env.error, resultCreate.env.message);
printConsole(!!resultCreate.private.error, resultCreate.private.message);
// get config
config.env = (!resultCreate.env.error) ? require(`./${path.env}`) : null;
config.private = (!resultCreate.private.error) ? require(`./${path.private}`) : null;
if (!(config.env && config.private)) exit();
// set application
config.env.APPLICATION = password.create(String(Date.now()), 5);
// set mainnet password
let passwordMainnet = await askText('Set MAINNET password. [DO NOT INPUT QTUM CORE PASSWORD]', true);
config.private.HASH_MAINNET = password.create(passwordMainnet, 10);
// set use testnet
config.env.USE_TESTNET = await askBoolean('Do you want to use "TESTNET"?', false);
// set testnet password
if (config.env.USE_TESTNET)
{
let passwordTestnet = await askText('Set TESTNET password. [DO NOT INPUT QTUM CORE PASSWORD]', false);
config.private.HASH_TESTNET = password.create(passwordTestnet, 10);
}
// TODO: 나중에 자동으로 qtum-cli 찾아주는 기능 추가가 필요함.
config.private.CORE_ADDRESS = await askText('Qtum core address? ex:`/usr/local/bin/`', true);
// write setup files
writeConfigFile('env', config.env).then();
writeConfigFile('private', config.private).then();
exit();
}
}
/**
* change password
*
* @return {Promise}
*/
async function changePassword()
{
let nextConfig = setupFile.get('all');
let pw_testnet = '';
let pw_mainnet = '';
let str = null;
let result = null;
switch(process.argv[3])
{
case '-testnet':
pw_testnet = await askText('Change input TESTNET password. [DO NOT INPUT QTUM CORE PASSWORD]', false);
nextConfig.env.USE_TESTNET = true;
nextConfig.private.HASH_TESTNET = password.create(pw_testnet, 10);
// update env file
str = JSON.stringify(nextConfig.env, null, 2);
result = await setupFile.create('env', str);
printConsole(!!result.error, result.message);
break;
case '-mainnet':
default:
pw_mainnet = await askText('Change input MAINNET password. [DO NOT INPUT QTUM CORE PASSWORD]', true);
nextConfig.private.HASH_MAINNET = password.create(pw_mainnet, 10);
break;
}
// update private file
str = JSON.stringify(nextConfig.private, null, 2);
result = await setupFile.create('private', str);
printConsole(!!result.error, result.message);
// exit
exit();
}
/**
* remake application key
*/
async function remakeApplication()
{
let nextEnv = setupFile.get('env');
nextEnv.APPLICATION = password.create(String(Date.now()), 5);
// update env
let str = JSON.stringify(nextEnv, null, 2);
let result = await setupFile.create('env', str);
// print console
printConsole(!!result.error, result.message);
// exit
exit();
}
// action
switch(process.argv[2])
{
case '-change-password':
changePassword().then();
break;
case '-remake-application':
remakeApplication().then();
break;
default:
setup().then();
break;
}