-
Notifications
You must be signed in to change notification settings - Fork 131
Expand file tree
/
Copy pathcreate.ts
More file actions
172 lines (151 loc) · 5.72 KB
/
create.ts
File metadata and controls
172 lines (151 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import inquirer from 'inquirer'
import { DevicePreferenceCreate, PreferenceType } from '@smartthings/core-sdk'
import { APIOrganizationCommand, askForInteger, askForNumber, askForRequiredString, askForString, inputAndOutputItem, userInputProcessor } from '@smartthings/cli-lib'
import { tableFieldDefinitions } from '../../lib/commands/devicepreferences-util'
export default class DevicePreferencesCreateCommand extends APIOrganizationCommand<typeof DevicePreferencesCreateCommand.flags> {
static description = 'create a device preference'
static flags = {
...APIOrganizationCommand.flags,
...inputAndOutputItem.flags,
}
static aliases = ['device-preferences:create']
static examples = [`
# create a new device preference by answering questions
$ smartthings devicepreferences:create
`,
`
# generate a device preference by answering questions but do not actually create it
$ smartthings devicepreferences:create -d
`,
`
# create a new device preference defined by the file dp.json
$ smartthings devicepreferences:create -i dp.json
`,
`
# create a new device preference defined by the file dp.json and write the results to dp - saved.json
$ smartthings devicepreferences: create - i dp.json - o dp - saved.json
`]
async run(): Promise<void> {
await inputAndOutputItem(this, { tableFieldDefinitions },
(_, input: DevicePreferenceCreate) => this.client.devicePreferences.create(input),
userInputProcessor(this))
}
async getInputFromUser(): Promise<DevicePreferenceCreate> {
const name = await askForString('Preference name:',
input => !input || input.match(/^[a-z][a-zA-Z0-9]{2,23}$/) ? true : 'must be camelCase starting with a lowercase letter and 3-24 characters')
const title = await askForRequiredString('Preference title:')
const description = await askForString('Preference description:')
const required = (await inquirer.prompt({
type: 'confirm',
name: 'value',
message: 'Is the preference required?',
default: false,
})).value as boolean
const preferenceType = (await inquirer.prompt({
type: 'list',
name: 'preferenceType',
message: 'Choose a type for your preference.',
choices: ['integer', 'number', 'boolean', 'string', 'enumeration'],
})).preferenceType as PreferenceType
const base = {
name, title, description, required,
}
if (preferenceType === 'integer') {
const minimum = await askForInteger('Optional minimum value.')
const maximum = await askForInteger('Optional maximum value.', minimum)
const defaultValue = await askForInteger('Optional default value.', minimum, maximum)
return {
...base, preferenceType, definition: {
minimum: minimum ?? undefined,
maximum: maximum ?? undefined,
default: defaultValue ?? undefined,
},
}
}
if (preferenceType === 'number') {
const minimum = await askForNumber('Optional minimum value.')
const maximum = await askForNumber('Optional maximum value.', minimum)
const defaultValue = await askForNumber('Optional default value.', minimum, maximum)
return {
...base, preferenceType, definition: {
minimum: minimum ?? undefined,
maximum: maximum ?? undefined,
default: defaultValue ?? undefined,
},
}
}
if (preferenceType === 'boolean') {
const defaultValue = (await inquirer.prompt({
type: 'list',
name: 'defaultValue',
message: 'Choose a default value.',
choices: [{ name: 'none', value: undefined },
{ name: 'true', value: true },
{ name: 'false', value: false }],
})).defaultValue as boolean | undefined
return {
...base, preferenceType, definition: {
default: defaultValue ?? undefined,
},
}
}
if (preferenceType === 'string') {
const minLength = await askForInteger('Optional minimum length.')
const maxLength = await askForInteger('Optional maximum length.', minLength)
const stringType = (await inquirer.prompt({
type: 'list',
name: 'stringType',
message: 'Choose a type of string.',
choices: ['text', 'password', 'paragraph'],
default: 'text',
})).stringType as 'text' | 'password' | 'paragraph'
const defaultValue = await askForString('Optional default value.',
input => {
if (minLength !== undefined && input.length < minLength) {
return `default must be no less than minLength (${minLength}) characters`
}
if (maxLength !== undefined && input.length > maxLength) {
return `default must be no more than maxLength (${maxLength}) characters`
}
return true
})
return {
...base, preferenceType, definition: {
minLength: minLength ?? undefined,
maxLength: maxLength ?? undefined,
stringType,
default: defaultValue ?? undefined,
},
}
}
if (preferenceType === 'enumeration') {
const firstName = await askForRequiredString('Enter a name (key) for the first option.')
let value = await askForRequiredString('Enter a value for the first option.')
const options: { [name: string]: string } = { [firstName]: value }
let name: string | undefined
do {
name = await askForString('Enter a name (key) for the next option or press enter to continue.')
if (name) {
value = await askForRequiredString('Enter a value for the option.')
options[name] = value
}
} while (name)
const defaultValue = (await inquirer.prompt({
type: 'list',
name: 'defaultValue',
message: 'Choose a default option.',
choices: [
{ name: 'none', value: undefined },
...Object.entries(options).map(([name, value]) => ({ name: `${value} (${name})`, value: name }))],
default: undefined,
})).defaultValue as 'text' | 'password' | 'paragraph'
return {
...base, preferenceType, definition: {
options,
default: defaultValue,
},
}
}
throw Error(`invalid preference type ${preferenceType}`)
}
}