Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove zip, use archiver and rimraf to generate epub and cleanup #26

Merged
merged 7 commits into from
Aug 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -340,35 +344,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
68 changes: 31 additions & 37 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
2 changes: 1 addition & 1 deletion test.js

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