Skip to content

Commit

Permalink
fix: Trim mmdc from mmdcPath. Added test and documentation for how to…
Browse files Browse the repository at this point in the history
… utilize new config ption. Resolve tests from nullable and primary key changes.
  • Loading branch information
keonik committed Mar 28, 2023
1 parent 7fc4410 commit 9c56a47
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,15 @@ Options

### mmdcPath

In order for this generator to succeed you must have `mmdc` installed. This is the mermaid cli tool that is used to generate the ERD. By default the generator searches for an existing binary file at `/node_modules/.bin/mmdc`. If it fails to find that binary it will run `find ../.. -name mmdc` to search through your folder for a `mmdc` binary. If you are using a different package manager or have a different location for your binary files, you can specify the path to the binary file.
In order for this generator to succeed you must have `mmdc` installed. This is the mermaid cli tool that is used to generate the ERD. By default the generator searches for an existing binary file at `/node_modules/.bin`. If it fails to find that binary it will run `find ../.. -name mmdc` to search through your folder for a `mmdc` binary. If you are using a different package manager or have a different location for your binary files, you can specify the path to the binary file.

```prisma
generator erd {
provider = "prisma-erd-generator"
theme = "forest"
mmcdPath = "node_modules/.bin"
}
```

### Disabled

Expand Down
2 changes: 1 addition & 1 deletion __tests__/compositePk.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ test('composite-pk.prisma', async () => {
.execSync(`cat ${folderName}/${fileName}`)
.toString();

const pks = svgAsString.match(/PK/g);
const pks = svgAsString.match(/🗝️/g);

// did it generate a file with the correct content
expect(svgAsString).toContain(`<svg`);
Expand Down
18 changes: 18 additions & 0 deletions __tests__/mmdcPath.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import * as child_process from 'child_process';

test('setting mmdcPath works', async () => {
const fileName = 'mmdcPath.svg';
const folderName = '__tests__';
child_process.execSync(`rm -f ${folderName}/${fileName}`);
child_process.execSync(`prisma generate --schema ./prisma/mmdcPath.prisma`);
const svgContent = child_process
.execSync(`cat ${folderName}/${fileName}`)
.toString();
// did the model get added
expect(svgContent).toContain('users');

// User has id
expect(svgContent).toMatch(
/id="text-entity-users([^\><]*)-attr-1-name"([^<\>]*)\>id<\/text\>/
);
});
2 changes: 1 addition & 1 deletion __tests__/nullables.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('nullables.prisma', async () => {

// did it generate a file with the correct content
expect(svgAsString).toContain(`<svg`);
expect(svgAsString).toContain(`nullable`);
expect(svgAsString).toContain(``);
expect(svgAsString).toContain(`inviteeEmail`);
expect(svgAsString).toContain(`cancelCode`);
expect(svgAsString).toContain(`name`);
Expand Down
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
},
"dependencies": {
"@mermaid-js/mermaid-cli": "^10.0.2",
"@prisma/client": "^4.11.0",
"@prisma/generator-helper": "^4.11.0",
"dotenv": "^16.0.3"
},
"peerDependencies": {
Expand Down
24 changes: 24 additions & 0 deletions prisma/mmdcPath.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

generator erd {
provider = "node ./dist/index.js"
output = "../__tests__/mmdcPath.svg"
theme = "default"
mmdcPath = "node_modules/.bin"
}

model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String
username String @unique
email String @unique
hashedPassword String? @map("hashed_password")
role String @default("user")
@@map("users")
}
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ generator erd {
provider = "node ./dist/index.js"
output = "../ERD.svg"
theme = "default"
mmdcPath = "node_modules/.bin"
}

model User {
Expand Down
11 changes: 8 additions & 3 deletions src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ export default async (options: GeneratorOptions) => {
const config = options.generator.config;
const theme = config.theme || 'forest';
let mermaidCliNodePath = path.resolve(
config.mmdcPath || path.join('node_modules', '.bin', 'mmdc')
path.join(config.mmdcPath || 'node_modules/.bin', 'mmdc')
);
const tableOnly = config.tableOnly === 'true';
const includeRelationFromFields =
Expand Down Expand Up @@ -444,8 +444,13 @@ export default async (options: GeneratorOptions) => {
maxTextSize: 90000,
})
);

if (!fs.existsSync(mermaidCliNodePath)) {
if (config.mmdcPath) {
if (!fs.existsSync(mermaidCliNodePath)) {
throw new Error(
`\nMermaid CLI provided path does not exist. \n${mermaidCliNodePath}`
);
}
} else if (!fs.existsSync(mermaidCliNodePath)) {
const findMermaidCli = child_process
.execSync('find ../.. -name mmdc')
.toString()
Expand Down

0 comments on commit 9c56a47

Please sign in to comment.