Skip to content

Commit 3d3823f

Browse files
darahakruyadorno
authored andcommitted
Exit when ESC is pressed (#10)
* Added exit on ESC key pressed. * Moved ESC handler + added test.
1 parent 60f06c8 commit 3d3823f

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ module.exports = function (p, exec, log, cwd, tasks, options) {
8686
choices: promptChoices
8787
}
8888
};
89+
90+
p.stdin.setEncoding('utf8');
91+
p.stdin.on('data', function (chunk) {
92+
if (chunk === '\u001b') { // ESC
93+
p.exit(0);
94+
}
95+
});
96+
8997
return prompt(
9098
options.multiple ? promptTypes.multiple : promptTypes.base,
9199
onPrompt
@@ -100,4 +108,3 @@ module.exports = function (p, exec, log, cwd, tasks, options) {
100108
return showList();
101109
}
102110
};
103-

test/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,3 +243,14 @@ test.cb(function shouldExitWithErrorMsgOnNoPackageJson(t) {
243243
});
244244
});
245245

246+
test.cb(function shouldExitWithCodeZeroOnPressedESC(t) {
247+
var run = spawn('node', ['../cli.js', './fixtures'], {
248+
cwd: cwd
249+
});
250+
run.on('close', function (code) {
251+
t.is(code, 0);
252+
t.end();
253+
});
254+
run.stdin.write('\u001b');
255+
run.stdin.end();
256+
});

0 commit comments

Comments
 (0)