summaryrefslogtreecommitdiff
path: root/deps/npm/node_modules/read/lib/read.js
diff options
context:
space:
mode:
authornpm CLI robot <npm-cli+bot@github.com>2023-02-18 17:09:39 -0500
committerGitHub <noreply@github.com>2023-02-18 22:09:39 +0000
commit005170c95d764228946ce014a0d437716d5d5b91 (patch)
treed16918ff4adf7c4d7a548e1587d5485fd7f83eb9 /deps/npm/node_modules/read/lib/read.js
parent070246f9fa587aa4e0bfcca34bb642711dfdb0b2 (diff)
downloadnode-new-005170c95d764228946ce014a0d437716d5d5b91.tar.gz
deps: upgrade npm to 9.5.0
PR-URL: https://github.com/nodejs/node/pull/46673 Reviewed-By: Luke Karrys <luke@lukekarrys.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il> Reviewed-By: Ruy Adorno <ruyadorno@google.com> Reviewed-By: Tobias Nießen <tniessen@tnie.de> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Myles Borins <myles.borins@gmail.com>
Diffstat (limited to 'deps/npm/node_modules/read/lib/read.js')
-rw-r--r--deps/npm/node_modules/read/lib/read.js151
1 files changed, 60 insertions, 91 deletions
diff --git a/deps/npm/node_modules/read/lib/read.js b/deps/npm/node_modules/read/lib/read.js
index a93d1b3b53..882b11c285 100644
--- a/deps/npm/node_modules/read/lib/read.js
+++ b/deps/npm/node_modules/read/lib/read.js
@@ -1,113 +1,82 @@
-
-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') {
+const readline = require('readline')
+const Mute = require('mute-stream')
+
+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
} else {
prompt += '(' + def + ') '
}
}
- 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)
- }
-
-
- output.unmute()
- rl.setPrompt(prompt)
- rl.prompt()
- 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)
+ return new Promise((resolve, reject) => {
+ const rl = readline.createInterface({ input, output, terminal })
+ const timer = timeout && setTimeout(() => onError(new Error('timed out')), timeout)
- rl.on('SIGINT', function () {
- rl.close()
- onError(new Error('canceled'))
- })
+ output.unmute()
+ rl.setPrompt(prompt)
+ rl.prompt()
- 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()
+ if (silent) {
+ output.mute()
+ } else if (editDef) {
+ rl.line = def
+ rl.cursor = def.length
+ rl._refreshLine()
}
- clearTimeout(timer)
- output.mute()
- output.end()
- }
-
- 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')
+ const done = () => {
+ rl.close()
+ clearTimeout(timer)
+ output.mute()
+ output.end()
}
- done()
- // truncate the \n at the end.
- line = line.replace(/\r?\n$/, '')
- var isDefault = !!(editDef && line === def)
- if (def && !line) {
- isDefault = true
- line = def
+
+ const onError = (er) => {
+ done()
+ reject(er)
}
- cb(null, line, isDefault)
- }
+
+ 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)
+ })
+
+ rl.on('SIGINT', () => {
+ rl.close()
+ onError(new Error('canceled'))
+ })
+ })
}