-
Notifications
You must be signed in to change notification settings - Fork 3
/
update.ts
137 lines (128 loc) · 4.22 KB
/
update.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
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
import inquirer from 'inquirer'
import {
fetchVariationByKey,
updateVariation,
UpdateVariationParams,
} from '../../api/variations'
import {
featurePrompt,
FeaturePromptResult,
keyPrompt,
namePrompt,
} from '../../ui/prompts'
import { Feature, Variable } from '../../api/schemas'
import {
getVariationVariablePrompt,
promptForVariationVariableValues,
variationPrompt,
} from '../../ui/prompts/variationPrompts'
import { Args, Flags } from '@oclif/core'
import { fetchFeatureByKey } from '../../api/features'
import UpdateCommandWithCommonProperties from '../updateCommandWithCommonProperties'
export default class UpdateVariation extends UpdateCommandWithCommonProperties {
static hidden = false
authRequired = true
static description = 'Update a Variation.'
prompts = [namePrompt, keyPrompt]
static args = {
feature: Args.string({
name: 'feature',
description: 'Feature key or ID',
}),
...UpdateCommandWithCommonProperties.args,
}
static flags = {
...UpdateCommandWithCommonProperties.flags,
variables: Flags.string({
description: 'The variables to create for the variation',
}),
key: Flags.string({
description: 'The variation key',
parse: async (input: string) => {
return input.toLowerCase()
},
}),
}
public async run(): Promise<void> {
const { args, flags } = await this.parse(UpdateVariation)
const { variables, name, key, headless, project } = flags
await this.requireProject(project, headless)
let featureKey
if (!args.feature) {
const { feature } = await inquirer.prompt<FeaturePromptResult>(
[featurePrompt],
{
token: this.authToken,
projectKey: this.projectKey,
},
)
featureKey = feature.key
} else {
featureKey = args.feature
}
let selectedVariation
if (!args.key) {
const { variation } = await inquirer.prompt([variationPrompt], {
token: this.authToken,
projectKey: this.projectKey,
featureKey,
})
selectedVariation = variation
} else {
selectedVariation = await fetchVariationByKey(
this.authToken,
this.projectKey,
featureKey,
args.key,
)
}
const feature = (await fetchFeatureByKey(
this.authToken,
this.projectKey,
featureKey,
)) as Feature
this.prompts.push(
await getVariationVariablePrompt(
this.authToken,
this.projectKey,
feature._id,
),
)
this.writer.printCurrentValues(selectedVariation)
const data = await this.populateParameters(
UpdateVariationParams,
this.prompts,
{
name,
key,
...(variables ? { variables: JSON.parse(variables) } : {}),
headless,
},
true,
)
let variableAnswers: Record<string, unknown> = {}
if (!variables && data.variables) {
variableAnswers = await promptForVariationVariableValues(
// This is a hack that's needed since the output from the variable prompt is different
// from the input for the --variable flag. That variation variable value data comes from
// this prompt.
data.variables as unknown as Variable[],
selectedVariation.variables,
)
}
const result = await updateVariation(
this.authToken,
this.projectKey,
featureKey,
selectedVariation.key,
{
key: data.key,
name: data.name,
variables: variables
? JSON.parse(variables)
: { ...selectedVariation.variables, ...variableAnswers },
},
)
this.writer.showResults(result)
}
}