Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81164 views
1
var assert = require("assert");
2
var fs = require("fs");
3
var path = require("path");
4
var file = path.join(__dirname, "install.js");
5
6
exports.makeGlobal = function() {
7
require("./install");
8
};
9
10
function Reader(file) {
11
var self = this;
12
assert.ok(self instanceof Reader);
13
14
var args;
15
var qhead = {};
16
var qtail = qhead;
17
18
fs.readFile(file, "utf8", function(err, data) {
19
args = [err, data];
20
process.nextTick(flush);
21
});
22
23
function flush() {
24
var next = qhead.next
25
if (next && args) {
26
qhead = next;
27
process.nextTick(flush);
28
next.cb.apply(null, args);
29
}
30
}
31
32
self.addCallback = function(cb) {
33
qtail = qtail.next = { cb: cb };
34
if (qhead.next === qtail)
35
process.nextTick(flush);
36
};
37
}
38
39
var reader;
40
41
exports.getCode = function(callback) {
42
reader = reader || new Reader(file);
43
reader.addCallback(callback);
44
};
45
46
function rename(installName, code) {
47
if (installName !== "install")
48
code = code.replace(
49
/\bglobal\.install\b/g,
50
"global." + installName);
51
return code;
52
}
53
54
exports.renameCode = function(installName, callback) {
55
reader = reader || new Reader(file);
56
reader.addCallback(function(err, data) {
57
callback(err, rename(installName, data));
58
});
59
};
60
61
function getCodeSync() {
62
return fs.readFileSync(file, "utf8");
63
}
64
exports.getCodeSync = getCodeSync;
65
66
exports.renameCodeSync = function(installName) {
67
return rename(installName, getCodeSync());
68
};
69
70
// Not perfect, but we need to match the behavior of install.js.
71
var requireExp = /\brequire\(['"]([^'"]+)['"]\)/g;
72
73
// This function should match the behavior of `ready` and `absolutize` in
74
// install.js, but the implementations are not worth unifying because we have
75
// access to the "path" module here.
76
exports.getRequiredIDs = function(id, source) {
77
var match, seen = {}, ids = [];
78
79
requireExp.lastIndex = 0;
80
while ((match = requireExp.exec(source))) {
81
var rid = match[1];
82
if (rid.charAt(0) === ".")
83
rid = path.normalize(path.join(id, "..", match[1]));
84
85
if (!seen.hasOwnProperty(rid)) {
86
seen[rid] = true;
87
ids.push(rid);
88
}
89
}
90
91
return ids;
92
};
93
94