test to show enquirer's sigint issue
This commit is contained in:
parent
5a4f07ce70
commit
6b9420804b
2 changed files with 55 additions and 0 deletions
40
test/sigint-enquirer-raw.js
Normal file
40
test/sigint-enquirer-raw.js
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
// https://github.com/enquirer/enquirer/issues/372
|
||||||
|
import { prompt } from '../util.js';
|
||||||
|
|
||||||
|
const handleSIGINT = () => process.on('SIGINT', () => { // e.g. when killed by Ctrl-C
|
||||||
|
console.log('\nInterrupted by SIGINT. Exit!');
|
||||||
|
process.exitCode = 130;
|
||||||
|
});
|
||||||
|
handleSIGINT();
|
||||||
|
|
||||||
|
function onRawSIGINT(fn) {
|
||||||
|
const { stdin, stdout } = process;
|
||||||
|
stdin.setRawMode(true);
|
||||||
|
stdin.resume();
|
||||||
|
stdin.on('data', data => {
|
||||||
|
const key = data.toString('utf-8');
|
||||||
|
if (key === '\u0003') { // ctrl + c
|
||||||
|
fn();
|
||||||
|
} else {
|
||||||
|
stdout.write(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
onRawSIGINT(() => {
|
||||||
|
console.log('raw'); process.exit(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
console.log('hello');
|
||||||
|
console.error('hello error');
|
||||||
|
try {
|
||||||
|
let i = 'foo';
|
||||||
|
i = await prompt(); // SIGINT no longer handled if this is executed
|
||||||
|
// handleSIGINT();
|
||||||
|
console.log('value:', i);
|
||||||
|
setTimeout(() => console.log('timeout 3s'), 3000);
|
||||||
|
} catch (e) {
|
||||||
|
process.exitCode ||= 1;
|
||||||
|
console.log('catch. exitCode:', process.exitCode);
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
console.log('end. exitCode:', process.exitCode);
|
||||||
15
test/sigint-enquirer-simple.js
Normal file
15
test/sigint-enquirer-simple.js
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
// https://github.com/enquirer/enquirer/issues/372
|
||||||
|
import Enquirer from 'enquirer';
|
||||||
|
const enquirer = new Enquirer();
|
||||||
|
|
||||||
|
let interrupted = false;
|
||||||
|
process.on('SIGINT', () => {
|
||||||
|
if (interrupted) process.exit();
|
||||||
|
interrupted = true;
|
||||||
|
console.log('SIGINT');
|
||||||
|
});
|
||||||
|
await enquirer.prompt({
|
||||||
|
type: 'input',
|
||||||
|
name: 'username',
|
||||||
|
message: 'What is your username?',
|
||||||
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue