This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
295 lines (228 loc) · 7.18 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
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
'use strict';
var assert = require('assert'),
child_process = require('child_process'),
fs = require('fs'),
url = require('url');
function _argsArray(args) {
return Array.prototype.slice.call(args, 0);
}
function safeCall(optionalThis, func, errorReturnValue) {
safeCall.error = null;
if (typeof optionalThis === 'function') {
errorReturnValue = func;
func = optionalThis;
optionalThis = null;
}
if (typeof errorReturnValue === 'undefined') {
errorReturnValue = null;
}
try {
return func.call(optionalThis);
} catch (e) {
safeCall.error = e;
return errorReturnValue;
}
}
function jsonParse() {
var args = _argsArray(arguments);
return safeCall(function () { return JSON.parse.apply(JSON, args); }, null);
}
function jsonStringify() {
var args = _argsArray(arguments);
return safeCall(function () { return JSON.stringify.apply(JSON, args); }, null);
}
function openSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.openSync.apply(fs, args); }, -1);
}
function closeSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.closeSync.apply(fs, args); }, 0);
}
function readFileSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.readFileSync.apply(fs, args); }, null);
}
function writeFileSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.writeFileSync.apply(fs, args); }) !== null;
}
function statSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.statSync.apply(fs, args); }, null);
}
function lstatSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.lstatSync.apply(fs, args); }, null);
}
function readdirSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.readdirSync.apply(fs, args); }, null);
}
// afaik, this never throws
function existsSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.existsSync.apply(fs, args); }, false);
}
function mkdirSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.mkdirSync.apply(fs, args); }) !== null;
}
function chownSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.chownSync.apply(fs, args); }) !== null;
}
function unlinkSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.unlinkSync.apply(fs, args); }) !== null;
}
function rmdirSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.rmdirSync.apply(fs, args); }) !== null;
}
function chmodSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.chmodSync.apply(fs, args); }) !== null;
}
function readlinkSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.readlinkSync.apply(fs, args); }, null);
}
function realpathSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.realpathSync.apply(fs, args); }, null);
}
function renameSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.renameSync.apply(fs, args); }) !== null;
}
function readSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.readSync.apply(fs, args); }) !== null;
}
function appendFileSync() {
var args = _argsArray(arguments);
return safeCall(function () { return fs.appendFileSync.apply(fs, args); }) !== null;
}
function urlParse() {
var args = _argsArray(arguments);
return safeCall(function () { return url.parse.apply(url, args); }, null);
}
function safeRequire() {
var args = _argsArray(arguments);
return safeCall(function () { return require.apply(null, args); }, null);
}
function execSync() {
var args = _argsArray(arguments);
return safeCall(function () { return child_process.execSync.apply(child_process, args); }, null);
}
function spawnSync() {
var args = _argsArray(arguments);
return safeCall(function () { return child_process.spawnSync.apply(child_process, args); }, null);
}
// http://stackoverflow.com/questions/6491463
// currently, '.' is assumed to be the separator
function query(o, s, defaultValue) {
if (!s) return o;
assert(typeof s === 'string' || Array.isArray(s));
let a;
if (Array.isArray(s)) { // already split up
a = s;
} else {
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
a = s.split('.'); // always returns an array
}
for (var i = 0; i < a.length; i++) {
var n = a[i];
if (!o || typeof o !== 'object' || !(n in o)) return defaultValue;
o = o[n];
}
return o;
}
// TODO: support array format like [0].some.value
function set(o, s, value) {
if (!s) return o;
assert(typeof s === 'string' || Array.isArray(s));
if (!o || typeof o !== 'object') o = { };
let a;
if (Array.isArray(s)) {
a = s;
} else {
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
a = s.split('.'); // always returns an array
}
var io = o;
for (var i = 0; i < a.length - 1; i++) {
var n = a[i];
if (!(n in io) || !io[n] || typeof io[n] !== 'object') {
io[n] = { };
}
io = io[n];
}
io[a[a.length - 1]] = value;
return o;
}
function unset(o, s) {
if (!s) return o;
assert(typeof s === 'string' || Array.isArray(s));
if (!o || typeof o !== 'object') return o;
let a;
if (Array.isArray(s)) {
a = s;
} else {
s = s.replace(/\[(\w+)\]/g, '.$1'); // convert indexes to properties
s = s.replace(/^\./, ''); // strip a leading dot
a = s.split('.'); // always returns an array
}
var io = o;
for (var i = 0; i < a.length - 1; i++) {
var n = a[i];
if (!(n in io)) return o;
if (!io[n] || typeof io[n] !== 'object') {
delete io[n];
return o;
}
io = io[n];
}
delete io[a[a.length - 1]];
return o;
}
safeCall.safeCall = safeCall; // compat
safeCall.JSON = {
parse: jsonParse,
stringify: jsonStringify
};
safeCall.fs = {
openSync: openSync,
closeSync: closeSync,
readSync: readSync,
readFileSync: readFileSync,
writeFileSync: writeFileSync,
statSync: statSync,
lstatSync: lstatSync,
existsSync: existsSync,
mkdirSync: mkdirSync,
rmdirSync: rmdirSync,
unlinkSync: unlinkSync,
renameSync: renameSync,
readdirSync: readdirSync,
chmodSync: chmodSync,
readlinkSync: readlinkSync,
realpathSync: realpathSync,
appendFileSync: appendFileSync,
chownSync: chownSync
};
safeCall.child_process = {
execSync: execSync,
spawnSync: spawnSync
};
safeCall.require = safeRequire;
safeCall.url = {
parse: urlParse
};
safeCall.query = query;
safeCall.set = set;
safeCall.unset = unset;
exports = module.exports = safeCall;