-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-package.js
42 lines (35 loc) · 1.24 KB
/
generate-package.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
const archiver = require('archiver');
const fs = require('fs');
const path = require('path');
const outputDir = process.argv.pop();
const inputDir = process.argv.pop();
const output = fs.createWriteStream(path.join(outputDir, 'ebook.epub'));
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// This event is fired when the data source is drained no matter what was the data source.
// It is not part of this library but rather from the NodeJS Stream API.
// @see: https://nodejs.org/api/stream.html#stream_event_end
output.on('end', function() {
console.log('Data has been drained');
});
// good practice to catch warnings (ie stat failures and other non-blocking errors)
archive.on('warning', function(err) {
if (err.code === 'ENOENT') {
// log warning
console.log("Warning", err)
} else {
// throw error
throw err;
}
});
// good practice to catch this error explicitly
archive.on('error', function(err) {
throw err;
});
// pipe archive data to the file
archive.pipe(output);
archive.directory(path.join(inputDir, 'META-INF'), 'META-INF');
archive.directory(path.join(inputDir, 'OEBPS'), 'OEBPS');
archive.append('application/epub+zip', { name: 'mimetype', store: true });
archive.finalize();