This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
install.js
229 lines (189 loc) · 6.83 KB
/
install.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
// NOTE: Make sure ./package.json ~ config.srcNpmUri is set to a URL containing gnu-tools npm package with sources
// if deploying this package without the `./*-src` directories.
// e.g. https://github.com/c9/node-gnu-tools/tarball/8950ceef20b479382032dfabbcb40a23bb188044
const SPAWN = require("child_process").spawn;
const EXEC = require("child_process").exec;
const GNU_TOOLS = require("./gnu-tools");
const PATH = require("path");
const FS = require("fs");
const OS = require("os");
const EXISTS_SYNC = FS.existsSync != null ? FS.existsSync : PATH.existsSync
const EXISTS = FS.exists != null ? FS.exists : PATH.exists
/* Basic workflow is this:
Are we on Solaris?
1. Yes: compile no matter what
2. No: let's look for find and grep commands; also grep needs PCRE
a. We found them! Nothing else is needed here.
b. We did not find them! Look for the sources in the gnu-tools package
* No sources found! Do `exec npm install` and set cwd()/../ to current gnu-tools dir
*/
function main() {
var binBasePath = PATH.join(__dirname, "/bin");
if (!EXISTS_SYNC(binBasePath)) {
console.log("Creating directory ", binBasePath);
FS.mkdir(binBasePath, 0755);
} else {
console.log("Directory exists at ", binBasePath);
}
// Check if commands exist on PATH.
commandExists("grep", function(err, grep) {
if (err) fail(err);
if (grep === false) {
fail(new Error("You need some version of `grep` on your PATH to compile the gnu-tools version of grep!"));
return;
}
commandExists("find", function(err, find) {
if (err) fail(err);
checkPCRE(grep, function(err, grepPCRE) {
if (err) fail(err);
if (OS.platform() == "SunOS" || find === false || grepPCRE === false) {
console.log("Compiling sources!");
// Grab sources from npm (if necessary)
compileSources(function (err) {
if (err) fail(err);
// All Done.
process.exit(0);
});
}
else {
console.log("Grand, you've already got 'find' and 'grep' on your system.");
// Link to commands on PATH.
if (find !== true) {
if (!EXISTS_SYNC(GNU_TOOLS.FIND_CMD)) {
console.log("Linking ", find, " to ", GNU_TOOLS.FIND_CMD);
FS.symlinkSync(find, GNU_TOOLS.FIND_CMD);
}
}
if (grep !== true) {
if (!EXISTS_SYNC(GNU_TOOLS.GREP_CMD)) {
console.log("Linking ", grep, " to ", GNU_TOOLS.GREP_CMD);
FS.symlinkSync(grep, GNU_TOOLS.GREP_CMD);
}
}
process.exit(0);
}
});
});
});
}
function fail(err) {
console.error(err, err.stack);
process.exit(1);
}
function commandExists(name, callback) {
if (name === "grep") {
if (EXISTS_SYNC(GNU_TOOLS.GREP_CMD)) {
callback(null, true);
return;
}
} else
if (name === "find") {
if (EXISTS_SYNC(GNU_TOOLS.FIND_CMD)) {
callback(null, true);
return;
}
}
// win32 not doing which
var command = process.platform === "win32" ? "where" : "which";
// NOTE: Assuming `which` command exists!
EXEC(command + " " + name, function (error, stdout, stderr) {
if (error || stderr) {
// TODO: Look for `which` command not found error.
callback(null, false);
return;
}
var path = stdout.split("\n")[0].trim();
EXISTS(path, function(exists) {
if (!exists) {
callback(null, false);
return;
}
callback(null, path);
});
});
}
function checkPCRE(grep, callback) {
// we already don't have grep; just stop
if (grep === false) {
callback(null, false);
return;
}
EXEC("grep -P", function (error, stdout, stderr) {
var firstLine = stderr.split("\n")[0];
// if -P is invalid, then grep isn't supporting PCRE
if (firstLine.match(/invalid option/)) {
callback(null, false);
return;
}
else {
callback(null, true);
return;
}
});
}
function runMake(args, callback) {
var make = SPAWN("make", args, {
cwd: __dirname
});
make.stdout.on("data", function(data) {
process.stdout.write(data);
});
make.stderr.on("data", function(data) {
process.stdout.write(data);
});
make.on("exit", function(code) {
if (code !== 0) {
callback(new Error("'make " + args.join(" ") + "' failed with code: " + code));
return;
}
callback(null);
});
}
function compileSources(callback) {
// check if sources already exist; don't get them below if it's not needed
if (EXISTS_SYNC("./findutils-src") && EXISTS_SYNC("./grep-src") && EXISTS_SYNC("./pcre-src")) {
// Compile from source.
runMake([
"install"
], function(err) {
if (err) fail(err);
runMake([
"clean"
], function(err) {
if (err) fail(err);
callback(null);
});
});
return;
}
var descriptor = JSON.parse(FS.readFileSync(PATH.join(__dirname, "package.json")));
var srcNpmUri = descriptor.config.srcNpmUri;
var cmd = "npm";
var cmdArgs = ["install", srcNpmUri];
console.log("Installing gnu-tools sources: " + cmd + " " + cmdArgs.join(" ") + " (cwd: " + __dirname + ")");
var make = SPAWN(cmd, cmdArgs, {
cwd: __dirname
});
make.stdout.on("data", function(data) {
process.stdout.write(data);
});
make.stderr.on("data", function(data) {
process.stdout.write(data);
});
make.on("exit", function(code) {
if (code) {
callback(new Error("'npm install gnu-tools' failed with: " + code));
return;
}
// Swap out new gnu-tools for our current one.
var dirname = __dirname;
FS.renameSync(PATH.join(dirname, "node_modules", "gnu-tools"), PATH.join(dirname, "..", "gnu-tools~src"));
FS.renameSync(PATH.join(dirname, "..", "gnu-tools"), PATH.join(dirname, "..", ".gnu-tools~no-src-" + new Date().getTime()));
FS.renameSync(PATH.join(dirname, "..", "gnu-tools~src"), PATH.join(dirname, "..", "gnu-tools"));
callback(null);
return;
});
}
if (require.main === module) {
main();
}