Skip to content

Commit 7b8d06b

Browse files
authored
Merge pull request #196 from Flakebi/exit-status
Exit with code 1 on error
2 parents b5d5b24 + 381d466 commit 7b8d06b

4 files changed

Lines changed: 33 additions & 7 deletions

File tree

‎src/index.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ async function main () {
140140
console.log(colors.red('Error: ' + err.toString()))
141141
})
142142

143-
await build(inputPath)
143+
const buildError = await build(inputPath)
144144

145-
if (options.buildOnce) {
146-
process.exit(0)
145+
if (program.buildOnce) {
146+
process.exit(buildError ? 1 : 0)
147147
} else {
148148
watch()
149149
}
@@ -200,10 +200,10 @@ async function build (filepath) {
200200
if (generatingPDF) {
201201
taskPromise = masterToPDF(inputPath, relaxedGlobals, tempHTMLPath, outputPath, locals)
202202
}
203-
await taskPromise
203+
const generateError = await taskPromise
204204
var duration = ((performance.now() - t0) / 1000).toFixed(2)
205205
console.log(colors.magenta.bold(`... Done in ${duration}s`))
206-
if (generatingPDF && relaxedGlobals.config.after) {
206+
if (generatingPDF && relaxedGlobals.config.after && !generateError) {
207207
console.log(colors.magenta.bold("Running 'after' command..."))
208208
var subprocess = exec(relaxedGlobals.config.after, cwd=relaxedGlobals.basedir)
209209

@@ -222,6 +222,7 @@ async function build (filepath) {
222222
console.log(colors.magenta.bold("...done running 'after' command."))
223223
}
224224
relaxedGlobals.busy = false
225+
return generateError
225226
}
226227

227228
/**

‎src/masterToPDF.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const filesize = require('filesize')
66
const path = require('path')
77
const { performance } = require('perf_hooks')
88

9+
// Returns undefined if successful or an error object on failure
910
exports.masterToPDF = async function (masterPath, relaxedGlobals, tempHTMLPath, outputPath, locals) {
1011
var t0 = performance.now()
1112
var page = relaxedGlobals.puppeteerPage
@@ -39,7 +40,7 @@ exports.masterToPDF = async function (masterPath, relaxedGlobals, tempHTMLPath,
3940
} catch (error) {
4041
console.log(error.message)
4142
console.error(colors.red('There was a Pug error (see above)'))
42-
return
43+
return error
4344
}
4445
} else if (masterPath.endsWith('.html')) {
4546
html = fs.readFileSync(masterPath, 'utf8')
@@ -84,7 +85,7 @@ exports.masterToPDF = async function (masterPath, relaxedGlobals, tempHTMLPath,
8485
'Increase the timeout by writing "pageRenderingTimeout: 60" ' +
8586
'at the top of your config.yml. Default is 30 (seconds).')
8687
}
87-
return
88+
return error
8889
}
8990

9091
var tLoad = performance.now()

‎test/samples/pug/error/master.pug‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
: Pug syntax error

‎test/test.js‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,29 @@ describe('Sample tests', function () {
110110
})
111111
})
112112

113+
describe('Error tests', function () {
114+
var tests = [
115+
{
116+
sampleName: 'error',
117+
timeout: 10000
118+
},
119+
]
120+
tests.forEach(function (test) {
121+
it('fails to render sample "' + test.sampleName + '"', function (done) {
122+
this.timeout(test.timeout)
123+
var basedir = path.join(__dirname, 'samples', 'pug', test.sampleName)
124+
var process = spawn(
125+
relaxed,
126+
[path.join(basedir, 'master.pug'), '--build-once', '--no-sandbox'].concat(test.cmdOptions || [])
127+
)
128+
process.on('close', function (code) {
129+
assert.equal(code, 1)
130+
done()
131+
})
132+
})
133+
})
134+
})
135+
113136
describe('Special rendering tests', function () {
114137
var tests = [
115138
{

0 commit comments

Comments
 (0)