-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·76 lines (64 loc) · 1.6 KB
/
index.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
import Configstore from "configstore"
import inquirer from "inquirer"
class ConfigurationStation {
constructor({ appName = "", config }) {
this.schema = config
this.appName = appName
const emptySchema = Object.fromEntries(
Object.keys(this.schema).map((key) => [key, undefined])
)
this.config = new Configstore(appName, emptySchema)
}
async ask(key, keyName = key) {
const existingValue = this.get(key)
const messageStart = existingValue ? "Update" : "Enter"
const options = {
default: existingValue,
message: `${messageStart} ${keyName}:`,
name: key,
type: this.translateType(this.schema[key])
}
const answer = await inquirer.prompt(options)
this.config.set(key, answer[key])
return answer[key]
}
async delete(key) {
this.config.delete(key)
}
async deleteAll() {
this.config.clear()
}
async askAll(schema = this.schema) {
for (const key of Object.keys(schema)) {
const keyName = schema[key].name || key
await this.ask(key, keyName)
}
}
get(key) {
return this.config.get(key)
}
getAll() {
return this.config.all
}
set(key, value) {
this.config.set(key, value)
}
translateType(type) {
const typeMap = {
boolean: "confirm",
number: "number",
password: "password",
string: "input"
}
return typeMap[type] || "input"
}
async valuesOrPrompts() {
for (const key of Object.keys(this.schema)) {
if (this.get(key) === undefined) {
await this.ask(key)
}
}
return this.getAll()
}
}
export default ConfigurationStation