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
var assert = require("assert");
var fs = require("fs");
var path = require("path");
var file = path.join(__dirname, "install.js");
exports.makeGlobal = function() {
require("./install");
};
function Reader(file) {
var self = this;
assert.ok(self instanceof Reader);
var args;
var qhead = {};
var qtail = qhead;
fs.readFile(file, "utf8", function(err, data) {
args = [err, data];
process.nextTick(flush);
});
function flush() {
var next = qhead.next
if (next && args) {
qhead = next;
process.nextTick(flush);
next.cb.apply(null, args);
}
}
self.addCallback = function(cb) {
qtail = qtail.next = { cb: cb };
if (qhead.next === qtail)
process.nextTick(flush);
};
}
var reader;
exports.getCode = function(callback) {
reader = reader || new Reader(file);
reader.addCallback(callback);
};
function rename(installName, code) {
if (installName !== "install")
code = code.replace(
/\bglobal\.install\b/g,
"global." + installName);
return code;
}
exports.renameCode = function(installName, callback) {
reader = reader || new Reader(file);
reader.addCallback(function(err, data) {
callback(err, rename(installName, data));
});
};
function getCodeSync() {
return fs.readFileSync(file, "utf8");
}
exports.getCodeSync = getCodeSync;
exports.renameCodeSync = function(installName) {
return rename(installName, getCodeSync());
};
// Not perfect, but we need to match the behavior of install.js.
var requireExp = /\brequire\(['"]([^'"]+)['"]\)/g;
// This function should match the behavior of `ready` and `absolutize` in
// install.js, but the implementations are not worth unifying because we have
// access to the "path" module here.
exports.getRequiredIDs = function(id, source) {
var match, seen = {}, ids = [];
requireExp.lastIndex = 0;
while ((match = requireExp.exec(source))) {
var rid = match[1];
if (rid.charAt(0) === ".")
rid = path.normalize(path.join(id, "..", match[1]));
if (!seen.hasOwnProperty(rid)) {
seen[rid] = true;
ids.push(rid);
}
}
return ids;
};