-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from GreatWizard/chore/better-errors
chore: handle better build errors
- Loading branch information
Showing
5 changed files
with
54 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) | ||
} |