amplify-prompts
Advanced tools
Comparing version 1.6.0-apiext4.0 to 1.7.0-beta.0
@@ -6,3 +6,3 @@ # Change Log | ||
# [1.6.0-apiext4.0](https://github.com/aws-amplify/amplify-cli/compare/amplify-prompts@1.5.1...amplify-prompts@1.6.0-apiext4.0) (2021-11-21) | ||
# [1.7.0-beta.0](https://github.com/aws-amplify/amplify-cli/compare/amplify-prompts@1.5.1...amplify-prompts@1.7.0-beta.0) (2021-11-23) | ||
@@ -12,4 +12,3 @@ | ||
* add min / max selection to prompter.pick ([#8971](https://github.com/aws-amplify/amplify-cli/issues/8971)) ([40ab535](https://github.com/aws-amplify/amplify-cli/commit/40ab53587a79139ea5c1aeb9040df524dbc2d89f)) | ||
* REST API extensibility ([#8958](https://github.com/aws-amplify/amplify-cli/issues/8958)) ([4efbdc0](https://github.com/aws-amplify/amplify-cli/commit/4efbdc043e234dc012e423c3a582162a4bbb16c2)) | ||
* override support for api category ([#9013](https://github.com/aws-amplify/amplify-cli/issues/9013)) ([ae7b001](https://github.com/aws-amplify/amplify-cli/commit/ae7b001f274f327a29c99c67fe851272c6208e84)), closes [#9001](https://github.com/aws-amplify/amplify-cli/issues/9001) [#8954](https://github.com/aws-amplify/amplify-cli/issues/8954) [#8958](https://github.com/aws-amplify/amplify-cli/issues/8958) [#8960](https://github.com/aws-amplify/amplify-cli/issues/8960) [#8967](https://github.com/aws-amplify/amplify-cli/issues/8967) [#8971](https://github.com/aws-amplify/amplify-cli/issues/8971) [#8976](https://github.com/aws-amplify/amplify-cli/issues/8976) [#8975](https://github.com/aws-amplify/amplify-cli/issues/8975) [#8981](https://github.com/aws-amplify/amplify-cli/issues/8981) [#8983](https://github.com/aws-amplify/amplify-cli/issues/8983) [#8992](https://github.com/aws-amplify/amplify-cli/issues/8992) [#9000](https://github.com/aws-amplify/amplify-cli/issues/9000) [#9002](https://github.com/aws-amplify/amplify-cli/issues/9002) [#9005](https://github.com/aws-amplify/amplify-cli/issues/9005) [#9006](https://github.com/aws-amplify/amplify-cli/issues/9006) [#9007](https://github.com/aws-amplify/amplify-cli/issues/9007) [#9008](https://github.com/aws-amplify/amplify-cli/issues/9008) [#9010](https://github.com/aws-amplify/amplify-cli/issues/9010) [#9011](https://github.com/aws-amplify/amplify-cli/issues/9011) [#9012](https://github.com/aws-amplify/amplify-cli/issues/9012) [#9014](https://github.com/aws-amplify/amplify-cli/issues/9014) [#9015](https://github.com/aws-amplify/amplify-cli/issues/9015) [#9017](https://github.com/aws-amplify/amplify-cli/issues/9017) [#9020](https://github.com/aws-amplify/amplify-cli/issues/9020) [#9024](https://github.com/aws-amplify/amplify-cli/issues/9024) [#9027](https://github.com/aws-amplify/amplify-cli/issues/9027) [#9028](https://github.com/aws-amplify/amplify-cli/issues/9028) [#9029](https://github.com/aws-amplify/amplify-cli/issues/9029) [#9032](https://github.com/aws-amplify/amplify-cli/issues/9032) [#9031](https://github.com/aws-amplify/amplify-cli/issues/9031) [#9035](https://github.com/aws-amplify/amplify-cli/issues/9035) [#9038](https://github.com/aws-amplify/amplify-cli/issues/9038) [#9039](https://github.com/aws-amplify/amplify-cli/issues/9039) | ||
@@ -16,0 +15,0 @@ |
@@ -60,6 +60,5 @@ "use strict"; | ||
this.input = async (message, ...options) => { | ||
var _a; | ||
const opts = options === null || options === void 0 ? void 0 : options[0]; | ||
const opts = (options === null || options === void 0 ? void 0 : options[0]) || {}; | ||
if (flags_1.isYes) { | ||
if (opts === null || opts === void 0 ? void 0 : opts.initial) { | ||
if (opts.initial) { | ||
return opts.initial; | ||
@@ -71,12 +70,12 @@ } | ||
} | ||
const validator = ((opts === null || opts === void 0 ? void 0 : opts.returnSize) === 'many' ? validateEachWith(opts === null || opts === void 0 ? void 0 : opts.validate) : opts === null || opts === void 0 ? void 0 : opts.validate); | ||
const validator = (opts.returnSize === 'many' ? validateEachWith(opts.validate) : opts.validate); | ||
const { result } = await this.prompter({ | ||
type: ((_a = opts) === null || _a === void 0 ? void 0 : _a.hidden) ? 'invisible' : (opts === null || opts === void 0 ? void 0 : opts.returnSize) === 'many' ? 'list' : 'input', | ||
type: 'hidden' in opts && opts.hidden ? 'invisible' : opts.returnSize === 'many' ? 'list' : 'input', | ||
name: 'result', | ||
message, | ||
validate: validator, | ||
initial: opts === null || opts === void 0 ? void 0 : opts.initial, | ||
footer: (opts === null || opts === void 0 ? void 0 : opts.returnSize) === 'many' ? 'Enter a comma-delimited list of values' : undefined, | ||
initial: opts.initial, | ||
footer: opts.returnSize === 'many' ? 'Enter a comma-delimited list of values' : undefined, | ||
}); | ||
if (typeof (opts === null || opts === void 0 ? void 0 : opts.transform) === 'function') { | ||
if (typeof opts.transform === 'function') { | ||
if (Array.isArray(result)) { | ||
@@ -92,10 +91,10 @@ return (await Promise.all(result.map(async (part) => opts.transform(part)))); | ||
this.pick = async (message, choices, ...options) => { | ||
if ((choices === null || choices === void 0 ? void 0 : choices.length) === 0) { | ||
if (!(choices === null || choices === void 0 ? void 0 : choices.length)) { | ||
throw new Error(`No choices provided for prompt [${message}]`); | ||
} | ||
const opts = options === null || options === void 0 ? void 0 : options[0]; | ||
const opts = (options === null || options === void 0 ? void 0 : options[0]) || {}; | ||
const genericChoices = typeof choices[0] === 'string' | ||
? choices.map(choice => ({ name: choice, value: choice })) | ||
: choices; | ||
const initialIndexes = initialOptsToIndexes(genericChoices.map(choice => choice.value), opts === null || opts === void 0 ? void 0 : opts.initial); | ||
const initialIndexes = initialOptsToIndexes(genericChoices.map(choice => choice.value), opts.initial); | ||
const choiceValueMap = new Map(); | ||
@@ -108,5 +107,9 @@ const enquirerChoices = genericChoices.map(choice => { | ||
let result = genericChoices[0].name; | ||
if ((choices === null || choices === void 0 ? void 0 : choices.length) === 1) { | ||
if (choices.length === 1 && opts.returnSize !== 'many') { | ||
this.print.info(`Only one option for [${message}]. Selecting [${result}].`); | ||
} | ||
else if ('pickAtLeast' in opts && (opts.pickAtLeast || 0) >= choices.length) { | ||
result = genericChoices.map(choice => choice.name); | ||
this.print.info(`Must pick at least ${opts.pickAtLeast} of ${choices.length} options. Selecting all options [${result}]`); | ||
} | ||
else if (flags_1.isYes) { | ||
@@ -128,3 +131,3 @@ if (initialIndexes === undefined || (Array.isArray(initialIndexes) && initialIndexes.length === 0)) { | ||
actions, | ||
footer: (opts === null || opts === void 0 ? void 0 : opts.returnSize) === 'many' ? chalk_1.default.gray('(Use <space> to select, <ctrl + a> to toggle all)') : undefined, | ||
footer: opts.returnSize === 'many' ? chalk_1.default.gray('(Use <space> to select, <ctrl + a> to toggle all)') : undefined, | ||
type: 'autocomplete', | ||
@@ -135,3 +138,3 @@ name: 'result', | ||
initial: initialIndexes, | ||
multiple: (opts === null || opts === void 0 ? void 0 : opts.returnSize) === 'many', | ||
multiple: opts.returnSize === 'many', | ||
choices: enquirerChoices, | ||
@@ -138,0 +141,0 @@ pointer(_, i) { |
{ | ||
"name": "amplify-prompts", | ||
"version": "1.6.0-apiext4.0", | ||
"version": "1.7.0-beta.0", | ||
"description": "Utility functions for Amplify CLI terminal I/O", | ||
@@ -51,3 +51,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "c9bd900b5ce025f0782393db31fddca89386e455" | ||
"gitHead": "94b64e9325704c2dfd22c1486e5bc52f9a425297" | ||
} |
@@ -142,2 +142,27 @@ import { byValue, byValues, prompter } from '../prompter'; | ||
}); | ||
it('returns array of single item if only one choice specified and must pick at least 1', async () => { | ||
expect(await prompter.pick<'many'>('test message', ['hello'], { returnSize: 'many', pickAtLeast: 1 })).toEqual(['hello']); | ||
expect(prompt_mock).toHaveBeenCalledTimes(0); | ||
}); | ||
it('returns array of all choices if must pick at lest that many options', async () => { | ||
expect(await prompter.pick<'many'>('test message', ['hello', 'hey'], { returnSize: 'many', pickAtLeast: 3 })).toEqual(['hello', 'hey']); | ||
expect(prompt_mock).toHaveBeenCalledTimes(0); | ||
}); | ||
it('prompts for selection when only one option with returnSize as many', async () => { | ||
prompt_mock.mockResolvedValueOnce({ result: ['hello'] }); | ||
expect(await prompter.pick<'many'>('test message', ['hello'], { returnSize: 'many' })).toEqual(['hello']); | ||
expect(prompt_mock).toHaveBeenCalled(); | ||
}); | ||
it('prompts for selection when pick at least is less than options length', async () => { | ||
prompt_mock.mockResolvedValueOnce({ result: ['hello', 'hey'] }); | ||
expect(await prompter.pick<'many'>('test message', ['hello', 'hey', 'hi'], { returnSize: 'many', pickAtLeast: 2 })).toEqual([ | ||
'hello', | ||
'hey', | ||
]); | ||
expect(prompt_mock).toHaveBeenCalled(); | ||
}); | ||
}); | ||
@@ -144,0 +169,0 @@ |
@@ -66,5 +66,5 @@ import { prompt } from 'enquirer'; | ||
input = async <RS extends ReturnSize = 'one', T = string>(message: string, ...options: MaybeOptionalInputOptions<RS, T>) => { | ||
const opts = options?.[0]; | ||
const opts = options?.[0] || {}; | ||
if (isYes) { | ||
if (opts?.initial) { | ||
if (opts.initial) { | ||
return opts.initial as PromptReturn<RS, T>; | ||
@@ -76,17 +76,17 @@ } else { | ||
const validator = (opts?.returnSize === 'many' ? validateEachWith(opts?.validate) : opts?.validate) as ValidatorCast; | ||
const validator = (opts.returnSize === 'many' ? validateEachWith(opts.validate) : opts.validate) as ValidatorCast; | ||
const { result } = await this.prompter<{ result: RS extends 'many' ? string[] : string }>({ | ||
type: (opts as any)?.hidden ? 'invisible' : opts?.returnSize === 'many' ? 'list' : 'input', | ||
type: 'hidden' in opts && opts.hidden ? 'invisible' : opts.returnSize === 'many' ? 'list' : 'input', | ||
name: 'result', | ||
message, | ||
validate: validator, | ||
initial: opts?.initial, | ||
initial: opts.initial, | ||
// footer is not part of the TS interface but it's part of the JS API | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
footer: opts?.returnSize === 'many' ? 'Enter a comma-delimited list of values' : undefined, | ||
footer: opts.returnSize === 'many' ? 'Enter a comma-delimited list of values' : undefined, | ||
}); | ||
if (typeof opts?.transform === 'function') { | ||
if (typeof opts.transform === 'function') { | ||
if (Array.isArray(result)) { | ||
@@ -120,7 +120,7 @@ return (await Promise.all(result.map(async part => (opts.transform as Function)(part) as T))) as PromptReturn<RS, T>; | ||
// some choices must be provided | ||
if (choices?.length === 0) { | ||
if (!choices?.length) { | ||
throw new Error(`No choices provided for prompt [${message}]`); | ||
} | ||
const opts = options?.[0]; | ||
const opts = options?.[0] || {}; | ||
@@ -135,3 +135,3 @@ // map string[] choices into GenericChoice<T>[] | ||
genericChoices.map(choice => choice.value), | ||
opts?.initial, | ||
opts.initial, | ||
); | ||
@@ -151,4 +151,8 @@ | ||
if (choices?.length === 1) { | ||
if (choices.length === 1 && opts.returnSize !== 'many') { | ||
this.print.info(`Only one option for [${message}]. Selecting [${result}].`); | ||
} else if ('pickAtLeast' in opts && (opts.pickAtLeast || 0) >= choices.length) { | ||
// if you have to pick at least as many options as are available, select all of them and return without prompting | ||
result = genericChoices.map(choice => choice.name); | ||
this.print.info(`Must pick at least ${opts.pickAtLeast} of ${choices.length} options. Selecting all options [${result}]`); | ||
} else if (isYes) { | ||
@@ -176,3 +180,3 @@ if (initialIndexes === undefined || (Array.isArray(initialIndexes) && initialIndexes.length === 0)) { | ||
// @ts-ignore | ||
footer: opts?.returnSize === 'many' ? chalk.gray('(Use <space> to select, <ctrl + a> to toggle all)') : undefined, | ||
footer: opts.returnSize === 'many' ? chalk.gray('(Use <space> to select, <ctrl + a> to toggle all)') : undefined, | ||
type: 'autocomplete', | ||
@@ -186,3 +190,3 @@ name: 'result', | ||
// @ts-ignore | ||
multiple: opts?.returnSize === 'many', | ||
multiple: opts.returnSize === 'many', | ||
choices: enquirerChoices, | ||
@@ -189,0 +193,0 @@ pointer(_: unknown, i: number) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
167951
1424