This repository was archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplatform.js
More file actions
183 lines (164 loc) · 5.53 KB
/
platform.js
File metadata and controls
183 lines (164 loc) · 5.53 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
var cmd = process.argv[2];
var exec = require("child_process").exec;
var spawn = require("child_process").spawn;
var fs = require("fs");
var path = require("path");
var http = require("http");
var https = require("https");
var util = require("util");
var os = require("os");
var make = process.env.MAKE || "make";
if (process.platform === "win32") {
win();
} else {
nix();
}
function exithandler(cmd, cb) {
return function(code) {
if (code != null && code !== 0) {
console.log("" + cmd + " process exited with code " + code);
process.exit(code);
}
if (cb) {
cb();
}
}
}
function nix() {
switch (cmd) {
case "preinstall":
{
var config = {};
fs.writeFileSync("configure.gypi", JSON.stringify(config));
console.log("Building EJDB...");
var m = spawn(make, ["-f", "libejdb.mk"], {"stdio" : "inherit"});
m.on("close", exithandler(make + " all", function() {
var ng = spawn("node-gyp", ["rebuild"], {stdio : "inherit"});
ng.on("close", exithandler("node-gyp"));
}));
break;
}
case "test":
{
console.log("Tesing Node EJDB...");
var m = spawn(make, ["-f", "tests.mk", "check"], {stdio : "inherit"});
m.on("close", exithandler(make));
}
}
}
function win() {
switch (cmd) {
case "preinstall":
{
var dlurl = process.env["npm_package_config_windownloadurl_" + os.arch()];
if (dlurl == null) {
console.log("Invalid package configuration, missing windows binaries download url");
process.exit(1);
}
var sdir = "ejdbdll";
try {
fs.statSync(sdir);
} catch (e) {
if ("ENOENT" !== e.code) {
throw e;
}
fs.mkdirSync(sdir);
}
var zfileExist = false;
var zfile = path.join(sdir, path.basename(dlurl));
try {
fs.statSync(zfile);
zfileExist = true;
} catch (e) {
if ("ENOENT" !== e.code) {
throw e;
}
}
if (!zfileExist) {
console.log("Downloading windows binaries from: %s ...", dlurl);
console.log("File: %s", zfile);
var req = https.get(dlurl, function(res) {
if (res.statusCode !== 200) {
console.log("Invalid response code %d", res.statusCode);
process.exit(1);
}
var len = 0;
var cnt = 0;
var wf = fs.createWriteStream(zfile);
var eh = function(ev) {
console.log("Error receiving data from %s Error: %s", dlurl, ev);
process.exit(1);
};
wf.on("error", eh);
res.on("error", eh);
res.on("data", function(chunk) {
if (++cnt % 80 == 0) {
process.stdout.write("\n");
}
len += chunk.length;
process.stdout.write(".");
});
res.on("end", function() {
console.log("\n%d bytes received", len);
setTimeout(processArchive, 2000);
});
res.pipe(wf);
});
req.end();
} else {
processArchive();
}
function processArchive() {
var targz = require("tar.gz");
console.log("Unzip archive '%s'", zfile);
var tgz = new targz();
tgz.extract(zfile, sdir, function(err){
if (err) {
console.log(err);
process.exit(1);
return;
}
sdir = path.resolve(sdir);
var config = {};
config["variables"] = {
"EJDB_HOME" : sdir
};
fs.writeFileSync("configure.gypi", JSON.stringify(config));
var args = ["configure", "rebuild"];
console.log("node-gyp %j", args);
var ng = spawn("node-gyp.cmd", args, {stdio : "inherit"});
ng.on("error", function(ev) {
console.log("Spawn error: " + ev);
process.exit(1);
});
ng.on("close", exithandler("node-gyp", function() {
copyFile(path.join(sdir, "bin/libejdb.dll"),
"lib/libejdb.dll",
exithandler("copy libejdb.dll"));
}));
});
}
}
}
}
function copyFile(source, target, cb) {
var cbCalled = false;
var rd = fs.createReadStream(source);
rd.on("error", function(err) {
done(err);
});
var wr = fs.createWriteStream(target);
wr.on("error", function(err) {
done(err);
});
wr.on("close", function(ex) {
done();
});
rd.pipe(wr);
function done(err) {
if (!cbCalled) {
cb(err);
cbCalled = true;
}
}
}