-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathcreate.ts
45 lines (39 loc) · 1.54 KB
/
create.ts
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
import { Flags, Errors } from '@oclif/core'
import { AppRequest, AppCreationResponse } from '@smartthings/core-sdk'
import { APICommand, inputAndOutputItem, lambdaAuthFlags } from '@smartthings/cli-lib'
import { addPermission } from '../../lib/aws-utils'
import { tableFieldDefinitions } from '../../lib/commands/apps/apps-util'
export default class AppCreateCommand extends APICommand {
static description = 'create an app'
static flags = {
...APICommand.flags,
...inputAndOutputItem.flags,
authorize: Flags.boolean({
description: 'authorize Lambda functions to be called by SmartThings',
}),
...lambdaAuthFlags,
}
async run(): Promise<void> {
const { args, argv, flags } = await this.parse(AppCreateCommand)
await super.setup(args, argv, flags)
const createApp = async (_: void, data: AppRequest): Promise<AppCreationResponse> => {
// TODO extract this authorization block out to util function and use in ./update.ts as well
if (flags.authorize) {
if (data.lambdaSmartApp) {
if (data.lambdaSmartApp.functions) {
const requests = data.lambdaSmartApp.functions.map((functionArn) => {
return addPermission(functionArn, flags.principal, flags['statement-id'])
})
await Promise.all(requests)
}
} else {
throw new Errors.CLIError('Authorization is not applicable to WebHook SmartApps')
}
}
return this.client.apps.create(data)
}
await inputAndOutputItem(this,
{ buildTableOutput: data => this.tableGenerator.buildTableFromItem(data.app, tableFieldDefinitions) },
createApp)
}
}