Comparing version 1.0.7 to 2.0.0
147
lib/read.js
@@ -0,30 +1,27 @@ | ||
const readline = require('readline') | ||
const Mute = require('mute-stream') | ||
module.exports = read | ||
var readline = require('readline') | ||
var Mute = require('mute-stream') | ||
function read (opts, cb) { | ||
if (opts.num) { | ||
throw new Error('read() no longer accepts a char number limit') | ||
} | ||
if (typeof opts.default !== 'undefined' && | ||
typeof opts.default !== 'string' && | ||
typeof opts.default !== 'number') { | ||
module.exports = async function read ({ | ||
default: def = '', | ||
input = process.stdin, | ||
output = process.stdout, | ||
prompt = '', | ||
silent, | ||
timeout, | ||
edit, | ||
terminal, | ||
replace, | ||
}) { | ||
if (typeof def !== 'undefined' && typeof def !== 'string' && typeof def !== 'number') { | ||
throw new Error('default value must be string or number') | ||
} | ||
var input = opts.input || process.stdin | ||
var output = opts.output || process.stdout | ||
var prompt = (opts.prompt || '').trim() + ' ' | ||
var silent = opts.silent | ||
var editDef = false | ||
var timeout = opts.timeout | ||
let editDef = false | ||
prompt = prompt.trim() + ' ' | ||
terminal = !!(terminal || output.isTTY) | ||
var def = opts.default || '' | ||
if (def) { | ||
if (silent) { | ||
prompt += '(<default hidden>) ' | ||
} else if (opts.edit) { | ||
} else if (edit) { | ||
editDef = true | ||
@@ -35,80 +32,52 @@ } else { | ||
} | ||
var terminal = !!(opts.terminal || output.isTTY) | ||
var m = new Mute({ replace: opts.replace, prompt: prompt }) | ||
m.pipe(output, {end: false}) | ||
const m = new Mute({ replace, prompt }) | ||
m.pipe(output, { end: false }) | ||
output = m | ||
var rlOpts = { input: input, output: output, terminal: terminal } | ||
if (process.version.match(/^v0\.6/)) { | ||
var rl = readline.createInterface(rlOpts.input, rlOpts.output) | ||
} else { | ||
var rl = readline.createInterface(rlOpts) | ||
} | ||
return new Promise((resolve, reject) => { | ||
const rl = readline.createInterface({ input, output, terminal }) | ||
const timer = timeout && setTimeout(() => onError(new Error('timed out')), timeout) | ||
output.unmute() | ||
rl.setPrompt(prompt) | ||
rl.prompt() | ||
output.unmute() | ||
rl.setPrompt(prompt) | ||
rl.prompt() | ||
if (silent) { | ||
output.mute() | ||
} else if (editDef) { | ||
rl.line = def | ||
rl.cursor = def.length | ||
rl._refreshLine() | ||
} | ||
if (silent) { | ||
output.mute() | ||
} else if (editDef) { | ||
rl.line = def | ||
rl.cursor = def.length | ||
rl._refreshLine() | ||
} | ||
var called = false | ||
rl.on('line', onLine) | ||
rl.on('error', onError) | ||
const done = () => { | ||
rl.close() | ||
clearTimeout(timer) | ||
output.mute() | ||
output.end() | ||
} | ||
rl.on('SIGINT', function () { | ||
rl.close() | ||
onError(new Error('canceled')) | ||
}) | ||
var timer | ||
if (timeout) { | ||
timer = setTimeout(function () { | ||
onError(new Error('timed out')) | ||
}, timeout) | ||
} | ||
function done () { | ||
called = true | ||
rl.close() | ||
if (process.version.match(/^v0\.6/)) { | ||
rl.input.removeAllListeners('data') | ||
rl.input.removeAllListeners('keypress') | ||
rl.input.pause() | ||
const onError = (er) => { | ||
done() | ||
reject(er) | ||
} | ||
clearTimeout(timer) | ||
output.mute() | ||
output.end() | ||
} | ||
rl.on('error', onError) | ||
rl.on('line', (line) => { | ||
if (silent && terminal) { | ||
output.unmute() | ||
output.write('\r\n') | ||
} | ||
done() | ||
// truncate the \n at the end. | ||
const res = line.replace(/\r?\n$/, '') || def || '' | ||
return resolve(res) | ||
}) | ||
function onError (er) { | ||
if (called) return | ||
done() | ||
return cb(er) | ||
} | ||
function onLine (line) { | ||
if (called) return | ||
if (silent && terminal) { | ||
output.unmute() | ||
output.write('\r\n') | ||
} | ||
done() | ||
// truncate the \n at the end. | ||
line = line.replace(/\r?\n$/, '') | ||
var isDefault = !!(editDef && line === def) | ||
if (def && !line) { | ||
isDefault = true | ||
line = def | ||
} | ||
cb(null, line, isDefault) | ||
} | ||
rl.on('SIGINT', () => { | ||
rl.close() | ||
onError(new Error('canceled')) | ||
}) | ||
}) | ||
} |
{ | ||
"name": "read", | ||
"version": "1.0.7", | ||
"version": "2.0.0", | ||
"main": "lib/read.js", | ||
"dependencies": { | ||
"mute-stream": "~0.0.4" | ||
"mute-stream": "~1.0.0" | ||
}, | ||
"devDependencies": { | ||
"tap": "^1.2.0" | ||
"@npmcli/eslint-config": "^4.0.0", | ||
"@npmcli/template-oss": "4.11.0", | ||
"tap": "^16.3.0" | ||
}, | ||
"engines": { | ||
"node": ">=0.8" | ||
"node": "^14.17.0 || ^16.13.0 || >=18.0.0" | ||
}, | ||
"author": "Isaac Z. Schlueter <i@izs.me> (http://blog.izs.me/)", | ||
"author": "GitHub Inc.", | ||
"description": "read(1) for node programs", | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/isaacs/read.git" | ||
"url": "https://github.com/npm/read.git" | ||
}, | ||
"license": "ISC", | ||
"scripts": { | ||
"test": "tap test/*.js" | ||
"test": "tap", | ||
"lint": "eslint \"**/*.js\"", | ||
"postlint": "template-oss-check", | ||
"template-oss-apply": "template-oss-apply --force", | ||
"lintfix": "npm run lint -- --fix", | ||
"snap": "tap", | ||
"posttest": "npm run lint" | ||
}, | ||
"files": [ | ||
"lib/read.js" | ||
] | ||
"bin/", | ||
"lib/" | ||
], | ||
"templateOSS": { | ||
"//@npmcli/template-oss": "This file is partially managed by @npmcli/template-oss. Edits may be overwritten.", | ||
"version": "4.11.0" | ||
}, | ||
"tap": { | ||
"statements": 77, | ||
"branches": 75, | ||
"functions": 57, | ||
"lines": 78, | ||
"test-ignore": "fixtures/", | ||
"nyc-arg": [ | ||
"--exclude", | ||
"tap-snapshots/**" | ||
] | ||
} | ||
} |
@@ -8,15 +8,15 @@ ## read | ||
## USAGE | ||
## Usage | ||
```javascript | ||
var read = require("read") | ||
read(options, callback) | ||
try { | ||
const result = await read(options, callback) | ||
} catch (er) { | ||
console.error(er) | ||
} | ||
``` | ||
The callback gets called with either the user input, or the default | ||
specified, or an error, as `callback(error, result, isDefault)` | ||
node style. | ||
## Options | ||
## OPTIONS | ||
Every option is optional. | ||
@@ -32,3 +32,3 @@ | ||
* `input` Readable stream to get input data from. (default `process.stdin`) | ||
* `output` Writeable stream to write prompts to. (default: `process.stdout`) | ||
* `output` Writable stream to write prompts to. (default: `process.stdout`) | ||
@@ -38,19 +38,4 @@ If silent is true, and the input is a TTY, then read will set raw | ||
## COMPATIBILITY | ||
## Contributing | ||
This module works sort of with node 0.6. It does not work with node | ||
versions less than 0.6. It is best on node 0.8. | ||
On node version 0.6, it will remove all listeners on the input | ||
stream's `data` and `keypress` events, because the readline module did | ||
not fully clean up after itself in that version of node, and did not | ||
make it possible to clean up after it in a way that has no potential | ||
for side effects. | ||
Additionally, some of the readline options (like `terminal`) will not | ||
function in versions of node before 0.8, because they were not | ||
implemented in the builtin readline module. | ||
## CONTRIBUTING | ||
Patches welcome. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
4699
3
71
39
+ Addedmute-stream@1.0.0(transitive)
- Removedmute-stream@0.0.8(transitive)
Updatedmute-stream@~1.0.0