Skip to content

Instantly share code, notes, and snippets.

@ncwhale
Last active October 10, 2020 09:41
Show Gist options
  • Save ncwhale/01e64d6097f92b095d1d725e99c82dac to your computer and use it in GitHub Desktop.
Save ncwhale/01e64d6097f92b095d1d725e99c82dac to your computer and use it in GitHub Desktop.
Node.js script for pack .epub resources.
const fs = require('fs')
const path = require('path')
const archiver = require('archiver')
var base_path = './epubs'
async function main() {
const dir = await fs.promises.opendir(base_path)
const books = {}
for await (const ent of dir) {
if (ent.isDirectory()) {
books[ent.name] = false
}
}
for (name in books) {
const output = fs.createWriteStream(path.join(base_path, `${name}.epub`))
const archive = archiver('zip', {
zlib: { level: 9 }
})
output.on('close', () => {
console.log(`${name}.epub ${archive.pointer()} bytes`)
})
output.on('error', (err) => {
throw err;
})
archive.pipe(output)
archive.append("application/epub+zip", { name: 'mimetype', store: true })
archive.directory(path.join(base_path, name), false)
archive.finalize()
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment