17
index.js
'use strict'; | ||
var readline = require('readline'); | ||
var rl; | ||
module.exports = function () { | ||
// don't unref if it's already listened to | ||
if (process.stdin.listeners('keypress').length === 0) { | ||
process.stdin.unref(); | ||
} | ||
var rl = readline.createInterface({ | ||
rl = readline.createInterface({ | ||
input: process.stdin, | ||
@@ -15,5 +11,4 @@ output: process.stdout | ||
process.stdin.on('keypress', function(s, key) { | ||
if (key.name === 'escape') { | ||
process.stdin.on('keypress', function(ch, key) { | ||
if (key && key.name === 'escape') { | ||
process.exit(); | ||
@@ -23,1 +18,5 @@ } | ||
}; | ||
module.exports.done = function () { | ||
rl.close(); | ||
}; |
{ | ||
"name": "esc-exit", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Exit the process when the `esc` key is pressed", | ||
@@ -16,3 +16,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "mocha" | ||
"test": "mocha && node test-done.js" | ||
}, | ||
@@ -19,0 +19,0 @@ "files": [ |
@@ -24,4 +24,17 @@ # esc-exit [](https://travis-ci.org/sindresorhus/esc-exit) | ||
## API | ||
Using this prevents the process from exiting normally. | ||
There are multiple ways you can handle this: | ||
- Call `escExit.done()` when you're done listening. It will then let the process exit normally. | ||
- Use `process.stdin.unref()` if you don't intend to use `procces.stdin` afterwards. It will let the process exit normally. | ||
- Call `process.exit()` to exit the process directly. | ||
## License | ||
MIT © [Sindre Sorhus](http://sindresorhus.com) |
1834
23%40
48.15%