-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdiagram.ts
More file actions
116 lines (99 loc) · 3.73 KB
/
diagram.ts
File metadata and controls
116 lines (99 loc) · 3.73 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
import Command from '../../core/command'
import { flags, FlagInput, managementSDKClient, cliux, printFlagDeprecation } from '@contentstack/cli-utilities'
import { createDiagram } from '../../core/content-type/diagram'
import { CreateDiagramOptions, DiagramOrientation } from '../../types'
import { getStack, getContentTypes, getGlobalFields } from '../../utils'
export default class DiagramCommand extends Command {
static description = "Create a visual diagram of a Stack's Content Types"
static examples = [
'$ csdx content-type:diagram --stack-api-key "xxxxxxxxxxxxxxxxxxx" --output "content-model.svg"',
'$ csdx content-type:diagram --alias "management token" --output "content-model.svg"',
'$ csdx content-type:diagram --alias "management token" --output "content-model.svg" --direction "landscape"',
'$ csdx content-type:diagram --alias "management token" --output "content-model.dot" --type "dot"'
]
static flags: any = {
stack: flags.string({
char: 's',
description: 'Stack UID',
exclusive: ['token-alias', 'alias'],
parse: printFlagDeprecation(['-s', '--stack'], ['-k', '--stack-api-key'])
}),
'stack-api-key': flags.string({
char: 'k',
description: 'Stack API Key',
exclusive: ['token-alias', 'alias']
}),
'token-alias': flags.string({
char: 'a',
description: 'Management token alias',
parse: printFlagDeprecation(['--token-alias'], ['-a', '--alias'])
}),
alias: flags.string({
char: 'a',
description: 'Alias of the management token'
}),
output: flags.string({
char: 'o',
description: 'full path to output',
hidden: false,
multiple: false,
required: true,
parse: printFlagDeprecation(['-o'], ['--output'])
}),
direction: flags.string({
char: 'd',
description: 'graph orientation',
default: 'portrait',
options: ['portrait', 'landscape'],
hidden: false,
multiple: false,
required: true,
parse: printFlagDeprecation(['-d'], ['--direction'])
}),
type: flags.string({
char: 't',
description: 'graph output file type',
default: 'svg',
options: ['svg', 'dot'],
hidden: false,
multiple: false,
required: true,
parse: printFlagDeprecation(['-t'], ['--type'])
})
}
async run() {
try {
const { flags } = await this.parse(DiagramCommand)
this.contentTypeManagementClient = await managementSDKClient({
host: this.cmaHost,
'X-CS-CLI': this.context?.analyticsInfo
})
this.setup(flags)
const outputPath = flags.output
if (!outputPath?.trim()) {
this.error('Please provide an output path.', { exit: 2 })
}
const spinner = cliux.loaderV2(Command.RequestDataMessage)
const [stack, contentTypes, globalFields] = await Promise.all([
getStack(this.contentTypeManagementClient, this.apiKey, spinner),
getContentTypes(this.contentTypeManagementClient, this.apiKey, spinner),
getGlobalFields(this.contentTypeManagementClient, this.apiKey, spinner)
])
cliux.loaderV2('', spinner)
const diagramOptions: CreateDiagramOptions = {
stackName: stack.name,
contentTypes: contentTypes,
globalFields: globalFields,
outputFileName: outputPath,
outputFileType: flags.type,
style: {
orientation: flags.direction === 'portrait' ? DiagramOrientation.Portrait : DiagramOrientation.Landscape
}
}
const output = await createDiagram(diagramOptions)
this.log(`Created Graph: ${output.outputPath}`)
} catch (error: any) {
this.error(error?.message || 'An error occurred.', { exit: 1, suggestions: error.suggestions })
}
}
}