Skip to content

Commit

Permalink
When using monitoring, generate flow documentation only when they hav…
Browse files Browse the repository at this point in the history
…e been updated
  • Loading branch information
nvuillam committed Dec 23, 2024
1 parent cce4515 commit ebeec02
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
33 changes: 28 additions & 5 deletions src/commands/hardis/doc/project2markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { listMajorOrgs } from '../../../common/utils/orgConfigUtils.js';
import { glob } from 'glob';
import { listFlowFiles } from '../../../common/utils/projectUtils.js';
import { generateFlowMarkdownFile, generateMarkdownFileWithMermaid } from '../../../common/utils/mermaidUtils.js';
import { MetadataUtils } from '../../../common/metadata-utils/index.js';

Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('sfdx-hardis', 'org');
Expand Down Expand Up @@ -54,6 +55,10 @@ _sfdx-hardis docker image is alpine-based and does not succeed to run mermaid/pu
];

public static flags: any = {
"diff-only": Flags.boolean({
default: false,
description: "Generate documentation only for changed files (used for monitoring)",
}),
debug: Flags.boolean({
char: 'd',
default: false,
Expand All @@ -70,6 +75,7 @@ _sfdx-hardis docker image is alpine-based and does not succeed to run mermaid/pu
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
public static requiresProject = true;

protected diffOnly: boolean = false;
protected packageXmlCandidates: any[];
protected outputMarkdownRoot = "docs"
protected outputMarkdownIndexFile = path.join(this.outputMarkdownRoot, "index.md")
Expand All @@ -81,6 +87,7 @@ _sfdx-hardis docker image is alpine-based and does not succeed to run mermaid/pu

public async run(): Promise<AnyJson> {
const { flags } = await this.parse(Project2Markdown);
this.diffOnly = flags["diff-only"] === true ? true : false;
this.debugMode = flags.debug || false;
this.packageXmlCandidates = this.listPackageXmlCandidates();

Expand Down Expand Up @@ -144,21 +151,30 @@ _sfdx-hardis docker image is alpine-based and does not succeed to run mermaid/pu
uxLog(this, c.cyan("Generating Flows Visual documentation... (if you don't want it, define GENERATE_FLOW_DOC=false in your environment variables)"));
await fs.ensureDir(path.join(this.outputMarkdownRoot, "flows"));
const packageDirs = this.project?.getPackageDirectories();
const updatedFlowNames = !this.diffOnly ?
[] :
(await MetadataUtils.listChangedFiles()).filter(f => f?.path?.endsWith(".flow-meta.xml")).map(f => path.basename(f.path, ".flow-meta.xml"));
const flowFiles = await listFlowFiles(packageDirs);
const flowErrors: string[] = [];
const flowWarnings: string[] = [];
const flowSkips: string[] = [];
const flowDescriptions: any[] = [];
for (const flowFile of flowFiles) {
const flowName = path.basename(flowFile, ".flow-meta.xml");
uxLog(this, c.grey(`Generating markdown for Flow ${flowFile}...`));
const flowXml = (await fs.readFile(flowFile, "utf8")).toString();
const flowContent = await parseXmlFile(flowFile);
flowDescriptions.push({
name: path.basename(flowFile).replace(".flow-meta.xml", ""),
name: flowName,
description: flowContent?.Flow?.description?.[0] || "",
type: flowContent?.Flow?.processType?.[0] === "Flow" ? "ScreenFlow" : flowContent?.Flow?.start?.[0]?.triggerType?.[0] ?? (flowContent?.Flow?.processType?.[0] || "ERROR (Unknown)"),
object: flowContent?.Flow?.start?.[0]?.object?.[0] || flowContent?.Flow?.processMetadataValues?.filter(pmv => pmv.name[0] === "ObjectType")?.[0]?.value?.[0]?.stringValue?.[0] || ""
});
const outputFlowMdFile = path.join(this.outputMarkdownRoot, "flows", path.basename(flowFile).replace(".flow-meta.xml", ".md"));
if (this.diffOnly && !updatedFlowNames.includes(flowName)) {
flowSkips.push(flowFile);
continue;
}
const outputFlowMdFile = path.join(this.outputMarkdownRoot, "flows", flowName + ".md");
const genRes = await generateFlowMarkdownFile(flowFile, flowXml, outputFlowMdFile);
if (!genRes) {
flowErrors.push(flowFile);
Expand All @@ -173,18 +189,21 @@ _sfdx-hardis docker image is alpine-based and does not succeed to run mermaid/pu
continue;
}
}
if (flowSkips.length > 0) {
uxLog(this, c.yellow(`Skipped generation for ${flowSkips.length} Flows that have not been updated: ${this.humanDisplay(flowSkips)}`));
}
uxLog(this, c.green(`Successfully generated ${flowFiles.length - flowWarnings.length - flowErrors.length} Flows documentation`));
if (flowWarnings.length > 0) {
uxLog(this, c.yellow(`Partially generated documentation (Markdown with mermaidJs but without SVG) for ${flowWarnings.length} Flows: ${flowWarnings.join(", ")}`));
uxLog(this, c.yellow(`Partially generated documentation (Markdown with mermaidJs but without SVG) for ${flowWarnings.length} Flows: ${this.humanDisplay(flowWarnings)}`));
}
if (flowErrors.length > 0) {
uxLog(this, c.yellow(`Error generating documentation for ${flowErrors.length} Flows: ${flowErrors.join(", ")}`));
uxLog(this, c.yellow(`Error generating documentation for ${flowErrors.length} Flows: ${this.humanDisplay(flowErrors)}`));
}

// Write table on doc index
const flowTableLines = await this.buildFlowsTable(flowDescriptions, 'flows/');
this.mdLines.push(...flowTableLines);
this.mdLines.push(...["___", ""])
this.mdLines.push(...["___", ""]);

// Write index file for flow folder
await fs.ensureDir(path.join(this.outputMarkdownRoot, "flows"));
Expand All @@ -194,6 +213,10 @@ _sfdx-hardis docker image is alpine-based and does not succeed to run mermaid/pu
uxLog(this, c.green(`Successfully generated doc index for Flows at ${flowIndexFile}`));
}

private humanDisplay(flows) {
return flows.map(flow => path.basename(flow, ".flow-meta.xml")).join(", ");
}

private async buildFlowsTable(flowDescriptions: any[], prefix: string) {
const lines: string[] = [];
lines.push(...[
Expand Down
2 changes: 1 addition & 1 deletion src/commands/hardis/org/monitor/backup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ This command is part of [sfdx-hardis Monitoring](${CONSTANTS.DOC_URL_ROOT}/sales

// Run project documentation generation
try {
await Project2Markdown.run();
await Project2Markdown.run(["--diff-only"]);
} catch (e: any) {
uxLog(this, c.yellow("Error while generating project documentation " + e.message));
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/utils/mermaidUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export async function generateMarkdownFileWithMermaidDocker(outputFlowMdFile: st
return true;
} catch (e: any) {
uxLog(this, c.yellow(`Error generating mermaidJs Graphs in ${outputFlowMdFile} documentation with Docker: ${e.message}`) + "\n" + c.grey(e.stack));
if (JSON.stringify(e).includes("Cannot connect to the Docker daemon")) {
if (JSON.stringify(e).includes("Cannot connect to the Docker daemon") || JSON.stringify(e).includes("daemon is not running")) {
globalThis.mermaidUnavailableTools = (globalThis.mermaidUnavailableTools || []).concat("docker");
uxLog(this, c.yellow("[Mermaid] Docker unavailable: do not try again"));
}
Expand Down

0 comments on commit ebeec02

Please sign in to comment.