Skip to content

Commit

Permalink
chore: handle better build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatWizard committed Jun 28, 2024
1 parent 0589d2d commit 7ff4c02
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 26 deletions.
11 changes: 10 additions & 1 deletion src/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const os = require('os')
const path = require('path')
const CleanCSS = require('clean-css')
const yaml = require('js-yaml')
const { log } = require('./message')

const nodePandoc = require('./node-pandoc-promise')

Expand All @@ -20,6 +21,7 @@ const {
opfSubstitutions,
appendExtraMetadata,
} = require('./edition')
const { build } = require('joi')

Check failure on line 24 in src/build.js

View workflow job for this annotation

GitHub Actions / Tests

'build' is assigned a value but never used

const FORMATS = [
{ name: 'LaTeX', extension: 'tex' },
Expand Down Expand Up @@ -87,6 +89,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 +184,11 @@ module.exports = async (config, options = {}) => {
args.push('-o', outputFile)

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

if (options.debug) {
log(buildLogs)
}

if (config.format === 'epub' || config.format === 'mobi') {
// Automatically edit epub: apply substitutions, append extra metadata
Expand Down
3 changes: 3 additions & 0 deletions src/kindlegen.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ module.exports.epubToMobi = (input, output, options = {}) => {
opts.push('-verbose')
}
opts.push(input)
if (options.debug) {
log(`${kindlegenPath} ${opts.join(' ')}`)
}
let kindlegen = spawn(kindlegenPath, opts)
kindlegen.on('close', async (code) => {
if (code !== 0 && code !== 1) {
Expand Down
51 changes: 26 additions & 25 deletions src/node-pandoc-promise.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
const stat = require('fs').stat
const spawn = require('child_process').spawn
const { log } = require('./message')

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

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

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

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

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

onStdOutEnd = function () {
resolve(result)
const onErr = function (data) {
error += data
}

onStdErrData = function (err) {
reject(new Error(err))
const onEnd = function () {
if (error !== '') {
reject(new Error(error))
} else {
resolve(result)
}
}

onStatCheck = function (err, stats) {
// Check if src is URL match.
const isURL = /^(https?|ftp):\/\//i.test(src)

const 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)
pdSpawn.stdout.on('data', onOut)
pdSpawn.stdout.on('end', onEnd)
pdSpawn.stderr.on('data', onErr)
pdSpawn.stderr.on('end', onEnd)
}

// Check if src is URL match.
isURL = isURL(src)

// Check file status of src
stat(src, onStatCheck)
})
Expand Down

0 comments on commit 7ff4c02

Please sign in to comment.