Skip to content

Commit

Permalink
feat: Configuration of Mermaid via .js file (#232)
Browse files Browse the repository at this point in the history
* feat: Enable override of mermaid configuration via a .js file

* fix: Move theme to mermaid config and out of cli extensions

* remove mermaid types stub

* fix theme for readme erd
  • Loading branch information
keonik committed Aug 10, 2023
1 parent b7137d2 commit 46d6ad5
Show file tree
Hide file tree
Showing 8 changed files with 3,617 additions and 2,290 deletions.
2 changes: 1 addition & 1 deletion ERD.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions example-mermaid-config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/** @type {import('mermaid').MermaidConfig} */
const config = {
deterministicIds: true,
maxTextSize: 90000,
er: {
/**
* When this flag is set to `true`, the height and width is set to 100%
* and is then scaled with the available space.
* If set to `false`, the absolute space required is used.
*
*/
useMaxWidth: false,
},
theme: 'default',
};

module.exports = config;
5,841 changes: 3,564 additions & 2,277 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
"babel-jest": "^29.6.2",
"concurrently": "^8.2.0",
"jest": "^29.6.2",
"mermaid": "^10.3.0",
"prettier": "3.0.1",
"prisma": "^4.0.0 || ^5.0.0",
"puppeteer": "^21.0.2",
Expand Down
9 changes: 5 additions & 4 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ datasource db {
}

generator erd {
provider = "node ./dist/index.js"
output = "../ERD.svg"
theme = "default"
mmdcPath = "node_modules/.bin"
provider = "node ./dist/index.js"
output = "../ERD.svg"
theme = "default"
mmdcPath = "node_modules/.bin"
mermaidConfig = "example-mermaid-config.js"
}

model User {
Expand Down
35 changes: 27 additions & 8 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
DMLModel,
DMLField,
} from 'types/dml';
import { MermaidConfig } from 'mermaid';

dotenv.config(); // Load the environment variables

Expand Down Expand Up @@ -398,14 +399,32 @@ export default async (options: GeneratorOptions) => {
fs.writeFileSync(tempMermaidFile, mermaid);

// default config parameters https://github.com/mermaid-js/mermaid/blob/master/packages/mermaid/src/defaultConfig.ts
const defaultMermaidConfig: MermaidConfig = {
deterministicIds: true,
maxTextSize: 90000,
er: {
useMaxWidth: true,
},
theme: theme,
};
let mermaidConfig = defaultMermaidConfig;

if (config?.mermaidConfig) {
const importedMermaidConfig = await import(
path.resolve(config.mermaidConfig)
);
if (debug) {
console.log('imported mermaid config: ', importedMermaidConfig);
}
// merge default config with imported config
mermaidConfig = {
...defaultMermaidConfig,
...importedMermaidConfig,
};
}

const tempConfigFile = path.resolve(path.join(tmpDir, 'config.json'));
fs.writeFileSync(
tempConfigFile,
JSON.stringify({
deterministicIds: true,
maxTextSize: 90000,
})
);
fs.writeFileSync(tempConfigFile, JSON.stringify(mermaidConfig));

// Generator option to adjust puppeteer
let puppeteerConfig = config.puppeteerConfig;
Expand Down Expand Up @@ -478,7 +497,7 @@ export default async (options: GeneratorOptions) => {
}
}

const mermaidCommand = `"${mermaidCliNodePath}" -i "${tempMermaidFile}" -o "${output}" -t ${theme} -c "${tempConfigFile}" -p "${puppeteerConfig}"`;
const mermaidCommand = `"${mermaidCliNodePath}" -i "${tempMermaidFile}" -o "${output}" -c "${tempConfigFile}" -p "${puppeteerConfig}"`;
if (debug && mermaidCommand)
console.log('mermaid command: ', mermaidCommand);
child_process.execSync(mermaidCommand, {
Expand Down
1 change: 1 addition & 0 deletions src/types/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export interface PrismaERDConfig {
includeRelationFromFields?: string;
erdDebug?: string;
puppeteerConfig?: string;
mermaidConfig?: string;
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"compilerOptions": {
"skipLibCheck": true,
"target": "es2019",
"baseUrl": "src",
"outDir": "dist",
Expand Down

0 comments on commit 46d6ad5

Please sign in to comment.