-
Notifications
You must be signed in to change notification settings - Fork 0
/
wshcjs.js
207 lines (162 loc) · 5.09 KB
/
wshcjs.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
var global,
console,
require;
global = (function () {
'use strict';
return {
NaN: NaN,
'Infinity': Infinity,
'undefined': undefined,
'eval': eval,
parseInt: parseInt,
parseFloat: parseFloat,
isNaN: isNaN,
isFinite: isFinite,
decodeURI: decodeURI,
decodeURIComponent: decodeURIComponent,
encodeURI: encodeURI,
encodeURIComponent: encodeURIComponent,
Object: Object,
Function: Function,
Array: Array,
String: String,
Boolean: Boolean,
Number: Number,
Date: Date,
RegExp: RegExp,
Error: Error,
EvalError: EvalError,
RangeError: RangeError,
SyntaxError: SyntaxError,
TypeError: TypeError,
URIError: URIError,
Math: Math
};
}());
console = (function () {
'use strict';
function Console() {
this._logLevel = 1;
}
Console.prototype._log = function (type, args) {
var i;
if (type < this._logLevel) {
return;
}
for (i = 0; i < args.length; i += 1) {
if ('object' === typeof args[i]) {
this._log(type, args[i]);
} else {
WScript.Stdout.WriteLine(String(args[i]));
}
}
};
Console.prototype.log = function () {
this._log(Console.TYPE_LOG, arguments);
};
Console.prototype.trace = function () {
this._log(Console.TYPE_TRACE, arguments);
};
Console.prototype.info = function () {
this._log(Console.TYPE_INFO, arguments);
};
Console.prototype.warn = function () {
this._log(Console.TYPE_WARN, arguments);
};
Console.prototype.error = function () {
this._log(Console.TYPE_ERROR, arguments);
};
Console.TYPE_TRACE = 1;
Console.TYPE_INFO = 2;
Console.TYPE_WARN = 3;
Console.TYPE_ERROR = 4;
Console.TYPE_LOG = 5;
return new Console(1);
}());
require = (function () {
'use strict';
var _shell = new ActiveXObject('WScript.Shell'),
_fso = new ActiveXObject('Scripting.FileSystemObject'),
_dirname = _shell.CurrentDirectory,
_require;
function read(filePath) {
var fd = _fso.OpenTextFile(filePath, 1),
content = fd.AtEndOfStream ? '' : fd.ReadAll();
return content;
}
function bind(fn, context) {
return function () {
return fn.apply(context, arguments);
};
}
function ModuleScope() {
this._varNames = [];
this._varValues = [];
}
ModuleScope.prototype.add = function (name, value) {
this._varNames.push(name);
this._varValues.push(value);
return this;
};
ModuleScope.prototype.getVarNames = function () {
return this._varNames;
};
ModuleScope.prototype.getVarValues = function () {
return this._varValues;
};
function Require(dirname) {
this._dirname = dirname;
}
Require.prototype.require = function (moduleName) {
var modulePath = this.resolve(moduleName),
module,
moduleContent,
moduleScope,
moduleInitializer,
moduleRequire,
__dirname,
__filename;
console.trace('$> requiring ' + modulePath);
module = this.cache[modulePath];
if (module !== undefined) {
console.trace('$> module cache found');
return module.exports;
}
moduleContent = read(modulePath);
module = {
loaded: false,
exports: {}
};
moduleRequire = new Require(_fso.GetParentFolderName(modulePath));
__dirname = _fso.GetParentFolderName(modulePath);
__filename = modulePath;
this.cache[modulePath] = module;
moduleScope = new ModuleScope();
moduleScope
.add('global', global)
.add('console', console)
.add('require', bind(moduleRequire.require, moduleRequire))
.add('module', module)
.add('exports', module.exports)
.add('__dirname', __dirname)
.add('__filename', __filename);
moduleInitializer = Function(
moduleScope.getVarNames().toString(),
'{' + moduleContent + '}'
);
try {
moduleInitializer.apply(global, moduleScope.getVarValues());
module.loaded = true;
} catch (e) {
delete this.cache[modulePath];
throw e;
}
return module.exports;
};
Require.prototype.resolve = function (moduleName) {
return _fso.GetAbsolutePathName(this._dirname + moduleName);
};
Require.prototype.cache = {};
_require = new Require(_dirname);
return bind(_require.require, _require);
}());