Skip to content

Commit

Permalink
Merge pull request #26 from jpittner/remove_zip
Browse files Browse the repository at this point in the history
remove zip, use archiver and rimraf to generate epub and cleanup
  • Loading branch information
pedrosanta authored Aug 16, 2016
2 parents 4b419b9 + 5557387 commit 79f313f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 61 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ It's very fast, except the time to download images from the web.

## Usage

First, make sure you have `zip` installed. then cd to your project dir, run:
Install the lib and add it as a dependency (recommended), run on your project dir:

npm install epub-gen
npm install epub-gen --save

Then put this in your code:

Expand Down
44 changes: 21 additions & 23 deletions lib/index.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ request = require "superagent"
fsextra = require "fs-extra"
removeDiacritics = require("diacritics").remove
mime = require "mime"
archiver = require "archiver"

# provides rm -rf for deleting temp directory across various platforms.
rimraf = require "rimraf"

uuid = ->
'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace /[xy]/g, (c)->
Expand Down Expand Up @@ -337,35 +341,29 @@ class EPub

genEpub: ()->
# Thanks to Paul Bradley
# http://www.bradleymedia.org/gzip-markdown-epub/
# http://www.bradleymedia.org/gzip-markdown-epub/ (404 as of 28.07.2016)

genDefer = new Q.defer()

self = @
filename = "book.epub.zip"
initCmd = "zip -q -X -0 #{filename} mimetype"
zipCmd = "zip -q -X -9 -r #{filename} * -x mimetype #{filename}"
cleanUp = "mv #{filename} book.epub && rm -f -r META-INF OEBPS mimetype"
cleanUp = "mv #{filename} book.epub"
cwd = @uuid
self.runCommand(initCmd, {cwd}).then ()->
self.runCommand(zipCmd, {cwd}).then ()->
self.runCommand(cleanUp, {cwd}).then ()->
stream = fs.createReadStream( path.resolve self.uuid, "book.epub" )
stream.pipe fs.createWriteStream self.options.output
stream.on "error", (err)->
console.error(err)
self.defer.reject(err)
stream.on "end", ()->
self.defer.resolve()
currentDir = self.options.tempDir
self.runCommand("rm -f -r #{self.id}/", {cwd: currentDir})
, (err)->

archive = archiver("zip", {zlib: {level: 9}})
output = fs.createWriteStream self.options.output
console.log "Zipping temp dir to", self.options.output
archive.file(cwd + "/mimetype", {store:true, name:"mimetype"})
archive.directory cwd + "/META-INF", "META-INF"
archive.directory cwd + "/OEBPS", "OEBPS"
archive.pipe output
archive.on "end", ()->
console.log "Done zipping, clearing temp dir..."
rimraf cwd, (err)->
if err
genDefer.reject(err)
, (err)->
genDefer.reject(err)
, (err)->
genDefer.reject(err)
else
genDefer.resolve()
archive.on "error", (err) -> genDefer.reject(err)
archive.finalize()

genDefer.promise

Expand Down
66 changes: 30 additions & 36 deletions lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
},
"homepage": "/~https://github.com/cyrilis/epub-gen",
"dependencies": {
"archiver": "^1.0.1",
"rimraf": "^2.5.2",
"cheerio": "^0.18.0",
"diacritics": "^1.2.1",
"ejs": "^2.2.4",
Expand Down

0 comments on commit 79f313f

Please sign in to comment.