fix: Diagram load issue

This commit is contained in:
Sidharth Vinod 2022-10-15 00:28:21 +05:30
parent 16be835c9b
commit 58a53c0fa8
No known key found for this signature in database
GPG Key ID: FB5CCD378D3907CD
5 changed files with 29 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import pkg from '../package.json' assert { type: 'json' };
const { dependencies } = pkg;
const watch = process.argv.includes('--watch');
const mermaidOnly = process.argv.includes('--mermaid');
const __dirname = fileURLToPath(new URL('.', import.meta.url));
type OutputOptions = Exclude<
@ -129,14 +130,19 @@ const buildPackage = async (entryName: keyof typeof packageOptions) => {
const main = async () => {
const packageNames = Object.keys(packageOptions) as (keyof typeof packageOptions)[];
for (const pkg of packageNames) {
if (mermaidOnly && pkg !== 'mermaid') {
continue;
}
await buildPackage(pkg);
}
};
if (watch) {
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid' }));
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-mindmap' }));
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-example-diagram' }));
build(getBuildConfig({ minify: false, watch, core: true, entryName: 'mermaid' }));
if (!mermaidOnly) {
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-mindmap' }));
build(getBuildConfig({ minify: false, watch, entryName: 'mermaid-example-diagram' }));
}
} else {
void main();
}

View File

@ -26,6 +26,7 @@
"git graph"
],
"scripts": {
"build:mermaid": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts --mermaid",
"build:vite": "ts-node-esm --transpileOnly --project=.vite/tsconfig.json .vite/build.ts",
"build:types": "concurrently \"tsc -p ./packages/mermaid/tsconfig.json --emitDeclarationOnly\" \"tsc -p ./packages/mermaid-mindmap/tsconfig.json --emitDeclarationOnly\"",
"build:watch": "pnpm build:vite --watch",

View File

@ -1,6 +1,6 @@
import * as configApi from './config';
import { log } from './logger';
import { getDiagram, registerDiagram } from './diagram-api/diagramAPI';
import { DiagramNotFoundError, getDiagram, registerDiagram } from './diagram-api/diagramAPI';
import { detectType, getDiagramLoader } from './diagram-api/detectType';
import { isDetailedError } from './utils';
export class Diagram {
@ -88,9 +88,13 @@ export const getDiagramFromText = async (txt: string, parseError?: Function): Pr
// Trying to find the diagram
getDiagram(type);
} catch (error) {
if (!(error instanceof DiagramNotFoundError)) {
log.error(error);
throw error;
}
const loader = getDiagramLoader(type);
if (!loader) {
throw new Error(`Diagram ${type} not found.`);
throw new Error(`Loader for ${type} not found.`);
}
// Diagram not available, loading it
const { diagram } = await loader();

View File

@ -34,6 +34,7 @@ export const registerDiagram = (
_setupGraphViewbox: any
) => void
) => {
log.debug(`Registering diagram ${id}`);
if (diagrams[id]) {
log.warn(`Diagram ${id} already registered.`);
// The error throw is commented out to as it breaks pages where you have multiple diagrams,
@ -50,11 +51,19 @@ export const registerDiagram = (
if (typeof callback !== 'undefined') {
callback(log, setLogLevel, getConfig, sanitizeText, setupGraphViewbox);
}
log.debug(`Registered diagram ${id}. ${Object.keys(diagrams).join(', ')} diagrams registered.`);
};
export const getDiagram = (name: string): DiagramDefinition => {
log.debug(`Getting diagram ${name}. ${Object.keys(diagrams).join(', ')} diagrams registered.`);
if (name in diagrams) {
return diagrams[name];
}
throw new Error(`Diagram ${name} not found.`);
throw new DiagramNotFoundError(name);
};
export class DiagramNotFoundError extends Error {
constructor(message: string) {
super(`Diagram ${message} not found.`);
}
}

View File

@ -30,6 +30,8 @@ export const log: Record<keyof typeof LEVELS, typeof console.log> = {
* @param {LogLevel} [level="fatal"] The level to set the logging to. Default is `"fatal"`
*/
export const setLogLevel = function (level: keyof typeof LEVELS | number | string = 'fatal') {
// TODO: Even if we call initialize with loglevel 0, many initial logs are skipped as LL is set to 5 initially.
let numericLevel: number = LEVELS.fatal;
if (typeof level === 'string') {
level = level.toLowerCase();
@ -39,6 +41,7 @@ export const setLogLevel = function (level: keyof typeof LEVELS | number | strin
} else if (typeof level === 'number') {
numericLevel = level;
}
numericLevel = 0;
log.trace = () => {};
log.debug = () => {};
log.info = () => {};