|
1
|
import { TransactionStepsDefinition } from "@medusajs/orchestration" |
|
2
|
import getRandomString from "../utils/get-random-string.js" |
|
3
|
|
|
4
|
type DiagramBuilderOptions = { |
|
5
|
theme?: boolean |
|
6
|
prettyNames?: boolean |
|
7
|
} |
|
8
|
|
|
9
|
type ReturnedSteps = { |
|
10
|
escapedStepNames: string[] |
|
11
|
links: string[] |
|
12
|
defsStr: string |
|
13
|
} |
|
14
|
|
|
15
|
export default class DiagramBuilder { |
|
16
|
private options: DiagramBuilderOptions |
|
17
|
static SPACING = "\t" |
|
18
|
|
|
19
|
constructor(options: DiagramBuilderOptions) { |
|
20
|
this.options = options |
|
21
|
} |
|
22
|
|
|
23
|
buildDiagram(workflow: TransactionStepsDefinition): string { |
|
24
|
let diagram = `${this.getThemeConfig()}${ |
|
25
|
this.options.theme ? this.getLinePrefix(1) : "" |
|
26
|
}flowchart TB` |
|
27
|
|
|
28
|
const stepsDiagram = this.getSteps(workflow, this.options.theme ? 2 : 1) |
|
29
|
|
|
30
|
diagram += |
|
31
|
stepsDiagram.defsStr + `\n` + this.formatLinks(stepsDiagram.links) |
|
32
|
|
|
33
|
return diagram |
|
34
|
} |
|
35
|
|
|
36
|
getThemeConfig(): string { |
|
37
|
return this.options.theme |
|
38
|
? `%%{ |
|
39
|
init: { |
|
40
|
'theme': 'base', |
|
41
|
'themeVariables': { |
|
42
|
'background': '#FFFFFF', |
|
43
|
'mainBkg': '#FFFFFF', |
|
44
|
'primaryColor': '#FFFFFF', |
|
45
|
'primaryTextColor': '#030712', |
|
46
|
'primaryBorderColor': '#D1D5DB', |
|
47
|
'nodeBorder': '#D1D5DB', |
|
48
|
'lineColor': '#11181C', |
|
49
|
'fontFamily': 'Inter', |
|
50
|
'fontSize': '13px', |
|
51
|
'tertiaryColor': '#F3F4F6', |
|
52
|
'tertiaryBorderColor': '#D1D5DB', |
|
53
|
'tertiaryTextColor': '#030712' |
|
54
|
} |
|
55
|
} |
|
56
|
}%%` |
|
57
|
: "" |
|
58
|
} |
|
59
|
|
|
60
|
getSteps( |
|
61
|
flow: TransactionStepsDefinition | TransactionStepsDefinition[], |
|
62
|
level: number |
|
63
|
): ReturnedSteps { |
|
64
|
const links: string[] = [] |
|
65
|
let defsStr = "" |
|
66
|
const escapedStepNames: string[] = [] |
|
67
|
const linePrefix = this.getLinePrefix(level) |
|
68
|
|
|
69
|
const flowArr: TransactionStepsDefinition[] | undefined = Array.isArray( |
|
70
|
flow |
|
71
|
) |
|
72
|
? flow |
|
73
|
: !flow.action && Array.isArray(flow.next) |
|
74
|
? flow.next |
|
75
|
: undefined |
|
76
|
|
|
77
|
if (flowArr) { |
|
78
|
// these are steps running in parallel |
|
79
|
// since there are changes where the flowArr contains |
|
80
|
// one item, we check the length before treating the |
|
81
|
// main steps as steps running in parallel |
|
82
|
const areStepsParallel = flowArr.length > 1 |
|
83
|
const parallelDefinitions: Record<string, string> = {} |
|
84
|
flowArr.forEach((flowItem) => { |
|
85
|
const flowSteps = this.getSteps(flowItem, level) |
|
86
|
if (areStepsParallel) { |
|
87
|
const escapedName = this.getEscapedStepName(flowItem.action) |
|
88
|
if (escapedName) { |
|
89
|
const itemDefinition = `${linePrefix}${escapedName}(${this.formatStepName( |
|
90
|
flowItem.action! |
|
91
|
)})` |
|
92
|
parallelDefinitions[itemDefinition] = flowSteps.defsStr.replace( |
|
93
|
itemDefinition, |
|
94
|
"" |
|
95
|
) |
|
96
|
} else { |
|
97
|
// if the step doesn't have an action name |
|
98
|
// we just show it as a regular step rather than |
|
99
|
// a subgraph |
|
100
|
defsStr += `${linePrefix}${flowSteps.defsStr}` |
|
101
|
} |
|
102
|
} else { |
|
103
|
// if the steps aren't parallel |
|
104
|
// just show them as regular steps |
|
105
|
defsStr += `${linePrefix}${flowSteps.defsStr}` |
|
106
|
} |
|
107
|
links.push(...flowSteps.links) |
|
108
|
escapedStepNames.push(...flowSteps.escapedStepNames) |
|
109
|
}) |
|
110
|
|
|
111
|
// if there are steps in parallel, |
|
112
|
// we show them as a subgraph |
|
113
|
const definitionKeys = Object.keys(parallelDefinitions) |
|
114
|
if (definitionKeys.length) { |
|
115
|
defsStr += `${this.getSubgraph( |
|
116
|
definitionKeys.join(""), |
|
117
|
linePrefix |
|
118
|
)}${linePrefix}${Object.values(parallelDefinitions).join("")}` |
|
119
|
} |
|
120
|
} else { |
|
121
|
const flowItem = flow as TransactionStepsDefinition |
|
122
|
const escapedName = this.getEscapedStepName(flowItem.action) |
|
123
|
|
|
124
|
if (escapedName.length) { |
|
125
|
escapedStepNames.push(escapedName) |
|
126
|
defsStr += `${linePrefix}${escapedName}(${this.formatStepName( |
|
127
|
flowItem.action! |
|
128
|
)})` |
|
129
|
} |
|
130
|
|
|
131
|
if (flowItem.next) { |
|
132
|
const nextSteps = this.getSteps(flowItem.next, level) |
|
133
|
defsStr += `${linePrefix}${nextSteps.defsStr}` |
|
134
|
if (escapedName.length) { |
|
135
|
nextSteps.escapedStepNames.forEach((escapedStep) => { |
|
136
|
links.push(`${linePrefix}${escapedName} --> ${escapedStep}`) |
|
137
|
}) |
|
138
|
} else { |
|
139
|
escapedStepNames.push(...nextSteps.escapedStepNames) |
|
140
|
} |
|
141
|
links.push(...nextSteps.links) |
|
142
|
} |
|
143
|
} |
|
144
|
|
|
145
|
return { |
|
146
|
escapedStepNames, |
|
147
|
links, |
|
148
|
defsStr, |
|
149
|
} |
|
150
|
} |
|
151
|
|
|
152
|
getSubgraph(defsStr: string, linePrefix: string): string { |
|
153
|
return `${linePrefix}subgraph parallel${getRandomString()} [Parallel]${linePrefix}${defsStr}${linePrefix}end` |
|
154
|
} |
|
155
|
|
|
156
|
getEscapedStepName(originalName: string | undefined): string { |
|
157
|
return originalName?.replaceAll("-", "") || "" |
|
158
|
} |
|
159
|
|
|
160
|
formatStepName(originalName: string): string { |
|
161
|
if (!this.options?.prettyNames) { |
|
162
|
return originalName |
|
163
|
} |
|
164
|
return originalName |
|
165
|
.replaceAll("-", " ") |
|
166
|
.replaceAll(/([A-Z])/g, " $1") |
|
167
|
.split(" ") |
|
168
|
.map((word) => `${word.charAt(0).toUpperCase()}${word.substring(1)}`) |
|
169
|
.join(" ") |
|
170
|
} |
|
171
|
|
|
172
|
getLinePrefix(indentation = 0): string { |
|
173
|
return `\n${DiagramBuilder.SPACING.repeat(indentation)}` |
|
174
|
} |
|
175
|
|
|
176
|
// TODO need to explore with this function |
|
177
|
// for now it just returns the joined links, but |
|
178
|
// it should split links on multiple lines in the |
|
179
|
// diagram |
|
180
|
formatLinks(links: string[], level = 2): string { |
|
181
|
const linePrefix = this.getLinePrefix(level) |
|
182
|
return links.join(linePrefix) |
|
183
|
|
|
184
|
// This is used to ensure that a line doesn't get too long |
|
185
|
// let nodesInCurrentLine = 0 |
|
186
|
// // TODO change this to be a command line option |
|
187
|
// const maxNodesInLine = 3 |
|
188
|
|
|
189
|
// if (links.length <= maxNodesInLine) { |
|
190
|
// return links.join(linePrefix) |
|
191
|
// } |
|
192
|
|
|
193
|
// let finalStr = "" |
|
194
|
|
|
195
|
// links.forEach((link) => { |
|
196
|
// if (nodesInCurrentLine === 0) { |
|
197
|
// finalStr += "subgraph" |
|
198
|
// } |
|
199
|
|
|
200
|
// finalStr += link |
|
201
|
// ++nodesInCurrentLine |
|
202
|
// if (nodesInCurrentLine === maxNodesInLine) { |
|
203
|
// finalStr += "end" |
|
204
|
// nodesInCurrentLine = 0 |
|
205
|
// } |
|
206
|
// }) |
|
207
|
|
|
208
|
// return finalStr |
|
209
|
} |
|
210
|
} |