-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (46 loc) · 1.58 KB
/
index.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
const pfs = require('fs-promise-native');
const fs = require('fs');
const yazl = require('yazl');
const { join, basename, dirname, resolve: absolute } = require('path');
const walk = require('walkdir');
const [ _nodeExe, _script, ...folderPaths ] = process.argv;
const find = (path) => new Promise((resolve, reject) => {
const walker = walk(path);
let rawFiles = [];
walker.on('file', (name) => { rawFiles.push(name) });
walker.on('end', () => { resolve({ rawFiles, path }) });
walker.on('error', reject);
});
const clean = async ({ rawFiles, path }) => {
const junk = (file) =>
!file.toLowerCase().endsWith('mimetype') &&
!basename(file).startsWith('.');
const files = rawFiles.filter(junk);
return { files, path }
}
const compress = ({ files, path }) => new Promise((resolve, reject) => {
const container = dirname(absolute(path));
const epubName = basename(path) + ".epub";
const destinationFile = join(container, epubName);
console.log(destinationFile);
const zip = new yazl.ZipFile();
const output = fs.createWriteStream(destinationFile);
const mimetype = Buffer.from("application/epub+zip", "ascii");
const mimetypemeta = { compress: false };
zip.outputStream.pipe(output);
zip.addBuffer(mimetype, "mimetype", mimetypemeta);
output.on("close", () => {
resolve("Completed: " + destinationFile);
});
files.forEach((file) => {
zip.addFile(file, file.slice(absolute(path).length + 1));
});
zip.end();
});
folderPaths.forEach((path) => {
find(path)
.then(clean)
.then(compress)
.then(console.info)
.catch(console.error)
});