forked from rse/node-prince
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprince-npm.js
384 lines (371 loc) · 19 KB
/
prince-npm.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
/*
** node-prince -- Node API for executing PrinceXML via prince(1) CLI
** Copyright (c) 2014-2021 Dr. Ralf S. Engelschall <[email protected]>
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/* global process: false */
/* global __dirname: false */
/* global require: false */
/* global console: false */
/* eslint no-console: 0 */
/*
* prince-npm.js: NPM install-time integration
*/
/* core requirements */
var child_process = require("child_process");
var fs = require("fs");
var path = require("path");
var zlib = require("zlib");
/* extra requirements */
var progress = require("progress");
var promise = require("promise");
var request = require("request");
var which = require("which");
var chalk = require("chalk");
var tar = require("tar");
var streamzip = require("node-stream-zip");
var rimraf = require("rimraf");
var mkdirp = require("mkdirp");
/* determine preferred prince version in parent package.json */
var princeVersion = function () {
var parentPackage = path.normalize(process.env.INIT_CWD + "/package.json");
var princeConfig = require(parentPackage).prince;
var version = "14.2"; // default
if (princeConfig) {
if (princeConfig.version) {
version = princeConfig.version;
return version;
}
} else {
return;
}
};
/* determine whether to use prince or prince-books executable */
var princeExecutable = function () {
var executable = "prince";
if (princeVersion().includes("books")) {
executable = "prince-books";
}
return executable;
}
/* determine path and version of prince(1) */
var princeInfo = function () {
return new promise(function (resolve, reject) {
which(princeExecutable, function (error, filename) {
if (error) {
reject("prince(1) not found in PATH: " + error);
return;
}
child_process.execFile(filename, [ "--version" ], function (error, stdout, stderr) {
if (error !== null) {
reject("prince(1) failed on \"--version\": " + error);
return;
}
var m = stdout.match(/^Prince\s+(\d+(?:\.\d+)?)/);
if (!(m !== null && typeof m[1] !== "undefined")) {
reject("prince(1) returned unexpected output on \"--version\":\n" + stdout + stderr);
return;
}
if (m[1] === princeVersion()) {
console.log("Prince version " + princeVersion() + " already installed.");
resolve({ command: filename, version: m[1] });
} else {
reject("your project requires Prince version " + princeVersion());
return;
}
});
});
});
};
/* return download URL for latest PrinceXML distribution */
var princeDownloadURL = function () {
return new promise(function (resolve /*, reject */) {
var id = process.arch + "-" + process.platform;
if (id.match(/^ia32-win32$/)) {
if (princeVersion().includes("books")) {
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-win32.zip");
} else {
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-win32-setup.exe");
}
}
else if (id.match(/^x64-win32$/)) {
if (princeVersion().includes("books")) {
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-win64.zip");
} else {
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-win64-setup.exe");
}
}
else if (id.match(/^(?:ia32|x64)-darwin/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-macos.zip");
else if (id.match(/^arm64-darwin/))
/* PrinceXML does not have an ARM64 build yet, but the Intel x64 version will
work just fine as long as Rosetta 2 is installed. Leave this condition here
and replace with the ARM64 build when it is available in the future. */
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-macos.zip");
else {
child_process.exec("sh \"" + __dirname + "/shtool\" platform -t binary", function (error, stdout /*, stderr */) {
if (error) {
console.log(chalk.red("ERROR: failed to determine platform details on platform \"" + id + "\": " + error));
process.exit(1);
}
var platform = stdout.toString().replace(/^(\S+).*\n?$/, "$1");
if (id.match(/^(?:ia32|x64)-linux/)) {
if (platform.match(/^ix86-ubuntu1[45](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-ubuntu14.04-i386.tar.gz");
else if (platform.match(/^amd64-ubuntu1[45](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-ubuntu14.04-amd64.tar.gz");
else if (platform.match(/^ix86-ubuntu1[67](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-ubuntu16.04-i386.tar.gz");
else if (platform.match(/^amd64-ubuntu1[67](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-ubuntu16.04-amd64.tar.gz");
else if (platform.match(/^ix86-ubuntu1[89](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-ubuntu18.04-i386.tar.gz");
else if (platform.match(/^amd64-ubuntu1[89](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-ubuntu18.04-amd64.tar.gz");
else if (platform.match(/^amd64-ubuntu2[01](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-ubuntu20.04-amd64.tar.gz");
else if (platform.match(/^amd64-debian10(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-debian10-amd64.tar.gz");
else if (platform.match(/^amd64-debian9(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-debian9-amd64.tar.gz");
else if (platform.match(/^amd64-debian8(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-debian8-amd64.tar.gz");
else if (platform.match(/^amd64-centos8(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-centos8-x86_64.tar.gz");
else if (platform.match(/^amd64-centos7(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-centos7-x86_64.tar.gz");
else if (platform.match(/^amd64-centos6(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-centos6-x86_64.tar.gz");
else if (platform.match(/^amd64-alpine3\.13(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-alpine3.13-x86_64.tar.gz");
else if (platform.match(/^amd64-alpine3\.12(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-alpine3.12-x86_64.tar.gz");
else if (platform.match(/^amd64-alpine3\.11(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-alpine3.11-x86_64.tar.gz");
else if (platform.match(/^amd64-alpine3\.10(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-alpine3.10-x86_64.tar.gz");
else if (platform.match(/^amd64-opensuse15.2(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-opensuse15.2-x86_64.tar.gz");
else if (platform.match(/^amd64-opensuse42.3(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-opensuse42.3-x86_64.tar.gz");
else if (id.match(/^ia32-/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-linux-generic-i686.tar.gz");
else if (id.match(/^x64-/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-linux-generic-x86_64.tar.gz");
}
else if (id.match(/^x64-freebsd12(?:\.\d+)*/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-freebsd12.0-amd64.tar.gz");
else if (id.match(/^x64-freebsd13(?:\.\d+)*/))
resolve("https://www.princexml.com/download/prince-" + princeVersion() + "-freebsd13.0-amd64.tar.gz");
else {
console.log(chalk.red("ERROR: PrinceXML not available for platform \"" + platform + "\""));
process.exit(1);
}
});
}
});
};
/* download data from URL */
var downloadData = function (url) {
return new promise(function (resolve, reject) {
var options = {
method: "GET",
url: url,
encoding: null,
headers: {
"User-Agent": "node-prince (prince-npm.js:install)"
}
};
(new promise(function (resolve /*, reject */) {
if (typeof process.env.http_proxy === "string" && process.env.http_proxy !== "") {
options.proxy = process.env.http_proxy;
console.log("-- using proxy ($http_proxy): " + options.proxy);
resolve();
}
else {
child_process.exec("npm config get proxy", function (error, stdout /*, stderr */) {
if (error === null) {
stdout = stdout.toString().replace(/\r?\n$/, "");
if (stdout.match(/^https?:\/\/.+/)) {
options.proxy = stdout;
console.log("-- using proxy (npm config get proxy): " + options.proxy);
}
}
resolve();
});
}
})).then(function () {
console.log("-- download: " + url);
var req = request(options, function (error, response, body) {
if (!error && response.statusCode === 200) {
console.log("-- download: " + body.length + " bytes received.");
resolve(body);
}
else
reject("download failed: " + error);
});
var progress_bar = null;
req.on("response", function (response) {
var len = parseInt(response.headers["content-length"], 10);
progress_bar = new progress(
"-- download: [:bar] :percent (ETA: :etas)", {
complete: "#",
incomplete: "=",
width: 40,
total: len
});
});
req.on("data", function (data) {
if (progress_bar !== null)
progress_bar.tick(data.length);
});
});
});
};
/* extract a zipfile (*.zip) */
var extractZipfile = function (zipfile, stripdir, destdir) {
return new promise(function (resolve, reject) {
var zip = new streamzip({ file: zipfile });
zip.on("ready", function () {
zip.extract(stripdir, destdir, function (error) {
zip.close();
if (error) {
reject(error);
} else {
setTimeout(function () { resolve(); }, 500);
}
});
});
});
};
/* extract a tarball (*.tar.gz) */
var extractTarball = function (tarball, destdir, stripdirs) {
return new promise(function (resolve, reject) {
fs.createReadStream(tarball)
.pipe(zlib.createGunzip())
.pipe(tar.extract({ cwd: destdir, strip: stripdirs }))
.on("error", function (error) { reject(error); })
.on("close", function () { /* global setTimeout: true */ setTimeout(function () { resolve(); }, 500); });
});
};
/* main procedure */
if (process.argv.length !== 3) {
console.log(chalk.red("ERROR: invalid number of arguments"));
process.exit(1);
}
var destdir;
if (process.argv[2] === "install") {
/* installation procedure */
console.log("++ checking for globally installed PrinceXML");
princeInfo().then(function (prince) {
console.log("-- found prince(1) command: " + chalk.blue(prince.command));
console.log("-- found prince(1) version: " + chalk.blue(prince.version));
}, function (/* error */) {
console.log("++ downloading PrinceXML version " + princeVersion());
princeDownloadURL().then(function (url) {
downloadData(url).then(function (data) {
console.log("++ locally unpacking PrinceXML distribution");
destdir = path.join(__dirname, "prince");
var destfile;
if (process.platform === "win32" && princeExecutable() === "prince") {
destfile = path.join(__dirname, princeExecutable() + ".exe");
fs.writeFileSync(destfile, data, { encoding: null });
var args = [ "/s", "/a", "/vTARGETDIR=\"" + path.resolve(destdir) + "\" /qn" ];
child_process.execFile(destfile, args, function (error, stdout, stderr) {
if (error !== null) {
console.log(chalk.red("** ERROR: failed to extract: " + error));
stdout = stdout.toString();
stderr = stderr.toString();
if (stdout !== "")
console.log("** STDOUT: " + stdout);
if (stderr !== "")
console.log("** STDERR: " + stderr);
}
else {
fs.unlinkSync(destfile);
console.log("-- OK: local PrinceXML installation now available");
}
});
}
else if (process.platform === "win32" && princeExecutable() === "prince-books") {
destfile = path.join(__dirname, "prince.zip");
fs.writeFileSync(destfile, data, { encoding: null });
mkdirp.sync(destdir);
var winArch = "win32";
if (process.arch.includes("64")) {
winArch = "win64";
}
extractZipfile(destfile, "prince-" + princeVersion() + "-" + winArch, destdir).then(function () {
fs.chmodSync(path.join(destdir, "lib/prince/bin/" + princeExecutable()), fs.constants.S_IRWXU
| fs.constants.S_IRGRP | fs.constants.S_IXGRP | fs.constants.S_IROTH | fs.constants.S_IXOTH);
fs.unlinkSync(destfile);
console.log("-- OK: local PrinceXML installation now available");
}, function (error) {
console.log(chalk.red("** ERROR: failed to extract: " + error));
});
}
else if (process.platform === "darwin") {
destfile = path.join(__dirname, "prince.zip");
fs.writeFileSync(destfile, data, { encoding: null });
mkdirp.sync(destdir);
extractZipfile(destfile, "prince-" + princeVersion() + "-macos", destdir).then(function () {
fs.chmodSync(path.join(destdir, "lib/prince/bin/" + princeExecutable()), fs.constants.S_IRWXU
| fs.constants.S_IRGRP | fs.constants.S_IXGRP | fs.constants.S_IROTH | fs.constants.S_IXOTH);
fs.unlinkSync(destfile);
console.log("-- OK: local PrinceXML installation now available");
}, function (error) {
console.log(chalk.red("** ERROR: failed to extract: " + error));
});
}
else {
destfile = path.join(__dirname, "prince.tgz");
fs.writeFileSync(destfile, data, { encoding: null });
mkdirp.sync(destdir);
extractTarball(destfile, destdir, 1).then(function () {
fs.unlinkSync(destfile);
console.log("-- OK: local PrinceXML installation now available");
}, function (error) {
console.log(chalk.red("** ERROR: failed to extract: " + error));
});
}
}, function (error) {
console.log(chalk.red("** ERROR: failed to download: " + error));
});
});
});
}
else if (process.argv[2] === "uninstall") {
/* uninstallation procedure */
destdir = path.join(__dirname, "prince");
if (fs.existsSync(destdir)) {
console.log("++ deleting locally unpacked PrinceXML distribution");
rimraf(destdir, function (error) {
if (error !== null)
console.log(chalk.red("** ERROR: " + error));
else
console.log("-- OK: done");
});
}
}
else {
console.log(chalk.red("ERROR: invalid argument"));
process.exit(1);
}