-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathprince-npm.js
More file actions
341 lines (330 loc) · 17.5 KB
/
prince-npm.js
File metadata and controls
341 lines (330 loc) · 17.5 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
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
/*
** node-prince -- Node API for executing PrinceXML via prince(1) CLI
** Copyright (c) 2014-2026 Dr. Ralf S. Engelschall <rse@engelschall.com>
**
** 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.
*/
/* eslint no-console: 0 */
/*
* prince-npm.js: NPM install-time integration
*/
/* core requirements */
const child_process = require("child_process")
const fs = require("fs")
const path = require("path")
const zlib = require("zlib")
/* extra requirements */
const progress = require("progress")
const promise = require("promise")
const axios = require("axios")
const which = require("which")
const chalk = require("chalk")
const tar = require("tar")
const streamzip = require("node-stream-zip")
const rimraf = require("rimraf")
const mkdirp = require("mkdirp")
/* determine path and version of prince(1) */
const princeInfo = function () {
return new promise(function (resolve, reject) {
which("prince").then(function (filename) {
child_process.execFile(filename, [ "--version" ], function (error, stdout, stderr) {
if (error !== null) {
reject(`prince(1) failed on "--version": ${error}`)
return
}
const m = stdout.match(/^Prince\s+(\d+(?:\.\d+)?)/)
if (m === null) {
reject(`prince(1) returned unexpected output on "--version":\n${stdout}${stderr}`)
return
}
resolve({ command: filename, version: m[1] })
})
}).catch(function (error) {
reject(`prince(1) not found in PATH: ${error}`)
})
})
}
/* return download URL for latest PrinceXML distribution */
const princeDownloadURL = function () {
return new promise(function (resolve /*, reject */) {
const id = `${process.arch}-${process.platform}`
if (id.match(/^ia32-win32$/))
resolve("https://www.princexml.com/download/prince-16.2-win32.zip")
else if (id.match(/^x64-win32$/))
resolve("https://www.princexml.com/download/prince-16.2-win64.zip")
else if (id.match(/^arm64-win32$/))
resolve("https://www.princexml.com/download/prince-16.2-win-arm64.zip")
else if (id.match(/^(?:x64|arm64)-darwin/))
resolve("https://www.princexml.com/download/prince-16.2-macos.zip")
else {
child_process.execFile("sh", [ path.join(__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)
}
const platform = stdout.toString().replace(/^(\S+).*\n?$/, "$1")
if (id.match(/^x64-linux/)) {
if (platform.match(/^amd64-ubuntu1[89](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-ubuntu20.04-amd64.tar.gz")
else if (platform.match(/^amd64-ubuntu2[23](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-ubuntu22.04-amd64.tar.gz")
else if (platform.match(/^amd64-ubuntu24(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-ubuntu24.04-amd64.tar.gz")
else if (platform.match(/^amd64-debian13(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-debian13-amd64.tar.gz")
else if (platform.match(/^amd64-debian12(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-debian12-amd64.tar.gz")
else if (platform.match(/^amd64-debian11(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-debian11-amd64.tar.gz")
else if (platform.match(/^amd64-almalinux10(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-almalinux10-x86_64.tar.gz")
else if (platform.match(/^amd64-almalinux9(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-almalinux9-x86_64.tar.gz")
else if (platform.match(/^amd64-almalinux8(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-almalinux8-x86_64.tar.gz")
else if (platform.match(/^amd64-alpine3\.23(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.23-x86_64.tar.gz")
else if (platform.match(/^amd64-alpine3\.22(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.22-x86_64.tar.gz")
else if (platform.match(/^amd64-alpine3\.21(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.21-x86_64.tar.gz")
else if (platform.match(/^amd64-alpine3\.20(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.20-x86_64.tar.gz")
else if (platform.match(/^amd64-alpine3\.19(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.19-x86_64.tar.gz")
else if (platform.match(/^amd64-opensuse15\.6(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-opensuse15.6-x86_64.tar.gz")
else if (id.match(/^x64-/))
resolve("https://www.princexml.com/download/prince-16.2-linux-generic-x86_64.tar.gz")
}
else if (id.match(/^arm64-linux/)) {
if (platform.match(/^arm64-ubuntu24(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-ubuntu24.04-arm64.tar.gz")
else if (platform.match(/^arm64-ubuntu2[23](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-ubuntu22.04-arm64.tar.gz")
else if (platform.match(/^arm64-debian13(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-debian13-arm64.tar.gz")
else if (platform.match(/^arm64-debian12(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-debian12-arm64.tar.gz")
else if (platform.match(/^arm64-debian11(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-debian11-arm64.tar.gz")
else if (platform.match(/^arm64-alpine3\.23(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.23-aarch64.tar.gz")
else if (platform.match(/^arm64-alpine3\.22(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.22-aarch64.tar.gz")
else if (platform.match(/^arm64-alpine3\.21(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.21-aarch64.tar.gz")
else if (platform.match(/^arm64-alpine3\.20(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.20-aarch64.tar.gz")
else if (platform.match(/^arm64-alpine3\.19(?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-alpine3.19-aarch64.tar.gz")
else if (platform.match(/^aarch64-alpine[23](?:\.\d+)*$/))
resolve("https://www.princexml.com/download/prince-16.2-linux-generic-aarch64-musl.tar.gz")
else
resolve("https://www.princexml.com/download/prince-16.2-linux-generic-aarch64.tar.gz")
}
else if (platform.match(/^amd64-freebsd15(?:\.\d+)*/))
resolve("https://www.princexml.com/download/prince-16.2-freebsd15.0-amd64.tar.gz")
else if (platform.match(/^amd64-freebsd14(?:\.\d+)*/))
resolve("https://www.princexml.com/download/prince-16.2-freebsd14.0-amd64.tar.gz")
else if (platform.match(/^amd64-freebsd13(?:\.\d+)*/))
resolve("https://www.princexml.com/download/prince-16.2-freebsd13.0-amd64.tar.gz")
else {
console.log(chalk.red(`ERROR: PrinceXML not available for platform "${platform}" ("${id}")`))
process.exit(1)
}
})
}
})
}
/* download data from URL */
const downloadData = function (url) {
return new promise(function (resolve, reject) {
let progress_bar = null
const options = {
method: "GET",
url: url,
encoding: null,
headers: {
"User-Agent": "node-prince (prince-npm.js:install)"
},
responseType: "arraybuffer",
onDownloadProgress: function(progressEvent) {
if (!progress_bar) {
progress_bar = new progress(
"-- download: [:bar] :percent (ETA: :etas)", {
complete: "#",
incomplete: "=",
width: 40,
total: progressEvent.total
})
}
progress_bar.tick(progressEvent.loaded)
}
}
;(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}`)
axios(options).then(function (response) {
if (response.status === 200) {
console.log(`-- download: ${response.data.length} bytes received.`)
resolve(response.data)
}
}).catch(function (error) {
reject(`download failed: ${error}`)
})
})
})
}
/* extract a zipfile (*.zip) */
const extractZipfile = function (zipfile, stripdir, destdir) {
return new promise(function (resolve, reject) {
const 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) */
const 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 () { setTimeout(function () { resolve() }, 500) })
})
}
/* main procedure */
if (process.argv.length !== 3) {
console.log(chalk.red("ERROR: invalid number of arguments"))
process.exit(1)
}
let 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 distribution")
princeDownloadURL().then(function (url) {
downloadData(url).then(function (data) {
console.log("++ locally unpacking PrinceXML distribution")
destdir = path.join(__dirname, "prince")
let destfile
const id = `${process.arch}-${process.platform}`
if (id.match(/^ia32-win32$/)) {
destfile = path.join(__dirname, "prince.zip")
fs.writeFileSync(destfile, data, { encoding: null })
mkdirp.sync(destdir)
extractZipfile(destfile, "prince-16.2-win32", destdir).then(function () {
fs.chmodSync(path.join(destdir, "bin", "prince.exe"), 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 (id.match(/^(?:x64|arm64)-win32$/)) {
destfile = path.join(__dirname, "prince.zip")
fs.writeFileSync(destfile, data, { encoding: null })
mkdirp.sync(destdir)
extractZipfile(destfile, "prince-16.2-win64", destdir).then(function () {
fs.chmodSync(path.join(destdir, "bin", "prince.exe"), 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-16.2-macos", destdir).then(function () {
fs.chmodSync(path.join(destdir, "lib/prince/bin/prince"), 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).then(function () {
console.log("-- OK: done")
}).catch(function (error) {
console.log(chalk.red(`** ERROR: ${error}`))
})
}
}
else {
console.log(chalk.red("ERROR: invalid argument"))
process.exit(1)
}