-
Notifications
You must be signed in to change notification settings - Fork 4
/
create.ts
108 lines (97 loc) · 3.26 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
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
import inquirer from 'inquirer'
import { Args, Flags } from '@oclif/core'
import {
featurePrompt,
FeaturePromptResult,
keyPrompt,
namePrompt,
} from '../../ui/prompts'
import CreateCommand from '../createCommand'
import { CreateVariationDto } from '../../api/schemas'
import { createVariation } from '../../api/variations'
import { promptForVariationVariableValues } from '../../ui/prompts/variationPrompts'
import { fetchVariables } from '../../api/variables'
import { fetchFeatureByKey } from '../../api/features'
export default class CreateVariation extends CreateCommand {
static hidden = false
static description = 'Create a new Variation for an existing Feature.'
static args = {
feature: Args.string({
name: 'feature',
description: 'Feature key or id',
}),
}
static flags = {
...CreateCommand.flags,
variables: Flags.string({
description: 'The variables to create for the variation',
}),
}
static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> --variables=\'{ "bool-var": true, "num-var": 80, "string-var": "test" }\'',
]
prompts = [namePrompt, keyPrompt]
public async run(): Promise<void> {
const { args, flags } = await this.parse(CreateVariation)
const { headless, key, name, variables, project } = flags
await this.requireProject(project, headless)
let featureKey
let feature_id
if (!args.feature) {
const { feature } = await inquirer.prompt<FeaturePromptResult>(
[featurePrompt],
{
token: this.authToken,
projectKey: this.projectKey,
},
)
featureKey = feature.key
feature_id = feature._id
} else {
featureKey = args.feature
}
const params = await this.populateParametersWithZod(
CreateVariationDto,
this.prompts,
{
key,
name,
headless,
},
)
let variableAnswers: Record<string, unknown> = {}
if (!variables) {
if (!feature_id) {
const feature = await fetchFeatureByKey(
this.authToken,
this.projectKey,
featureKey,
)
if (!feature) {
throw new Error(`Feature not found for key ${featureKey}`)
}
feature_id = feature._id
}
const variablesForFeature = await fetchVariables(
this.authToken,
this.projectKey,
{ feature: feature_id },
)
variableAnswers =
await promptForVariationVariableValues(variablesForFeature)
}
const variation = {
key: params.key,
name: params.name,
variables: variables ? JSON.parse(variables) : variableAnswers,
}
const result = await createVariation(
this.authToken,
this.projectKey,
featureKey,
variation,
)
this.writer.showResults(result)
}
}