1
|
|
|
/** |
2
|
|
|
* @file Module 'configure' |
3
|
|
|
* @author woshilapin <[email protected]> |
4
|
|
|
* @version 0.3.0 |
5
|
|
|
*/ |
6
|
|
|
/** |
7
|
|
|
* Manage global configuration for Codingame's connector |
8
|
|
|
* @module configure |
9
|
|
|
*/ |
10
|
|
|
import fs from 'fs'; |
11
|
|
|
import readline from 'readline'; |
12
|
|
|
import subprocess from 'child_process'; |
13
|
|
|
|
14
|
|
|
let parameters = {}; |
15
|
|
|
|
16
|
|
|
const rl = readline.createInterface({ |
17
|
|
|
"input": process.stdin, |
18
|
|
|
"output": process.stdout |
19
|
|
|
}); |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Load the configuration file |
24
|
|
|
* |
25
|
|
|
* @name load |
26
|
|
|
* @function |
27
|
|
|
* @param {string} path Path of the configuration file |
28
|
|
|
* @param {Object} [options] Additionnal parameters which will replace parameters from configuration file |
|
|
|
|
29
|
|
|
* @returns {Promise<Object>} Configuration parameters |
30
|
|
|
* @memberof module:configure |
31
|
|
|
* @instance |
32
|
|
|
*/ |
33
|
|
|
let load = function load(path, options) { |
|
|
|
|
34
|
|
|
return new Promise(function(resolve, reject) { |
35
|
|
|
let resolvefromerror = function resolvefromerror(error) { |
36
|
|
|
if (options !== undefined && typeof options === `object`) { |
37
|
|
|
resolve(options); |
38
|
|
|
} else { |
39
|
|
|
reject(error); |
40
|
|
|
} |
41
|
|
|
}; |
42
|
|
|
try { |
43
|
|
|
fs.readFile(path, `utf8`, function(error, file) { |
44
|
|
|
if (error) { |
45
|
|
|
resolvefromerror(error); |
46
|
|
|
} else { |
47
|
|
|
parameters = JSON.parse(file); |
48
|
|
|
Object.assign(parameters, options); |
49
|
|
|
resolve(parameters); |
50
|
|
|
} |
51
|
|
|
}); |
52
|
|
|
} catch (error) { |
53
|
|
|
resolvefromerror(error); |
54
|
|
|
} |
55
|
|
|
}); |
56
|
|
|
}; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the value from configuration |
60
|
|
|
* |
61
|
|
|
* @name get |
62
|
|
|
* @function |
63
|
|
|
* @param {string} name Name of the parameter |
64
|
|
|
* @param {string} [option] 'shell' if value may be executed as shell command, 'file' if value is a path and content of file should be returned |
|
|
|
|
65
|
|
|
* @param {string} [question] If present and value not found, will be shown to the user to ask for the value on 'stdin' |
|
|
|
|
66
|
|
|
* @returns {Promise<string|Array|Object>} The value of the parameter |
67
|
|
|
* @memberof module:configure |
68
|
|
|
* @instance |
69
|
|
|
*/ |
70
|
|
|
let get = function get(name, option, question) { |
|
|
|
|
71
|
|
|
return new Promise(function(resolve, reject) { |
72
|
|
|
if (!name || typeof name !== `string`) { |
73
|
|
|
reject(new Error(`'configure.get()' takes at least one string parameter.`)); |
74
|
|
|
} |
75
|
|
|
let property = parameters[name]; |
76
|
|
|
if (property !== undefined && option !== undefined && option === `shell` && Array.isArray(property)) { |
77
|
|
|
subprocess.exec(property.join(` `), function(error, stdout, stderr) { |
78
|
|
|
if (error) { |
79
|
|
|
console.error(stderr); |
80
|
|
|
reject(error); |
81
|
|
|
} else { |
82
|
|
|
let result = stdout.trim(); |
83
|
|
|
parameters[name] = result; |
84
|
|
|
resolve(result); |
85
|
|
|
} |
86
|
|
|
}); |
87
|
|
|
} else if (property !== undefined && option !== undefined && option === `file`) { |
88
|
|
|
fs.readFile(property, `utf8`, function(error, file) { |
89
|
|
|
if (error) { |
90
|
|
|
reject(error); |
91
|
|
|
} else { |
92
|
|
|
resolve({ |
93
|
|
|
"path": property, |
94
|
|
|
"content": file |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
}); |
98
|
|
|
} else if (property !== undefined) { |
99
|
|
|
resolve(property); |
100
|
|
|
} else { |
101
|
|
|
if (question !== undefined && typeof question === `string`) { |
102
|
|
|
rl.question(question, (result) => { |
103
|
|
|
parameters[name] = result; |
104
|
|
|
resolve(result); |
105
|
|
|
}); |
106
|
|
|
} else { |
107
|
|
|
reject(new Error(`'configure.get()' second parameter must be a string.`)); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
}); |
111
|
|
|
}; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Ask configure module to delete a parameter |
115
|
|
|
* |
116
|
|
|
* @name forget |
117
|
|
|
* @function |
118
|
|
|
* @param {string} name Name of the parameter |
119
|
|
|
* @memberof module:configure |
120
|
|
|
* @instance |
121
|
|
|
*/ |
122
|
|
|
let forget = function forget(name) { |
|
|
|
|
123
|
|
|
if (!name || typeof name !== `string`) { |
124
|
|
|
throw new Error(`'configure.forget()' function takes one string parameter.`); |
125
|
|
|
} else { |
126
|
|
|
delete parameters[name]; |
127
|
|
|
} |
128
|
|
|
}; |
129
|
|
|
|
130
|
|
|
export default { |
131
|
|
|
"load": load, |
132
|
|
|
"get": get, |
133
|
|
|
"forget": forget |
134
|
|
|
}; |
135
|
|
|
|