Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
wooorm committed Aug 21, 2023
1 parent 55af0ed commit 150ffe7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
30 changes: 24 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ const ttyStream = new stream.Readable()
// @ts-expect-error: TS doesn’t understand but that’s how Node streams work.
ttyStream.isTTY = true

/**
* Exit, lazily, with the correct exit status code.
*
* @type {number | undefined}
*/
let exitCode

process.on('exit', onexit)

// Handle uncaught errors, such as from unexpected async behaviour.
process.on('uncaughtException', fail)

Expand Down Expand Up @@ -107,7 +116,7 @@ export function args(options) {
clean()
fail(error)
} else {
if (typeof code === 'number') process.exitCode = code
if (typeof code === 'number') exitCode = code

if (state.args.watch && !watcher && context) {
subscribe(context)
Expand Down Expand Up @@ -157,20 +166,17 @@ export function args(options) {
* Handle a SIGINT.
*/
function onsigint() {
console.log('n1')
// Hide the `^C` in terminal.
process.stderr.write('\n', noop)
console.log('n2')

clean()

console.log('n3')
// Do another process if `output` specified regeneration.
if (output === true) {
console.log('n4')
state.engine.output = output
state.args.watch = false
engine(state.engine, done)
onexit()
}
}
}
Expand All @@ -184,10 +190,22 @@ export function args(options) {
* Nothing.
*/
function fail(error) {
process.exitCode = 1
exitCode = 1
process.stderr.write(String(error.stack || error).trimEnd() + '\n', noop)
}

/**
* Set `exitCode`.
*
* @returns {undefined}
* Nothing.
*/
function onexit() {
// eslint-disable-next-line unicorn/no-process-exit
process.exit(exitCode)
// . process.exitCode = exitCode
}

/**
* Do nothing.
*
Expand Down
9 changes: 9 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,15 @@ test('args', async function (t) {
assert.equal(resolved, false)
console.log('z2')
proc.kill('SIGINT')
setTimeout(check, 500)
}

function check() {
if (!resolved) {
proc.kill()
}

assert.equal(resolved, true)
}
})
})
Expand Down

0 comments on commit 150ffe7

Please sign in to comment.