From fcde071a87fca0cf9edbf69958e3e36fa141c28d Mon Sep 17 00:00:00 2001 From: Qingyu Deng Date: Wed, 5 May 2021 17:55:36 +0800 Subject: [PATCH] globals: introduce `prompt` on global Fixes: https://github.com/nodejs/node/issues/37925 --- .eslintrc.js | 1 + doc/api/globals.md | 20 ++++++++++++ lib/internal/bootstrap/node.js | 55 +++++++++++++++++++++++++++++++++ test/common/index.js | 4 +++ test/parallel/test-global.js | 1 + test/pseudo-tty/test-prompt.in | 2 ++ test/pseudo-tty/test-prompt.js | 12 +++++++ test/pseudo-tty/test-prompt.out | 3 ++ 8 files changed, 98 insertions(+) create mode 100644 test/pseudo-tty/test-prompt.in create mode 100644 test/pseudo-tty/test-prompt.js create mode 100644 test/pseudo-tty/test-prompt.out diff --git a/.eslintrc.js b/.eslintrc.js index 27dd8aa10c37a1..399c48639f747b 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -333,5 +333,6 @@ module.exports = { btoa: 'readable', atob: 'readable', performance: 'readable', + prompt: 'readable' }, }; diff --git a/doc/api/globals.md b/doc/api/globals.md index ea764a20fb30e2..19d7f67740ed82 100644 --- a/doc/api/globals.md +++ b/doc/api/globals.md @@ -294,6 +294,26 @@ added: v0.1.7 The process object. See the [`process` object][] section. +## `prompt([message[, defaultValue]])` + + + + +* `message` {string} Optional, Message shown to user. Default: `'prompt'`. +* `defaultValue` {string} Optional, Returned value when user inputs the + empty string. +* Returns: {string|null} + +`prompt()` the given message and waits for the user's input. +If the `stdin` is not interactive, it returns `null`. +If default value is not given and the user's input is empty string, it also +returns `null`. +If default value is given and the user's input is empty string, it returns +`defaultValue`. +Otherwise, it returns the user's input as `string`. + ## `queueMicrotask(callback)`