util.js
9.5 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
var assert = require("assert");
var path = require("path");
var fs = require("graceful-fs");
var Q = require("q");
var createHash = require("crypto").createHash;
var mkdirp = require("mkdirp");
var iconv = require("iconv-lite");
var Ap = Array.prototype;
var slice = Ap.slice;
var join = Ap.join;
// The graceful-fs module attempts to limit the total number of open files
// by queueing fs operations, but it doesn't know about all open files, so
// we set the limit somewhat lower than the default to provide a healthy
// buffer against EMFILE (too many open files) errors.
fs.MAX_OPEN = 512;
function makePromise(callback, context) {
var deferred = Q.defer();
function finish(err, result) {
if (err) {
deferred.reject(err);
} else {
deferred.resolve(result);
}
}
process.nextTick(function() {
try {
callback.call(context || null, finish);
} catch (err) {
finish(err);
}
});
return deferred.promise;
}
exports.makePromise = makePromise;
exports.cachedMethod = function(fn, keyFn) {
var p = require("private").makeAccessor();
function wrapper() {
var priv = p(this);
var cache = priv.cache || (priv.cache = {});
var args = arguments;
var key = keyFn
? keyFn.apply(this, args)
: join.call(args, "\0");
return cache.hasOwnProperty(key)
? cache[key]
: cache[key] = fn.apply(this, args);
}
wrapper.originalFn = fn;
return wrapper;
};
function readFileP(file, charset) {
return makePromise(charset ? function(callback) {
return fs.readFile(file, function(err, data) {
if (err) {
callback(err);
} else {
callback(null, iconv.decode(data, charset));
}
});
} : function(callback) {
return fs.readFile(file, "utf8", callback);
});
}
exports.readFileP = readFileP;
exports.readJsonFileP = function(file) {
return readFileP(file).then(function(json) {
return JSON.parse(json);
});
};
function mkdirP(dir) {
return makePromise(function(callback) {
mkdirp(dir, function(err) {
callback(err, dir);
});
});
}
exports.mkdirP = mkdirP;
function readFromStdinP(timeLimit, message, color) {
var deferred = Q.defer();
var ins = [];
timeLimit = timeLimit || 1000;
var timeout = setTimeout(function() {
log.err(
message || ("Warning: still waiting for STDIN after " +
timeLimit + "ms"),
color || "yellow"
);
}, timeLimit);
try {
// On Windows, just accessing process.stdin throws an exception
// when no standard input has been provided. For consistency with
// other platforms, log the error but continue waiting (until
// killed) for the nonexistent input.
var stdin = process.stdin;
} catch (err) {
log.err(err);
}
if (stdin) {
stdin.resume();
stdin.setEncoding("utf8");
stdin.on("data", function(data) {
ins.push(data);
}).on("end", function() {
clearTimeout(timeout);
deferred.resolve(ins.join(""));
});
}
return deferred.promise;
}
exports.readFromStdinP = readFromStdinP;
exports.readJsonFromStdinP = function(timeLimit) {
return readFromStdinP(timeLimit).then(function(input) {
return JSON.parse(input);
});
};
function deepHash(val) {
var hash = createHash("sha1");
var type = typeof val;
if (val === null) {
type = "null";
}
switch (type) {
case "object":
Object.keys(val).sort().forEach(function(key) {
if (typeof val[key] === "function") {
// Silently ignore nested methods, but nevertheless
// complain below if the root value is a function.
return;
}
hash.update(key + "\0")
.update(deepHash(val[key]));
});
break;
case "function":
assert.ok(false, "cannot hash function objects");
break;
default:
hash.update(val + "");
break;
}
return hash.digest("hex");
}
exports.deepHash = deepHash;
exports.existsP = function(fullPath) {
return makePromise(function(callback) {
fs.exists(fullPath, function(exists) {
callback(null, exists);
});
});
};
function writeFdP(fd, content) {
return makePromise(function(callback) {
content += "";
var buffer = new Buffer(content, "utf8");
var length = fs.writeSync(fd, buffer, 0, buffer.length, null);
assert.strictEqual(length, buffer.length);
callback(null, content);
}).finally(function() {
fs.closeSync(fd);
});
}
exports.writeFdP = writeFdP;
function openFileP(file, mode) {
return makePromise(function(callback) {
fs.open(file, mode || "w+", callback);
});
}
exports.openFileP = openFileP;
function openExclusiveP(file) {
// The 'x' in "wx+" means the file must be newly created.
return openFileP(file, "wx+");
}
exports.openExclusiveP = openExclusiveP;
exports.copyP = function(srcFile, dstFile) {
return makePromise(function(callback) {
var reader = fs.createReadStream(srcFile);
function onError(err) {
callback(err || new Error(
"error in util.copyP(" +
JSON.stringify(srcFile) + ", " +
JSON.stringify(dstFile) + ")"
));
}
reader.on("error", onError).pipe(
fs.createWriteStream(dstFile)
).on("finish", function() {
callback(null, dstFile);
}).on("error", onError);
});
};
// Even though they use synchronous operations to avoid race conditions,
// linkP and unlinkP have promise interfaces, for consistency. Note that
// this means the operation will not happen until at least the next tick
// of the event loop, but it will be atomic when it happens.
exports.linkP = function(srcFile, dstFile) {
return mkdirP(path.dirname(dstFile)).then(function() {
if (fs.existsSync(dstFile))
fs.unlinkSync(dstFile);
fs.linkSync(srcFile, dstFile);
return dstFile;
});
};
exports.unlinkP = function(file) {
return makePromise(function(callback) {
try {
if (fs.existsSync(file))
fs.unlinkSync(file);
callback(null, file);
} catch (err) {
callback(err);
}
});
};
var colors = {
bold: "\033[1m",
red: "\033[31m",
green: "\033[32m",
yellow: "\033[33m",
cyan: "\033[36m",
reset: "\033[0m"
};
Object.keys(colors).forEach(function(key) {
if (key !== "reset") {
exports[key] = function(text) {
return colors[key] + text + colors.reset;
};
}
});
var log = exports.log = {
out: function(text, color) {
text = (text + "").trim();
if (colors.hasOwnProperty(color))
text = colors[color] + text + colors.reset;
process.stdout.write(text + "\n");
},
err: function(text, color) {
text = (text + "").trim();
if (!colors.hasOwnProperty(color))
color = "red";
text = colors[color] + text + colors.reset;
process.stderr.write(text + "\n");
}
};
var slugExp = /[^a-z\-]/ig;
exports.idToSlug = function(id) {
return id.replace(slugExp, "_");
};
var moduleIdExp = /^[ a-z0-9\-_\/\.]+$/i;
exports.isValidModuleId = function(id) {
return id === "<stdin>" || moduleIdExp.test(id);
};
var objToStr = Object.prototype.toString;
var arrStr = objToStr.call([]);
function flatten(value, into) {
if (objToStr.call(value) === arrStr) {
into = into || [];
for (var i = 0, len = value.length; i < len; ++i)
if (i in value) // Skip holes.
flatten(value[i], into);
} else if (into) {
into.push(value);
} else {
return value;
}
return into;
};
exports.flatten = flatten;
exports.inherits = function(ctor, base) {
return ctor.prototype = Object.create(base.prototype, {
constructor: { value: ctor }
});
};
function absolutize(moduleId, requiredId) {
if (requiredId.charAt(0) === ".")
requiredId = path.join(moduleId, "..", requiredId);
return path.normalize(requiredId).replace(/\\/g, '/');
}
exports.absolutize = absolutize;
function relativize(moduleId, requiredId) {
requiredId = absolutize(moduleId, requiredId);
if (requiredId.charAt(0) === ".") {
// Keep the required ID relative.
} else {
// Relativize the required ID.
requiredId = path.relative(
path.join(moduleId, ".."),
requiredId
);
}
if (requiredId.charAt(0) !== ".")
requiredId = "./" + requiredId;
return requiredId.replace(/\\/g, '/');
}
exports.relativize = relativize;
function waitForValuesP(obj, makeCopy) {
if (typeof obj !== "object")
return Q(obj);
var result = makeCopy ? {} : obj;
var keys = Object.keys(obj);
if (keys.length === 0)
return Q(result);
return Q.all(keys.map(function(key) {
return obj[key];
})).then(function(values) {
for (var i = values.length - 1; i >= 0; --i)
result[keys[i]] = values[i];
return result;
});
}
exports.waitForValuesP = waitForValuesP;
function camelize(hyphenated) {
return hyphenated.replace(/-(.)/g, function(_, ch) {
return ch.toUpperCase();
});
}
exports.camelize = camelize;