Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
feat: default for prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed May 6, 2018
1 parent f0a71d4 commit b136fce
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 25 deletions.
9 changes: 6 additions & 3 deletions examples/prompt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cli } from '../src'
import {cli} from '../src'

const wait = (ms = 400) => new Promise(resolve => setTimeout(resolve, ms))

Expand All @@ -9,10 +9,13 @@ async function run() {
cli.action.start('working')
await wait()
cli.log(`you entered: ${input}`)
input = await cli.prompt('your name (mask)', { type: 'mask' })
input = await cli.prompt('your name (mask)', {type: 'mask'})
await wait()
cli.log(`you entered: ${input}`)
input = await cli.prompt('your name (hide)', { type: 'hide' })
input = await cli.prompt('your name (hide)', {type: 'hide'})
await wait()
cli.log(`you entered: ${input}`)
input = await cli.prompt('your name (default)', {default: 'somedefault'})
await wait()
cli.action.stop()
cli.log(`you entered: ${input}`)
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"fs-extra": "^6.0.0",
"indent-string": "^3.2.0",
"lodash": "^4.17.10",
"password-prompt": "^1.0.4",
"password-prompt": "^1.0.5",
"semver": "^5.5.0",
"strip-ansi": "^4.0.0",
"supports-color": "^5.4.0"
Expand All @@ -30,7 +30,7 @@
"@types/indent-string": "^3.0.0",
"@types/lodash": "^4.14.108",
"@types/mocha": "^5.2.0",
"@types/node": "^10.0.2",
"@types/node": "^10.0.4",
"@types/semver": "^5.5.0",
"@types/strip-ansi": "^3.0.0",
"@types/supports-color": "^5.3.0",
Expand All @@ -41,8 +41,8 @@
"fancy-test": "^1.0.6",
"husky": "^0.14.3",
"mocha": "^5.1.1",
"ts-node": "^6.0.2",
"tslint": "^5.9.1",
"ts-node": "^6.0.3",
"tslint": "^5.10.0",
"typescript": "^2.8.3"
},
"engines": {
Expand Down
14 changes: 10 additions & 4 deletions src/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface IPromptOptions {
* Requires user input if true, otherwise allows empty input
*/
required?: boolean
default?: string
}

interface IPromptConfig {
Expand All @@ -18,6 +19,7 @@ interface IPromptConfig {
type: 'normal' | 'mask' | 'hide'
isTTY: boolean
required: boolean
default?: string
}

export default {
Expand All @@ -40,9 +42,13 @@ export default {
}

function _prompt(name: string, inputOptions: Partial<IPromptOptions> = {}): Promise<string> {
let prompt = chalk.dim('> ')
if (name && inputOptions.default) prompt = chalk.dim(name) + ' ' + chalk.yellow('[' + inputOptions.default + ']') + chalk.dim(': ')
else if (name) prompt = chalk.dim(`${name}: `)
const options: IPromptConfig = {
isTTY: !!(process.env.TERM !== 'dumb' && process.stdin.isTTY), name,
prompt: name ? chalk.dim(`${name}: `) : chalk.dim('> '),
isTTY: !!(process.env.TERM !== 'dumb' && process.stdin.isTTY),
name,
prompt,
type: 'normal',
required: true,
...inputOptions,
Expand All @@ -67,10 +73,10 @@ function normal(options: IPromptConfig, retries = 100): Promise<string> {
process.stdin.once('data', data => {
process.stdin.pause()
data = data.trim()
if (options.required && data === '') {
if (!options.default && options.required && data === '') {
resolve(normal(options, retries - 1))
} else {
resolve(data)
resolve(data || options.default)
}
})
})
Expand Down
50 changes: 36 additions & 14 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@
version "9.4.5"
resolved "https://registry.yarnpkg.com/@types/node/-/node-9.4.5.tgz#d2a90c634208173d1b1a0a6ba9f1df3de62edcf5"

"@types/node@^10.0.2":
version "10.0.2"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.0.2.tgz#180046ebbc37de8b64777765c2486ebecb59448e"
"@types/node@^10.0.4":
version "10.0.4"
resolved "https://registry.yarnpkg.com/@types/node/-/node-10.0.4.tgz#130598ee56e9a1210a53f557d64935571f05390d"

"@types/semver@^5.5.0":
version "5.5.0"
Expand Down Expand Up @@ -101,6 +101,10 @@ ansi-escapes@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.0.0.tgz#ec3e8b4e9f8064fc02c3ac9b65f1c275bda8ef92"

ansi-escapes@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/ansi-escapes/-/ansi-escapes-3.1.0.tgz#f73207bb81207d75fd6c83f125af26eea378ca30"

ansi-regex@^0.2.0, ansi-regex@^0.2.1:
version "0.2.1"
resolved "https://registry.yarnpkg.com/ansi-regex/-/ansi-regex-0.2.1.tgz#0d8e946967a3d8143f93e24e298525fc1b2235f9"
Expand Down Expand Up @@ -351,6 +355,16 @@ cross-spawn@^5.1.0:
shebang-command "^1.2.0"
which "^1.2.9"

cross-spawn@^6.0.5:
version "6.0.5"
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-6.0.5.tgz#4a5ec7c64dfae22c3a14124dbacdee846d80cbc4"
dependencies:
nice-try "^1.0.4"
path-key "^2.0.1"
semver "^5.5.0"
shebang-command "^1.2.0"
which "^1.2.9"

date-fns@^1.23.0:
version "1.29.0"
resolved "https://registry.yarnpkg.com/date-fns/-/date-fns-1.29.0.tgz#12e609cdcb935127311d04d33334e2960a2a54e6"
Expand Down Expand Up @@ -918,6 +932,10 @@ natural-compare@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/natural-compare/-/natural-compare-1.4.0.tgz#4abebfeed7541f2c27acfb29bdbbd15c8d5ba4f7"

nice-try@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/nice-try/-/nice-try-1.0.4.tgz#d93962f6c52f2c1558c0fbda6d512819f1efe1c4"

normalize-path@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/normalize-path/-/normalize-path-1.0.0.tgz#32d0e472f91ff345701c15a8311018d3b0a90379"
Expand Down Expand Up @@ -953,12 +971,12 @@ os-tmpdir@~1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/os-tmpdir/-/os-tmpdir-1.0.2.tgz#bbe67406c79aa85c5cfec766fe5734555dfa1274"

password-prompt@^1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/password-prompt/-/password-prompt-1.0.4.tgz#933bac8db3528fcb27e9fdbc0a6592adcbdb5ed9"
password-prompt@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/password-prompt/-/password-prompt-1.0.5.tgz#d2f2effd89bf26d359c87a4678339804ce782b87"
dependencies:
ansi-escapes "^3.0.0"
cross-spawn "^5.1.0"
ansi-escapes "^3.1.0"
cross-spawn "^6.0.5"

path-is-absolute@^1.0.0:
version "1.0.1"
Expand All @@ -968,6 +986,10 @@ path-is-inside@^1.0.1, path-is-inside@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/path-is-inside/-/path-is-inside-1.0.2.tgz#365417dede44430d1c11af61027facf074bdfc53"

path-key@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/path-key/-/path-key-2.0.1.tgz#411cadb574c5a140d3a4b1910d40d80cc9f40b40"

path-parse@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.5.tgz#3c1adf871ea9cd6c9431b6ea2bd74a0ff055c4c1"
Expand Down Expand Up @@ -1255,9 +1277,9 @@ tree-kill@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/tree-kill/-/tree-kill-1.2.0.tgz#5846786237b4239014f05db156b643212d4c6f36"

ts-node@^6.0.2:
version "6.0.2"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-6.0.2.tgz#e132d530e53173bc6a8c21ea65f64d8b47bc573e"
ts-node@^6.0.3:
version "6.0.3"
resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-6.0.3.tgz#28bf74bcad134fad17f7469dad04638ece03f0f4"
dependencies:
arrify "^1.0.0"
chalk "^2.3.0"
Expand Down Expand Up @@ -1301,9 +1323,9 @@ tslint-xo@^0.7.0:
tslint-eslint-rules "^5.1.0"
tslint-microsoft-contrib "^5.0.2"

tslint@^5.9.1:
version "5.9.1"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.9.1.tgz#1255f87a3ff57eb0b0e1f0e610a8b4748046c9ae"
tslint@^5.10.0:
version "5.10.0"
resolved "https://registry.yarnpkg.com/tslint/-/tslint-5.10.0.tgz#11e26bccb88afa02dd0d9956cae3d4540b5f54c3"
dependencies:
babel-code-frame "^6.22.0"
builtin-modules "^1.1.1"
Expand Down

0 comments on commit b136fce

Please sign in to comment.