Skip to content

Commit

Permalink
fix: make cancel action actually cancel even when there are validatio…
Browse files Browse the repository at this point in the history
…n errors
  • Loading branch information
rossiam committed Aug 23, 2023
1 parent e7386c7 commit b5e2f94
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 16 deletions.
6 changes: 6 additions & 0 deletions .changeset/short-timers-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@smartthings/cli-lib": patch
"@smartthings/cli": patch
---

Fixed cancel action on schema:create command when validation errors exist.
9 changes: 5 additions & 4 deletions packages/lib/src/item-input/command-helpers.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -11,7 +12,6 @@ import {
previewJSONAction,
previewYAMLAction,
} from './defs'
import { red } from 'chalk'


export type UpdateFromUserInputOptions = {
Expand Down Expand Up @@ -55,9 +55,10 @@ export const updateFromUserInput = async <T extends object>(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 = [
Expand All @@ -76,7 +77,7 @@ export const updateFromUserInput = async <T extends object>(command: SmartThings
name: 'action',
message: 'Choose an action.',
choices,
default: validationResult === true ? finishAction : editAction,
default: finishAction,
})).action

if (action === editAction) {
Expand Down
3 changes: 2 additions & 1 deletion packages/lib/src/item-input/defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ export type InputDefinition<T> = {
*/
export type InputDefinitionValidateFunction = (input: string,
context?: unknown[]) => true | string | Promise<true | string>
export type InputDefinitionDefaultValueOrFn<T> = T | ((context?: unknown[]) => T)
export type DefaultValueFunction<T> = (context?: unknown[]) => T
export type InputDefinitionDefaultValueOrFn<T> = T | DefaultValueFunction<T>

export const addAction = Symbol('add')
export type AddAction = typeof addAction
Expand Down
10 changes: 9 additions & 1 deletion packages/lib/src/item-input/misc.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
12 changes: 2 additions & 10 deletions packages/lib/src/item-input/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export function objectDef<T extends object>(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 {
Expand Down Expand Up @@ -190,10 +190,6 @@ export function objectDef<T extends object>(name: string, inputDefsByProperty: I
} else if (propertyName === updatedPropertyName) {
afterUpdatedProperty = true
}
const propertyInputDefinition = inputDefsByProperty[propertyName]
if (!propertyInputDefinition) {
continue
}
}
}
} else {
Expand All @@ -218,7 +214,7 @@ export function objectDef<T extends object>(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
}
Expand All @@ -228,10 +224,6 @@ export function objectDef<T extends object>(name: string, inputDefsByProperty: I
// (Once this is implemented, remove note from documentation for this function.)
afterUpdatedProperty = true
}
const propertyInputDefinition = inputDefsByProperty[propertyName]
if (!propertyInputDefinition) {
continue
}
}
}
}
Expand Down

0 comments on commit b5e2f94

Please sign in to comment.