amplify-prompts
Advanced tools
Comparing version 2.0.2-alpha.20 to 2.0.2-alpha.34
@@ -0,1 +1,2 @@ | ||
import { IFlowData } from 'amplify-cli-shared-interfaces'; | ||
import { Validator } from './validators'; | ||
@@ -11,2 +12,3 @@ export declare const prompter: Prompter; | ||
pick: <RS extends ReturnSize = 'one', T = string>(message: string, choices: Choices<T>, ...options: MaybeOptionalPickOptions<RS, T>) => Promise<PromptReturn<RS, T>>; | ||
setFlowData: (flowData: IFlowData) => void; | ||
}; | ||
@@ -13,0 +15,0 @@ declare type MaybeAvailableHiddenInputOption<RS extends ReturnSize> = RS extends 'many' ? unknown : { |
@@ -35,13 +35,37 @@ "use strict"; | ||
this.print = print; | ||
this.throwLoggedError = (message, errorMsg) => { | ||
var _a; | ||
(_a = this.flowData) === null || _a === void 0 ? void 0 : _a.pushInteractiveFlow(message, errorMsg); | ||
throw new Error(errorMsg); | ||
}; | ||
this.setFlowData = (flowData) => { | ||
this.flowData = flowData; | ||
}; | ||
this.pushInteractiveFlow = (promptString, input, redact = false) => { | ||
if (flags_1.isInteractiveShell) { | ||
if (this.flowData && input) { | ||
const finalInput = (redact) ? "*".repeat(input.length) : input; | ||
this.flowData.pushInteractiveFlow(promptString, finalInput); | ||
} | ||
} | ||
}; | ||
this.confirmContinue = async (message = 'Do you want to continue?') => { | ||
let result = false; | ||
if (flags_1.isYes) { | ||
return true; | ||
result = true; | ||
} | ||
return this.yesOrNoCommon(message, false); | ||
else { | ||
result = await this.yesOrNoCommon(message, false); | ||
} | ||
return result; | ||
}; | ||
this.yesOrNo = async (message, initial = true) => { | ||
let result = false; | ||
if (flags_1.isYes) { | ||
return initial; | ||
result = initial; | ||
} | ||
return this.yesOrNoCommon(message, initial); | ||
else { | ||
result = await this.yesOrNoCommon(message, initial); | ||
} | ||
return result; | ||
}; | ||
@@ -61,2 +85,3 @@ this.yesOrNoCommon = async (message, initial) => { | ||
}); | ||
this.pushInteractiveFlow(message, result); | ||
return result; | ||
@@ -67,11 +92,13 @@ }; | ||
const opts = (_a = options === null || options === void 0 ? void 0 : options[0]) !== null && _a !== void 0 ? _a : {}; | ||
const enquirerPromptType = 'hidden' in opts && opts.hidden ? EnquirerPromptType.INVISIBLE : opts.returnSize === 'many' ? EnquirerPromptType.LIST : EnquirerPromptType.INPUT; | ||
if (flags_1.isYes) { | ||
if (opts.initial !== undefined) { | ||
this.pushInteractiveFlow(message, opts.initial, enquirerPromptType == EnquirerPromptType.INVISIBLE); | ||
return opts.initial; | ||
} | ||
throw new Error(`Cannot prompt for [${message}] when '--yes' flag is set`); | ||
this.throwLoggedError(message, `Cannot prompt for [${message}] when '--yes' flag is set`); | ||
} | ||
const validator = (opts.returnSize === 'many' ? validateEachWith(opts.validate) : opts.validate); | ||
const { result } = await this.prompter({ | ||
type: 'hidden' in opts && opts.hidden ? 'invisible' : opts.returnSize === 'many' ? 'list' : 'input', | ||
type: enquirerPromptType, | ||
name: 'result', | ||
@@ -84,7 +111,13 @@ message, | ||
if (typeof opts.transform === 'function') { | ||
let functionResult; | ||
if (Array.isArray(result)) { | ||
return (await Promise.all(result.map(async (part) => opts.transform(part)))); | ||
functionResult = (await Promise.all(result.map(async (part) => opts.transform(part)))); | ||
} | ||
return opts.transform(result); | ||
else { | ||
functionResult = opts.transform(result); | ||
} | ||
this.pushInteractiveFlow(message, functionResult, enquirerPromptType == EnquirerPromptType.INVISIBLE); | ||
return functionResult; | ||
} | ||
this.pushInteractiveFlow(message, result, enquirerPromptType == EnquirerPromptType.INVISIBLE); | ||
return result; | ||
@@ -94,3 +127,3 @@ }; | ||
if (!(choices === null || choices === void 0 ? void 0 : choices.length)) { | ||
throw new Error(`No choices provided for prompt [${message}]`); | ||
this.throwLoggedError(message, `No choices provided for prompt [${message}]`); | ||
} | ||
@@ -105,3 +138,4 @@ const opts = (options === null || options === void 0 ? void 0 : options[0]) || {}; | ||
choiceValueMap.set(choice.name, choice.value); | ||
return { name: choice.name, disabled: choice.disabled, hint: choice.hint }; | ||
const enqResult = { name: choice.name, disabled: choice.disabled, hint: choice.hint }; | ||
return enqResult; | ||
}); | ||
@@ -159,6 +193,11 @@ actions.ctrl.a = 'a'; | ||
} | ||
let loggedRet; | ||
if (Array.isArray(result)) { | ||
return result.map(item => choiceValueMap.get(item)); | ||
loggedRet = result.map(item => choiceValueMap.get(item)); | ||
} | ||
return choiceValueMap.get(result); | ||
else { | ||
loggedRet = choiceValueMap.get(result); | ||
} | ||
this.pushInteractiveFlow(message, loggedRet); | ||
return loggedRet; | ||
}; | ||
@@ -200,2 +239,9 @@ const prompterShim = ((opts) => { | ||
const defaultEquals = (a, b) => a === b; | ||
var EnquirerPromptType; | ||
(function (EnquirerPromptType) { | ||
EnquirerPromptType["INVISIBLE"] = "invisible"; | ||
EnquirerPromptType["LIST"] = "list"; | ||
EnquirerPromptType["INPUT"] = "input"; | ||
})(EnquirerPromptType || (EnquirerPromptType = {})); | ||
; | ||
//# sourceMappingURL=prompter.js.map |
{ | ||
"name": "amplify-prompts", | ||
"version": "2.0.2-alpha.20+18d4eecb1", | ||
"version": "2.0.2-alpha.34+714f2fab0", | ||
"description": "Utility functions for Amplify CLI terminal I/O", | ||
@@ -8,3 +8,3 @@ "main": "lib/index.js", | ||
"build": "tsc", | ||
"clean": "rimraf lib tsconfig.tsbuildinfo", | ||
"clean": "rimraf lib tsconfig.tsbuildinfo node_modules", | ||
"demo": "yarn build && node lib/demo/demo.js", | ||
@@ -30,2 +30,3 @@ "test": "jest --color", | ||
"dependencies": { | ||
"amplify-cli-shared-interfaces": "1.0.1-alpha.5142+714f2fab0", | ||
"chalk": "^4.1.1", | ||
@@ -53,3 +54,3 @@ "enquirer": "^2.3.6" | ||
}, | ||
"gitHead": "18d4eecb12c3611bfa50a6eeaef1dbd42aa90248" | ||
"gitHead": "714f2fab03b62b7b0c93dbcbd4837fe0eb5ce937" | ||
} |
@@ -0,1 +1,7 @@ | ||
/* eslint-disable @typescript-eslint/ban-types */ | ||
/* eslint-disable spellcheck/spell-checker */ | ||
/* eslint-disable @typescript-eslint/explicit-function-return-type */ | ||
/* eslint-disable max-len */ | ||
/* eslint-disable no-return-assign */ | ||
/* eslint-disable no-nested-ternary */ | ||
import { prompt } from 'enquirer'; | ||
@@ -7,2 +13,3 @@ // enquirer actions are not part of the TS types, but they are the recommended way to override enquirer behavior | ||
import chalk from 'chalk'; | ||
import { IFlowData } from 'amplify-cli-shared-interfaces'; | ||
import { isYes, isInteractiveShell } from './flags'; | ||
@@ -16,2 +23,3 @@ import { Validator } from './validators'; | ||
class AmplifyPrompter implements Prompter { | ||
flowData: IFlowData|undefined; // interactive cli flow data journal | ||
constructor(private readonly prompter: typeof prompt = prompt, private readonly print: typeof printer = printer) { | ||
@@ -30,2 +38,20 @@ // construct a shim on top of enquirer to throw an error if it is called when stdin is non-interactive | ||
private throwLoggedError = (message: string, errorMsg : string) : void => { | ||
this.flowData?.pushInteractiveFlow(message, errorMsg); | ||
throw new Error(errorMsg); | ||
} | ||
setFlowData = (flowData: IFlowData): void => { | ||
this.flowData = flowData; | ||
} | ||
private pushInteractiveFlow = (promptString: string, input: unknown, redact : boolean = false) => { | ||
if ( isInteractiveShell ) { | ||
if (this.flowData && input ) { | ||
const finalInput = ( redact ) ? "*".repeat((input as string).length) : input; | ||
this.flowData.pushInteractiveFlow(promptString, finalInput); | ||
} | ||
} | ||
} | ||
/** | ||
@@ -36,6 +62,9 @@ * Asks a continue prompt. | ||
confirmContinue = async (message = 'Do you want to continue?'): Promise<boolean> => { | ||
let result = false; | ||
if (isYes) { | ||
return true; | ||
result = true; | ||
} else { | ||
result = await this.yesOrNoCommon(message, false); | ||
} | ||
return this.yesOrNoCommon(message, false); | ||
return result; | ||
}; | ||
@@ -48,6 +77,9 @@ | ||
yesOrNo = async (message: string, initial = true): Promise<boolean> => { | ||
let result = false; | ||
if (isYes) { | ||
return initial; | ||
result = initial; | ||
} else { | ||
result = await this.yesOrNoCommon(message, initial); | ||
} | ||
return this.yesOrNoCommon(message, initial); | ||
return result; | ||
}; | ||
@@ -69,5 +101,8 @@ | ||
}); | ||
this.pushInteractiveFlow(message, result); | ||
return result; | ||
}; | ||
/** | ||
@@ -89,7 +124,10 @@ * Prompt for an input. | ||
const opts = options?.[0] ?? ({} as InputOptions<RS, T>); | ||
const enquirerPromptType : EnquirerPromptType = 'hidden' in opts && opts.hidden ? EnquirerPromptType.INVISIBLE : opts.returnSize === 'many' ? EnquirerPromptType.LIST : EnquirerPromptType.INPUT; | ||
if (isYes) { | ||
if (opts.initial !== undefined) { | ||
this.pushInteractiveFlow(message, opts.initial, enquirerPromptType == EnquirerPromptType.INVISIBLE); | ||
return opts.initial as PromptReturn<RS, T>; | ||
} | ||
throw new Error(`Cannot prompt for [${message}] when '--yes' flag is set`); | ||
this.throwLoggedError(message, `Cannot prompt for [${message}] when '--yes' flag is set`); | ||
} | ||
@@ -101,3 +139,3 @@ | ||
// eslint-disable-next-line no-nested-ternary | ||
type: 'hidden' in opts && opts.hidden ? 'invisible' : opts.returnSize === 'many' ? 'list' : 'input', | ||
type: enquirerPromptType, | ||
name: 'result', | ||
@@ -114,8 +152,12 @@ message, | ||
if (typeof opts.transform === 'function') { | ||
let functionResult; | ||
if (Array.isArray(result)) { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
return (await Promise.all(result.map(async part => (opts.transform as Function)(part) as T))) as unknown as PromptReturn<RS, T>; | ||
functionResult = (await Promise.all(result.map(async part => (opts.transform as Function)(part) as T))) as unknown as PromptReturn<RS, T>; | ||
} else { | ||
functionResult = opts.transform(result as string) as unknown as PromptReturn<RS, T>; | ||
} | ||
return opts.transform(result as string) as unknown as PromptReturn<RS, T>; | ||
this.pushInteractiveFlow(message, functionResult, enquirerPromptType == EnquirerPromptType.INVISIBLE ); | ||
return functionResult; | ||
} | ||
this.pushInteractiveFlow(message, result, enquirerPromptType == EnquirerPromptType.INVISIBLE ); | ||
return result as unknown as PromptReturn<RS, T>; | ||
@@ -144,3 +186,3 @@ }; | ||
if (!choices?.length) { | ||
throw new Error(`No choices provided for prompt [${message}]`); | ||
this.throwLoggedError(message, `No choices provided for prompt [${message}]`); | ||
} | ||
@@ -166,3 +208,4 @@ | ||
choiceValueMap.set(choice.name, choice.value); | ||
return { name: choice.name, disabled: choice.disabled, hint: choice.hint }; | ||
const enqResult = { name: choice.name, disabled: choice.disabled, hint: choice.hint }; | ||
return enqResult; | ||
}); | ||
@@ -243,7 +286,12 @@ | ||
let loggedRet; | ||
if (Array.isArray(result)) { | ||
return result.map(item => choiceValueMap.get(item) as T) as PromptReturn<RS, T>; | ||
// result is an array | ||
loggedRet = result.map(item => choiceValueMap.get(item) as T) as PromptReturn<RS, T>; | ||
} else { | ||
// result is a string | ||
loggedRet = choiceValueMap.get(result as string) as PromptReturn<RS, T>; | ||
} | ||
// result is a string | ||
return choiceValueMap.get(result as string) as PromptReturn<RS, T>; | ||
this.pushInteractiveFlow(message, loggedRet); | ||
return loggedRet; | ||
}; | ||
@@ -261,4 +309,4 @@ } | ||
export const byValues = <T>(selection: T[], equals: EqualsFunction<T> = defaultEquals): MultiFilterFunction<T> => ( | ||
choices: T[], | ||
) => selection.map(sel => choices.findIndex(choice => equals(choice, sel))).filter(idx => idx >= 0); | ||
choices: T[] | ||
) => selection.map(sel => choices.findIndex(choice => equals(choice, sel))).filter(idx => idx >= 0); | ||
@@ -316,2 +364,3 @@ /** | ||
) => Promise<PromptReturn<RS, T>>; | ||
setFlowData: (flowData: IFlowData) => void; | ||
}; | ||
@@ -325,4 +374,4 @@ | ||
: { | ||
hidden?: boolean; | ||
}; | ||
hidden?: boolean; | ||
}; | ||
@@ -346,4 +395,4 @@ // The initial selection for a pick prompt can be specified either by index or a selection function that generates indexes. | ||
: { | ||
pickAtLeast?: number; | ||
}; | ||
pickAtLeast?: number; | ||
}; | ||
@@ -353,4 +402,4 @@ type MultiSelectMaximum<RS extends ReturnSize> = RS extends 'one' | ||
: { | ||
pickAtMost?: number; | ||
}; | ||
pickAtMost?: number; | ||
}; | ||
@@ -371,7 +420,7 @@ type ValidateValueOption = { | ||
? { | ||
returnSize: 'many'; | ||
} | ||
returnSize: 'many'; | ||
} | ||
: { | ||
returnSize?: 'one'; | ||
}; | ||
returnSize?: 'one'; | ||
}; | ||
@@ -410,1 +459,8 @@ type Choices<T> = T extends string ? GenericChoice<T>[] | string[] : GenericChoice<T>[]; | ||
MaybeAvailableHiddenInputOption<RS>; | ||
// abstraction over equirer prompt types | ||
enum EnquirerPromptType { | ||
INVISIBLE = 'invisible', | ||
LIST = 'list', | ||
INPUT = 'input' | ||
}; |
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
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
178915
1604
3