-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws-tools.js
174 lines (163 loc) · 4.37 KB
/
ws-tools.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
(function (moduleName) {
var crlf = '\r\n'
function getScriptArguments () {
if (!getScriptArguments.cache) {
var i, result = []
for (i = 0; i < WScript.Arguments.Count(); i++) {
result.push(WScript.Arguments(i))
}
getScriptArguments.cache = result
}
return getScriptArguments.cache
}
function getInfo (obj, isFirstRecursion) {
switch (typeof obj) {
case "undefined":
return "undefined"
case "number":
if (isNaN(obj)) {return "NaN"} else {return obj}
case "string":
if (isFirstRecursion) {
return obj
} else {
return "'" + obj + "'"
}
case "boolean":
if (obj) {return "true"} else {return "false"}
case "function":
if (isFirstRecursion) {
return obj.toString()
} else {
return "[object Function]"
}
case "object":
if (!obj) {
return "null"
} else if (Array.isArray(obj)) {
return "[ " + obj.map(function (item) {
return getInfo(item)
}).join(", ") + " ]"
} else if (obj instanceof ActiveXObject) {
return "[object ActiveXObject]"
} else if (Object.keys(obj).length) {
var keys = Object.keys(obj)
var objStr = "{ " + keys.map(function (key) {
return key + ": " + getInfo(obj[key])
}).join(", ") + " }"
return objStr
} else {
return "{ empty }"
}
default:
return "[unknown]"
}
}
function _getInfo (obj) {
return getInfo(obj, true)
}
function yamlize (obj, level) {
var indentPerLv = 4
if (!level) { level = 0 }
var indent = ' '.repeat(indentPerLv * level)
switch (typeof obj) {
case "undefined":
return "undefined"
case "function":
return "[object Function]"
case "number":
return obj
case "string":
if (!obj.includes('\n')) {
return obj
} else {
var splitter
if (obj.includes('\r\n')) { splitter = '\r\n' }
else { splitter = '\n' }
return "|" + crlf + obj.split(splitter).map(function (line) {
return indent + line
}).join(crlf)
}
case "boolean":
return obj + ""
case "object":
if (!obj) {
return "null"
} else if (obj instanceof ActiveXObject) {
return "[object ActiveXObject]"
} else if (!Object.keys(obj).length) {
return "{ empty }"
} else if (Array.isArray(obj)) {
return (level ? crlf : '') +
obj.map(function (item) {
return indent + "- " + yamlize(item, level + 1)
}).join(crlf)
} else {
var keys = Object.keys(obj)
return (level ? crlf : '') +
keys.map(function (key) {
return indent + key + ': ' + yamlize(obj[key], level + 1)
}).join(crlf)
}
default:
return "[unknown]"
}
}
function toArray (arrayLike) {
var result = [], i
for (i = 0; i < arrayLike.length; i++) {
result.push(arrayLike[i])
}
return result
}
function echo () {
var args = toArray(arguments)
var toShow = args.map(function (arg) {
return _getInfo(arg)
}).join(crlf)
WScript.Echo(toShow)
}
var popupParams = {
TYPE_OK: 0,
TYPE_OK_CANCEL: 1,
TYPE_ABORT_RETRY_IGNORE: 2,
TYPE_YES_NO_CANCEL: 3,
TYPE_YES_NO: 4,
TYPE_RETRY_CANCEL: 5,
TYPE_CANCEL_TRYAGAIN_CONTINUE: 6,
TYPE_ERROR: 16,
TYPE_QUEST: 32,
TYPE_WARNING: 48,
TYPE_INFO: 64,
CHOOSED_NONE: -1,
CHOOSED_OK: 1,
CHOOSED_CANCEL: 2,
CHOOSED_ABORT: 3,
CHOOSED_RETRY: 4,
CHOOSED_IGNORE: 5,
CHOOSED_YES: 6,
CHOOSED_NO: 7,
CHOOSED_TRYAGAIN: 10,
CHOOSED_CONTINUE: 11
}
function getShell () {
if (!getShell.instance) {
getShell.instance = WScript.CreateObject("WScript.Shell")
}
return getShell.instance
}
function popup (message, title, type, secsToWait) {
return getShell().Popup(message, secsToWait, title, type)
}
function info (message, title, secsToWait) {
return getShell().Popup(message, secsToWait, title, 64)
}
function error (message, title, secsToWait) {
return getShell().Popup(message, secsToWait, title, 16)
}
$modules[moduleName] = {
getScriptArguments: getScriptArguments,
getInfo: _getInfo,
echo: echo,
yamlize: function (obj) { return yamlize(obj) }
}
})("syshu.ws-tools")