Skip to content

Commit

Permalink
Update clearDeadDiagrams to remove unused diagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
SPWwj committed Aug 20, 2023
1 parent e30f5d5 commit d0ff17d
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/core/src/plugins/default/markbind-plugin-plantuml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { MbNode } from '../../utils/node';

interface DiagramStatus {
isFresh: boolean;
filePath: string;
}

const LockManager = require('../../utils/LockManager');
Expand All @@ -36,7 +37,14 @@ const processedDiagrams = new Map<string, DiagramStatus>();
function clearDeadDiagrams(diagrams: Map<string, DiagramStatus>) {
Array.from(diagrams.entries())
.filter(([, value]) => !value.isFresh)
.forEach(([key]) => diagrams.delete(key));
.forEach(([key, value]) => {

Check warning on line 40 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L37-L40

Added lines #L37 - L40 were not covered by tests
if (fs.existsSync(value.filePath)) {
fs.unlink(value.filePath, (err) => {

Check warning on line 42 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L42

Added line #L42 was not covered by tests
if (err) logger.error(`Error generating ${value.filePath}`);
});
}
diagrams.delete(key);

Check warning on line 46 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L46

Added line #L46 was not covered by tests
});
}

// Set all diagrams to be stale (isFresh = false)
Expand Down Expand Up @@ -71,14 +79,16 @@ function generateDiagram(imageOutputPath: string, content: string) {
return;

Check warning on line 79 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L77-L79

Added lines #L77 - L79 were not covered by tests
}

processedDiagrams.set(hashKey, { isFresh: true });

// Creates output dir if it doesn't exist
const outputDir = path.dirname(imageOutputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const lockId = LockManager.createLock();

// Add the diagram to the map
processedDiagrams.set(hashKey, { isFresh: true, filePath: imageOutputPath });

// Java command to launch PlantUML jar
const cmd = `java -jar "${JAR_PATH}" -nometadata -pipe > "${imageOutputPath}"`;
const childProcess = exec(cmd, {
Expand Down

0 comments on commit d0ff17d

Please sign in to comment.