-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
174 lines (161 loc) · 3.91 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
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
const chalk = require('chalk');
const inquirer = require("inquirer");
const deasync = require("deasync")
const getGlobal = () => {
if(typeof gloabal !== "undefined") return gloabal;
return window;
}
const callbackify = (promiseFactory) => {
return (...args) => {
const cb = args[args.length-1];
args.pop();
return promiseFactory(...args)
.then( result => cb(undefined,result) )
.catch( e => cb(e,undefined) )
}
}
let display_development_output = false;
const colors = {
}
const defaultColors = {
log: (...input) => input.join(""),
info: (...input) => chalk.blue(...input),
form: (...input) => chalk.magenta(...input),
success: (...input) => chalk.green(...input),
warning: (...input) => chalk.red(...input),
warn: (...input) => chalk.red(...input),
error: (...input) => chalk.white.bgRed(...input),
err: (...input) => chalk.white.bgRed(...input),
}
const apply_color = (colorId, ...input) => {
const brush = colors[colorId] || console.log;
return brush(...input);
}
const output = (colorId, ...input) => {
console.log(apply_color(colorId, ...input));
}
const devOutput = (...args) => {
if (display_development_output) output(...args);
}
const setColor = (colorId, brush) => {
colors[colorId] = brush;
output[colorId] = (...input) => output(colorId, ...input)
devOutput[colorId] = (...input) => devOutput(colorId, ...input)
}
for (const colorId in defaultColors) {
if (defaultColors.hasOwnProperty(colorId)) {
setColor(colorId, defaultColors[colorId])
}
}
const input = {
text: async (questionText, defaultValue, validator) => {
const question = {
type: "input",
name: "inp",
message: questionText
};
if (["string", "number", "boolean", "array", "function"].includes(defaultValue))
question.default = defaultValue;
if (typeof validator === "function") {
question.validate = validator;
}
try {
const response = await inquirer.prompt([question]);
return response.inp;
}
catch (e) {
output.error("unable to querry", e)
}
},
list: async (questionText, choices) => {
const question = {
type: "list",
name: "inp",
choices,
message: questionText
};
try {
const response = await inquirer.prompt([question]);
return response.inp;
}
catch (e) {
output.error("unable to querry", e)
}
},
checkbox: async (questionText, choices) => {
const question = {
type: "checkbox",
name: "inp",
choices,
message: questionText
};
try {
const response = await inquirer.prompt([question]);
return response.inp;
}
catch (e) {
output.error("unable to querry", e)
}
},
confirm: async (questionText) => {
const question = {
type: "confirm",
name: "inp",
message: questionText
};
try {
const response = await inquirer.prompt([question]);
return response.inp;
}
catch (e) {
output.error("unable to querry", e)
}
},
pass: async (questionText, validator) => {
const question = {
type: "password",
name: "inp",
message: questionText
}
if (typeof validator === "function") {
question.validate = validator;
}
try {
const response = await inquirer.prompt([question]);
return response.inp;
}
catch (e) {
output.error("unable to querry", e)
}
}
}
const syncInput = {
text: (...args) => {
return deasync(
callbackify(input.text)
)(...args);
}
}
for (const inputType in input) {
if (input.hasOwnProperty(inputType)) {
syncInput[inputType] = (...args) => {
return deasync(
callbackify(input[inputType])
)(...args);
}
}
}
module.exports = {
setDevelopmentMode: (active) => {
display_development_output = active;
return active;
},
output,
out:output,
devOutput,
dev:devOutput,
apply_color,
setColor,
input,
syncInput,
}