This repository has been archived by the owner on Sep 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.js
140 lines (124 loc) · 3.07 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
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
const path = require('path');
const yaml = require('write-yaml');
const co = require('co');
const { prompt } = require('inquirer');
const { APPLICATION } = require('./properties');
let username = [{
type: 'input',
name: 'username',
message: 'username: ',
validate: value => {
if (!value) {
return 'username required';
}
return true;
}
}];
let hostname = [{
type: 'input',
name: 'hostname',
message: 'hostname (with http:// || https://): ',
validate: value => {
let reg = /^(http|https):\/\/.*$/;
if (!value) {
return 'hostname required';
} else {
if(!reg.test(value)) {
return 'protocol required';
}
}
return true;
}
}];
let configStruct = {
username: null,
hostname: APPLICATION.TESTNET
};
/**
* Entry point for the strato config command
* @returns {Promise}
*/
function saveConfig() {
return new Promise((resolve, reject) => {
co(function* () {
console.log('Please enter the configuration information:');
yield prompt(username)
.then((data) => {
configStruct.username = data.username;
console.log('Press [ ENTER ] for Default Host');
console.log('Press [ SPACE ] for Custom Host');
})
.catch((err) => {
return reject(err);
});
let _key;
yield captureKeys()
.then((key) => {
_key = key;
});
if (_key === 'SPACE') {
yield prompt(hostname)
.then(data => {
let hostname = data.hostname;
if(!hostname.startsWith('http://') && !hostname.startsWith('https://')) {
configStruct.hostname = 'http://' + hostname;
} else {
configStruct.hostname = hostname;
}
})
.catch((err) => {
return reject(err);
});
}
yield generateYAML()
.then(() => {
return resolve();
})
.catch((err) => {
return reject(err);
});
});
});
}
/**
* Capture Keys
* @returns {Promise}
*/
function captureKeys() {
return new Promise((resolve, reject) => {
let stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
stdin.addListener('data', (key) => {
if (key === '\u000D') {
stdin.pause();
stdin.removeAllListeners('data');
resolve('ENTER');
} else if (key === '\u0020') {
stdin.pause();
stdin.removeAllListeners('data');
resolve('SPACE');
} else if (key === '\u0003') {
process.exit();
}
});
});
}
/**
* Generate config.yaml inside User's home directory /strato
* @returns {Promise}
*/
function generateYAML() {
return new Promise((resolve, reject) => {
const target = path.join(APPLICATION.HOME_PATH, APPLICATION.CONFIG_FOLDER, APPLICATION.CONFIG_FILE);
yaml(target, configStruct, err => {
if (err) {
return reject(err);
}
console.log('Configuration file has been saved');
resolve();
});
});
}
module.exports.saveConfig = saveConfig;