Implement zip step

This commit is contained in:
Domenic Denicola 2015-05-21 00:58:56 -04:00
commit 48ddecfed4
4 changed files with 34 additions and 1 deletions

1
.gitignore vendored
View file

@ -3,3 +3,4 @@
cache/
book/
Worm.epub

View file

@ -8,6 +8,7 @@ const packageJson = require("../package.json");
const download = require("./download.js");
const convert = require("./convert.js");
const scaffold = require("./scaffold.js");
const zip = require("./zip.js");
require("./track-rejections.js");
@ -83,7 +84,7 @@ if (argv._.indexOf("scaffold") !== -1) {
if (argv._.indexOf("zip") !== -1) {
commands.push(function () {
console.log("Not yet implemented!");
return zip(bookPath, contentPath, path.resolve(argv.out));
});
}

30
lib/zip.js Normal file
View file

@ -0,0 +1,30 @@
"use strict";
const fs = require("fs");
const path = require("path");
const archiver = require("archiver");
module.exports = function (bookPath, contentPath, outPath) {
return new Promise(function (resolve, reject) {
console.log(`Zipping up ${bookPath} into an EPUB`);
const archive = archiver("zip");
const destStream = fs.createWriteStream(outPath);
destStream.on("close", function () {
console.log(`EPUB written to ${outPath} (${archive.pointer()} bytes)`);
resolve();
});
archive.on("error", reject);
destStream.on("error", reject);
archive.pipe(destStream);
// Order matters; mimetype must be first for a valid EPUB
archive.file(path.resolve(bookPath, "mimetype"), { name: "mimetype" });
archive.directory(contentPath, "OEBPS", { name: "OEBPS" });
archive.directory(path.resolve(bookPath, "META-INF"), "META-INF", { name: "META-INF" });
archive.finalize();
});
};

View file

@ -19,6 +19,7 @@
"lint": "eslint lib && jscs lib"
},
"dependencies": {
"archiver": "^0.14.4",
"cpr": "^0.4.0",
"jsdom": "^5.3.0",
"mkdirp-then": "^1.0.1",