diff --git a/.changeset/short-timers-exercise.md b/.changeset/short-timers-exercise.md new file mode 100644 index 00000000..d05024a7 --- /dev/null +++ b/.changeset/short-timers-exercise.md @@ -0,0 +1,6 @@ +--- +"@smartthings/cli-lib": patch +"@smartthings/cli": patch +--- + +Fixed cancel action on schema:create command when validation errors exist. diff --git a/packages/lib/src/item-input/command-helpers.ts b/packages/lib/src/item-input/command-helpers.ts index 2f7aef11..505496a6 100644 --- a/packages/lib/src/item-input/command-helpers.ts +++ b/packages/lib/src/item-input/command-helpers.ts @@ -1,6 +1,7 @@ import inquirer, { ChoiceCollection } from 'inquirer' import { jsonFormatter, OutputFormatter, yamlFormatter } from '../output' +import { red } from '../colors' import { SmartThingsCommandInterface } from '../smartthings-command' import { cancelAction, @@ -11,7 +12,6 @@ import { previewJSONAction, previewYAMLAction, } from './defs' -import { red } from 'chalk' export type UpdateFromUserInputOptions = { @@ -55,9 +55,10 @@ export const updateFromUserInput = async (command: SmartThings if (validationResult !== true) { console.log(red(validationResult)) const answer = await inputDefinition.updateFromUserInput(retVal) - if (answer !== cancelAction) { - retVal = answer + if (answer === cancelAction) { + command.cancel() } + retVal = answer continue } const choices: ChoiceCollection = [ @@ -76,7 +77,7 @@ export const updateFromUserInput = async (command: SmartThings name: 'action', message: 'Choose an action.', choices, - default: validationResult === true ? finishAction : editAction, + default: finishAction, })).action if (action === editAction) { diff --git a/packages/lib/src/item-input/defs.ts b/packages/lib/src/item-input/defs.ts index 892018e3..5217546e 100644 --- a/packages/lib/src/item-input/defs.ts +++ b/packages/lib/src/item-input/defs.ts @@ -84,7 +84,8 @@ export type InputDefinition = { */ export type InputDefinitionValidateFunction = (input: string, context?: unknown[]) => true | string | Promise -export type InputDefinitionDefaultValueOrFn = T | ((context?: unknown[]) => T) +export type DefaultValueFunction = (context?: unknown[]) => T +export type InputDefinitionDefaultValueOrFn = T | DefaultValueFunction export const addAction = Symbol('add') export type AddAction = typeof addAction diff --git a/packages/lib/src/item-input/misc.ts b/packages/lib/src/item-input/misc.ts index e2246ad1..73b0b6f0 100644 --- a/packages/lib/src/item-input/misc.ts +++ b/packages/lib/src/item-input/misc.ts @@ -1,5 +1,13 @@ import inquirer, { ChoiceCollection } from 'inquirer' -import { askForString, askForOptionalString, AskForStringOptions, ValidateFunction, AskForBooleanOptions, askForBoolean, DefaultValueOrFn } from '../user-query' +import { + askForString, + askForOptionalString, + AskForStringOptions, + ValidateFunction, + AskForBooleanOptions, + askForBoolean, + DefaultValueOrFn, +} from '../user-query' import { CancelAction, InputDefinition, diff --git a/packages/lib/src/item-input/object.ts b/packages/lib/src/item-input/object.ts index 148b5c7c..c694df94 100644 --- a/packages/lib/src/item-input/object.ts +++ b/packages/lib/src/item-input/object.ts @@ -162,7 +162,7 @@ export function objectDef(name: string, inputDefsByProperty: I if (action === helpAction) { console.log(`\n${options?.helpText}\n`) } else if (action === cancelAction) { - return original + return cancelAction } else if (action === finishAction) { return updated } else { @@ -190,10 +190,6 @@ export function objectDef(name: string, inputDefsByProperty: I } else if (propertyName === updatedPropertyName) { afterUpdatedProperty = true } - const propertyInputDefinition = inputDefsByProperty[propertyName] - if (!propertyInputDefinition) { - continue - } } } } else { @@ -218,7 +214,7 @@ export function objectDef(name: string, inputDefsByProperty: I const laterPropertyInputDefinition = inputDefsByProperty[propertyName] const updateIfNeeded = laterPropertyInputDefinition.updateIfNeeded if (updateIfNeeded) { - const laterPropertyValue = await updateIfNeeded(updated[propertyName], updatedPropertyName, [{ ...updated }, ...context]) + const laterPropertyValue = await updateIfNeeded(updated[propertyName], action, [{ ...updated }, ...context]) if (laterPropertyValue !== cancelAction) { updated[propertyName] = laterPropertyValue } @@ -228,10 +224,6 @@ export function objectDef(name: string, inputDefsByProperty: I // (Once this is implemented, remove note from documentation for this function.) afterUpdatedProperty = true } - const propertyInputDefinition = inputDefsByProperty[propertyName] - if (!propertyInputDefinition) { - continue - } } } }