Commit d0d04c26 authored by Alf Eaton's avatar Alf Eaton

Wrap file stream handling in a Promise

parent 430f7ae3
......@@ -76,21 +76,35 @@ class HTMLEPUB {
archive.append(item.$.xml(), { name: 'EPUB/' + item.target })
})
// EPUB/images/* - the images
epub.images.forEach(item => {
const inputStream = fs.createReadStream(item.source)
archive.append(inputStream, { name: 'EPUB/' + item.target })
})
// open a file stream and add it to the archive
const appendFile = item => new Promise((resolve, reject) => {
const stream = fs.createReadStream(item.source)
stream.on('error', onError)
stream.on('readable', onReadable)
function onError (err) {
reject(err)
}
// EPUB/styles/* - the styles
epub.styles.forEach(item => {
const inputStream = fs.createReadStream(item.source)
archive.append(inputStream, { name: 'EPUB/' + item.target })
function onReadable () {
stream.removeListener('readable', onReadable)
stream.removeListener('error', onError)
archive.append(stream, { name: 'EPUB/' + item.target })
resolve()
}
})
archive.finalize()
Promise.all([
// EPUB/images/* - the images
Promise.all(epub.images.map(appendFile)),
// EPUB/styles/* - the styles
Promise.all(epub.styles.map(appendFile))
]).then(() => {
archive.finalize()
resolve(archive)
resolve(archive)
}).catch(reject)
})
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment