From 48ddecfed46a0d220eb90027fd22965f63cfc3fa Mon Sep 17 00:00:00 2001 From: Domenic Denicola Date: Thu, 21 May 2015 00:58:56 -0400 Subject: [PATCH] Implement zip step --- .gitignore | 1 + lib/worm-scraper.js | 3 ++- lib/zip.js | 30 ++++++++++++++++++++++++++++++ package.json | 1 + 4 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 lib/zip.js diff --git a/.gitignore b/.gitignore index 74f03f7..29d3eed 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ cache/ book/ +Worm.epub diff --git a/lib/worm-scraper.js b/lib/worm-scraper.js index 8a3d901..05b78ae 100644 --- a/lib/worm-scraper.js +++ b/lib/worm-scraper.js @@ -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)); }); } diff --git a/lib/zip.js b/lib/zip.js new file mode 100644 index 0000000..929b229 --- /dev/null +++ b/lib/zip.js @@ -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(); + }); +}; diff --git a/package.json b/package.json index 778a7ab..3a9e1ce 100644 --- a/package.json +++ b/package.json @@ -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",