(directory: string, datamodel: string, schemaPath: string)
| 555 | } |
| 556 | |
| 557 | async function verifyOutputDirectory(directory: string, datamodel: string, schemaPath: string) { |
| 558 | let content: string |
| 559 | try { |
| 560 | content = await fs.readFile(path.join(directory, 'package.json'), 'utf8') |
| 561 | } catch (e) { |
| 562 | if (e.code === 'ENOENT') { |
| 563 | // no package.json exists, we are good |
| 564 | return |
| 565 | } |
| 566 | throw e |
| 567 | } |
| 568 | const { name } = JSON.parse(content) |
| 569 | if (name === clientPkg.name) { |
| 570 | const message = [`Generating client into ${bold(directory)} is not allowed.`] |
| 571 | message.push('This package is used by `prisma generate` and overwriting its content is dangerous.') |
| 572 | message.push('') |
| 573 | message.push('Suggestion:') |
| 574 | const outputDeclaration = findOutputPathDeclaration(datamodel) |
| 575 | |
| 576 | if (outputDeclaration && outputDeclaration.content.includes(clientPkg.name)) { |
| 577 | const outputLine = outputDeclaration.content |
| 578 | message.push(`In ${bold(schemaPath)} replace:`) |
| 579 | message.push('') |
| 580 | message.push(`${dim(outputDeclaration.lineNumber)} ${replacePackageName(outputLine, red(clientPkg.name))}`) |
| 581 | message.push('with') |
| 582 | |
| 583 | message.push(`${dim(outputDeclaration.lineNumber)} ${replacePackageName(outputLine, green('.prisma/client'))}`) |
| 584 | } else { |
| 585 | message.push(`Generate client into ${bold(replacePackageName(directory, green('.prisma/client')))} instead`) |
| 586 | } |
| 587 | |
| 588 | message.push('') |
| 589 | message.push("You won't need to change your imports.") |
| 590 | message.push('Imports from `@prisma/client` will be automatically forwarded to `.prisma/client`') |
| 591 | const error = new Error(message.join('\n')) |
| 592 | throw error |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | function replacePackageName(directoryPath: string, replacement: string): string { |
| 597 | return directoryPath.replace(clientPkg.name, replacement) |
no test coverage detected