-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.js
106 lines (86 loc) · 2.42 KB
/
util.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
import chalk from "chalk"
// Chalk shorthands
const red = chalk.redBright
const green = chalk.greenBright
const yellow = chalk.yellowBright
const blue = chalk.blueBright
const magenta = chalk.magentaBright
const cyan = chalk.cyanBright
const white = chalk.whiteBright
const grey = chalk.grey
function colorizeCommand(name, data) {
let colorized = ""
// Command name
colorized += `${cyan(name)}`
// Parameters
if (data.hasOwnProperty("parameters")) {
for (let parameter of data.parameters) {
colorized += " "
colorized += `${white(`<${parameter}>`)}`
}
}
return colorized
}
function colorizeOperation(name, data) {
let colorized = ""
// Operation type
if (typeof data.returns !== "object") {
colorized += `${blue(data.returns)}`
} else {
let type = data.returns.type
colorized += type.includes("[]") ? `${green(type.replace("[]", "")) + white("[]")}` : `${green(type)}`
if (data.returns.hasOwnProperty("required")) {
colorized += `${magenta(data.returns.required ? "" : "?")}`
}
}
// Operation name
colorized += " "
colorized += yellow(name)
// Operation parameters
colorized += "("
if (!data.hasOwnProperty("parameters")) {
colorized += ")"
return colorized
}
let index = 0
for (let name in data.parameters) {
let parameter = data.parameters[name]
// Parameter type & name
if (parameter.required) {
colorized += `${red(parameter.type)} ${white(name)}`
} else {
colorized += `${red(parameter.type) + magenta("?")} ${white(name)}`
if (parameter.hasOwnProperty("default")) {
colorized += " = "
colorized += typeof parameter.default === "object" ? white("[]") : cyan(parameter.default)
}
}
if (index == Object.keys(data.parameters).length - 1) {
colorized += ")"
} else {
colorized += ", "
}
index++
}
return colorized
}
function setTerminalTitle(title) {
if (process.platform == 'win32') {
process.title = title;
} else {
process.stdout.write('\x1b]2;' + title + '\x1b\x5c');
}
}
export default {
setTerminalTitle,
colorizeCommand,
colorizeOperation,
red,
green,
yellow,
blue,
magenta,
cyan,
white,
grey
}