Skip to content

Commit

Permalink
Merge pull request #56 from GreatWizard/chore/better-errors
Browse files Browse the repository at this point in the history
chore: handle better build errors
  • Loading branch information
GreatWizard authored Jun 28, 2024
2 parents 0589d2d + 27f170f commit 16be5ee
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 42 deletions.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ jobs:
distribution: 'temurin'
java-version: '17'
- run: |
sudo apt-get install -y texlive texlive-xetex
PANDOC_URL=$(curl https://api.github.com/repos/jgm/pandoc/releases/latest | jq -r ".assets[] | select(.name | test(\"amd64.deb$\")) | .browser_download_url")
curl --silent --show-error --location --fail --retry 4 --retry-delay 5 --output pandoc.deb $PANDOC_URL
sudo dpkg -i pandoc.deb
Expand Down
5 changes: 4 additions & 1 deletion src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ module.exports = async (config, options = {}) => {

let args = config.files

// Suppress warning messages
args.push('--quiet')

if (config.format === 'epub' || config.format === 'mobi') {
// add stylesheet
let cssContent = ''
Expand Down Expand Up @@ -179,7 +182,7 @@ module.exports = async (config, options = {}) => {
args.push('-o', outputFile)

// compile with pandoc
await nodePandoc('', args, { cwd })
await nodePandoc('', args, { cwd }, undefined, { debug: options.debug })

if (config.format === 'epub' || config.format === 'mobi') {
// Automatically edit epub: apply substitutions, append extra metadata
Expand Down
15 changes: 12 additions & 3 deletions src/kindlegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { spawn } = require('child_process')
const tar = require('tar')
const unzipper = require('unzipper')

const { log, warn } = require('./message')
const { debug, log, warn } = require('./message')
const { humanizePlatformArch } = require('./utils')

const kindlegenFilename = () => (process.platform === 'win32' ? 'kindlegen.exe' : 'kindlegen')
Expand Down Expand Up @@ -95,12 +95,21 @@ const extractKindleGen = (file) => {
module.exports.epubToMobi = (input, output, options = {}) => {
let kindlegenPath = options.kindlegenPath || localKindlegenPath()
return new Promise((resolve, reject) => {
let opts = ['-c2', '-dont_append_source']
const opts = ['-c2', '-dont_append_source']

if (options.debug) {
opts.push('-verbose')
}

opts.push(input)
let kindlegen = spawn(kindlegenPath, opts)

if (options.debug) {
debug('*** mobi build')
debug(`${kindlegenPath} ${opts.join(' ')}`)
}

const kindlegen = spawn(kindlegenPath, opts)

kindlegen.on('close', async (code) => {
if (code !== 0 && code !== 1) {
reject(`KindleGen returned error ${code}`)
Expand Down
4 changes: 4 additions & 0 deletions src/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ module.exports.error = (message, icon = '📕') => {
console.error(chalk.red(`${icon} ${message}`))
}

module.exports.debug = (message, icon = '⚙️') => {
console.log(chalk.gray(`${icon} ${message}`))
}

module.exports.log = (message = '') => {
console.log(message)
}
71 changes: 33 additions & 38 deletions src/node-pandoc-promise.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,53 @@
const stat = require('fs').stat
const spawn = require('child_process').spawn
const { debug, log } = require('./message')

module.exports = function (src, args = [], options = {}, pandocPath = 'pandoc') {
return new Promise((resolve, reject) => {
let pdSpawn
let result = ''
let isURL

// Event Handlers
let onStdOutData
let onStdOutEnd
let onStdErrData
let onStatCheck

isURL = function (src) {
return /^(https?|ftp):\/\//i.test(src)
}

onStdOutData = function (data) {
result += data
}

onStdOutEnd = function () {
resolve(result)
}
module.exports = function (src, args = [], spawnOptions = {}, pandocPath = 'pandoc', options = {}) {
if (options.debug) {
debug('*** pandoc build')
debug(JSON.stringify(spawnOptions, 0, 2))
debug(`${pandocPath} ${args.join(' ')}`)
}

onStdErrData = function (err) {
reject(new Error(err))
}
return new Promise((resolve, reject) => {
// Check file status of src
stat(src, (_err, stats) => {
// Check if src is URL match.
const isURL = /^(https?|ftp):\/\//i.test(src)

onStatCheck = function (err, stats) {
// If src is a file or valid web URL, push the src back into args array
if ((stats && stats.isFile()) || isURL) {
args.unshift(src)
}

// Create child_process.spawn
pdSpawn = spawn(pandocPath, args, options)
const pdSpawn = spawn(pandocPath, args, spawnOptions)

// If src is not a file, assume a string input.
if (typeof stats === 'undefined' && !isURL) {
pdSpawn.stdin.end(src, 'utf-8')
}

// Set handlers...
pdSpawn.stdout.on('data', onStdOutData)
pdSpawn.stdout.on('end', onStdOutEnd)
pdSpawn.on('error', onStdErrData)
}
let result = ''
let error = ''

// Check if src is URL match.
isURL = isURL(src)
pdSpawn.stdout.on('data', (data) => {
result += data
if (options.debug) {
log(data)
}
})

// Check file status of src
stat(src, onStatCheck)
pdSpawn.stderr.on('data', (data) => {
error += data
})

pdSpawn.on('close', (code) => {
if (code !== 0 || error !== '') {
reject(new Error(`pandoc returned error ${code}: ${error}`))
}

resolve(result)
})
})
})
}

0 comments on commit 16be5ee

Please sign in to comment.