2016-08-15 08:42:57 +12:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2017-10-19 02:02:07 +13:00
|
|
|
// Fix for mithril
|
|
|
|
const noop = () => {}
|
|
|
|
global.window = {
|
|
|
|
document: { createDocumentFragment: noop },
|
|
|
|
history: { pushState: noop }
|
|
|
|
}
|
|
|
|
|
2016-08-15 08:42:57 +12:00
|
|
|
const FimFic2Epub = require('../fimfic2epub')
|
2016-08-15 21:11:20 +12:00
|
|
|
const fs = require('fs')
|
2016-08-15 08:42:57 +12:00
|
|
|
|
|
|
|
const STORY_ID = process.argv[2]
|
|
|
|
|
|
|
|
const ffc = new FimFic2Epub(STORY_ID)
|
|
|
|
|
2016-08-15 21:11:20 +12:00
|
|
|
const outputStdout = process.argv[3] === '-' || process.argv[3] === '/dev/stdout'
|
|
|
|
|
|
|
|
if (outputStdout) {
|
|
|
|
console.log = console.error
|
|
|
|
console.log('Outputting to stdout')
|
|
|
|
}
|
|
|
|
|
2016-08-25 00:47:48 +12:00
|
|
|
ffc.on('progress', (percent, status) => {
|
|
|
|
if (status) {
|
|
|
|
console.log('fimfic2epub: ' + status)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
ffc.fetchAll()
|
2016-08-24 02:32:55 +12:00
|
|
|
.then(ffc.build.bind(ffc))
|
2016-08-15 21:11:20 +12:00
|
|
|
.then(() => {
|
|
|
|
let filename = process.argv[3] || ffc.filename
|
|
|
|
let stream
|
|
|
|
|
|
|
|
if (outputStdout) {
|
|
|
|
stream = process.stdout
|
|
|
|
} else {
|
|
|
|
stream = fs.createWriteStream(filename)
|
|
|
|
}
|
2017-10-19 02:02:07 +13:00
|
|
|
ffc.streamFile(null)
|
2016-08-15 21:11:20 +12:00
|
|
|
.pipe(stream)
|
|
|
|
.on('finish', () => {
|
|
|
|
if (!outputStdout) {
|
|
|
|
console.log('Saved story as ' + filename)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2016-08-30 02:20:20 +12:00
|
|
|
if (err && err.stack) {
|
|
|
|
console.error(err.stack)
|
|
|
|
} else {
|
|
|
|
console.error('Error: ' + (err || 'Unknown error'))
|
|
|
|
}
|
2016-08-15 21:11:20 +12:00
|
|
|
process.exit(1)
|
2016-08-15 08:42:57 +12:00
|
|
|
})
|