docs: diagrams plugin tooling (#5741)
* added plugin * updated plugin + added component * dummy data TO BE REMOVED * (wip) workflow generator tool * add workflow generator tooling * updated the generator tool * added code file creation * fix design of diagrams * configured diagram theme * added build script * removed comments + unnecessary files * general fixes * refactored plugin * added README + more output types
This commit is contained in:
@@ -0,0 +1,83 @@
|
||||
/* eslint-disable no-case-declarations */
|
||||
import { WorkflowManager } from "@medusajs/orchestration"
|
||||
import * as path from "path"
|
||||
import { existsSync, mkdirSync, rmSync, writeFileSync } from "fs"
|
||||
import registerWorkflows from "../utils/register-workflows.js"
|
||||
import DiagramBuilder from "../classes/diagram-builder.js"
|
||||
import { run as runMermaid } from "@mermaid-js/mermaid-cli"
|
||||
|
||||
type Options = {
|
||||
output: string
|
||||
type: "docs" | "markdown" | "mermaid" | "console" | "svg" | "png" | "pdf"
|
||||
theme: boolean
|
||||
prettyNames: boolean
|
||||
}
|
||||
|
||||
export default async function (workflowPath: string, options: Options) {
|
||||
const workflowDefinitions = await registerWorkflows(workflowPath)
|
||||
|
||||
const diagramBuilder = new DiagramBuilder(options)
|
||||
|
||||
if (
|
||||
workflowDefinitions.size > 0 &&
|
||||
["svg", "png", "pdf"].includes(options.type)
|
||||
) {
|
||||
console.log(
|
||||
`Generating ${options.type} file(s) with mermaid. This may take some time...`
|
||||
)
|
||||
}
|
||||
|
||||
for (const [name, code] of workflowDefinitions) {
|
||||
const workflow = WorkflowManager.getWorkflow(name)
|
||||
|
||||
if (!workflow) {
|
||||
continue
|
||||
}
|
||||
|
||||
const diagram = diagramBuilder.buildDiagram(workflow.flow_)
|
||||
if (!existsSync(options.output)) {
|
||||
mkdirSync(options.output, { recursive: true })
|
||||
}
|
||||
|
||||
switch (options.type) {
|
||||
case "docs":
|
||||
const workflowPath = path.join(options.output, name)
|
||||
if (!existsSync(workflowPath)) {
|
||||
mkdirSync(workflowPath, { recursive: true })
|
||||
}
|
||||
// write files
|
||||
writeFileSync(path.join(workflowPath, "diagram.mermaid"), diagram)
|
||||
if (code) {
|
||||
writeFileSync(path.join(workflowPath, "code.ts"), code)
|
||||
}
|
||||
break
|
||||
case "mermaid":
|
||||
writeFileSync(path.join(options.output, `${name}.mermaid`), diagram)
|
||||
break
|
||||
case "markdown":
|
||||
writeFileSync(
|
||||
path.join(options.output, `${name}.md`),
|
||||
`\`\`\`mermaid\n${diagram}\n\`\`\``
|
||||
)
|
||||
break
|
||||
case "console":
|
||||
console.log(`Diagram for workflow ${name}:\n${diagram}`)
|
||||
break
|
||||
case "svg":
|
||||
case "png":
|
||||
case "pdf":
|
||||
const tempFilePath = path.join(options.output, `${name}.mermaid`)
|
||||
writeFileSync(tempFilePath, diagram)
|
||||
await runMermaid(
|
||||
tempFilePath,
|
||||
path.join(options.output, `${name}.${options.type}`),
|
||||
{
|
||||
quiet: true,
|
||||
}
|
||||
)
|
||||
rmSync(tempFilePath)
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`Generated diagrams for ${workflowDefinitions.size} workflows.`)
|
||||
}
|
||||
Reference in New Issue
Block a user