Skip to content

Commit

Permalink
feat(cli): generate, add --no-hint argument (#24274)
Browse files Browse the repository at this point in the history
* feat(cli): generate no hint argument Closes #22513

* feat(cli): address failing tests

* addressed PR feedback

* trigger CI again

* consistent spelling of hints argument

* update message to trigger CI and run all tests rather than skipping some of them

* feat: fix verbage

* (fix): pnpm run lint

* (fix): skip unneded code paths and remove useless test

* Few stylistic changes

---------

Co-authored-by: Serhii Tatarintsev <[email protected]>
  • Loading branch information
nickcarnival and SevInf authored Jun 10, 2024
1 parent cea0267 commit a59dc6f
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 20 deletions.
47 changes: 28 additions & 19 deletions packages/cli/src/Generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ ${bold('Usage')}
${dim('$')} prisma generate [options]
${bold('Options')}
-h, --help Display this help message
--schema Custom path to your Prisma schema
--watch Watch the Prisma schema and rerun after a change
--generator Generator to use (may be provided multiple times)
--no-engine Generate a client for use with Accelerate only
--no-hints Hides the hint messages but still outputs errors and warnings
--allow-no-models Allow generating a client without models
${bold('Examples')}
Expand Down Expand Up @@ -107,6 +107,7 @@ ${bold('Examples')}
'--data-proxy': Boolean,
'--accelerate': Boolean,
'--no-engine': Boolean,
'--no-hints': Boolean,
'--generator': [String],
// Only used for checkpoint information
'--postinstall': String,
Expand Down Expand Up @@ -158,7 +159,6 @@ ${bold('Examples')}
Boolean(process.env.PRISMA_GENERATE_DATAPROXY) || // legacy, keep for backwards compatibility
Boolean(process.env.PRISMA_GENERATE_ACCELERATE) || // legacy, keep for backwards compatibility
Boolean(process.env.PRISMA_GENERATE_NO_ENGINE),
allowNoModels: Boolean(args['--allow-no-models']),
})

if (!generators || generators.length === 0) {
Expand Down Expand Up @@ -220,8 +220,10 @@ Please run \`prisma generate\` manually.`

if (!watchMode) {
const prismaClientJSGenerator = generators?.find(
(g) => g.options?.generator.provider && parseEnvValue(g.options?.generator.provider) === 'prisma-client-js',
({ options }) =>
options?.generator.provider && parseEnvValue(options?.generator.provider) === 'prisma-client-js',
)

let hint = ''
if (prismaClientJSGenerator) {
const generator = prismaClientJSGenerator.options?.generator
Expand All @@ -240,12 +242,15 @@ When using Deno, you need to define \`output\` in the client generator section o
),
)
: '@prisma/client'

const breakingChangesStr = printBreakingChangesMessage
? `
${breakingChangesMessage}`
: ''

const hideHints = args['--no-hints'] ?? false

const versionsOutOfSync = clientGeneratorVersion && pkg.version !== clientGeneratorVersion
const versionsWarning =
versionsOutOfSync && logger.should.warn()
Expand All @@ -256,18 +261,21 @@ This might lead to unexpected behavior.
Please make sure they have the same version.`
: ''

const tryAccelerateMessage = `Deploying your app to serverless or edge functions?
if (hideHints) {
hint = `${getHardcodedUrlWarning(config)}${breakingChangesStr}${versionsWarning}`
} else {
const tryAccelerateMessage = `Deploying your app to serverless or edge functions?
Try Prisma Accelerate for connection pooling and caching.
${link('https://pris.ly/cli/--accelerate')}`

const boxedTryAccelerateMessage = drawBox({
height: tryAccelerateMessage.split('\n').length,
width: 0, // calculated automatically
str: tryAccelerateMessage,
horizontalPadding: 2,
})
const boxedTryAccelerateMessage = drawBox({
height: tryAccelerateMessage.split('\n').length,
width: 0, // calculated automatically
str: tryAccelerateMessage,
horizontalPadding: 2,
})

hint = `
hint = `
Start using Prisma Client in Node.js (See: ${link('https://pris.ly/d/client')})
${dim('```')}
${highlightTS(`\
Expand All @@ -285,9 +293,10 @@ See other ways of importing Prisma Client: ${link('http://pris.ly/d/importing-cl
${boxedTryAccelerateMessage}
${getHardcodedUrlWarning(config)}${breakingChangesStr}${versionsWarning}`
if (generator?.previewFeatures.includes('driverAdapters')) {
if (generator?.isCustomOutput && isDeno) {
hint = `

if (generator?.previewFeatures.includes('driverAdapters')) {
if (generator?.isCustomOutput && isDeno) {
hint = `
${bold('Start using Prisma Client')}
${dim('```')}
${highlightTS(`\
Expand All @@ -296,8 +305,8 @@ const prisma = new PrismaClient()`)}
${dim('```')}
More information: https://pris.ly/d/client`
} else {
hint = `
} else {
hint = `
${bold('Start using Prisma Client')}
${dim('```')}
${highlightTS(`\
Expand All @@ -306,12 +315,13 @@ const prisma = new PrismaClient()`)}
${dim('```')}
More information: https://pris.ly/d/client`
}
}

hint = `${hint}
hint = `${hint}
${boxedTryAccelerateMessage}
${getHardcodedUrlWarning(config)}${breakingChangesStr}${versionsWarning}`
}
}
}

Expand Down Expand Up @@ -404,7 +414,6 @@ function getCurrentClientVersion(): string | null {
}
}
} catch (e) {
//
return null
}

Expand Down
43 changes: 43 additions & 0 deletions packages/cli/src/__tests__/commands/Generate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,49 @@ describe('using cli', () => {
}
})

it('should hide hints with --no-hints', async () => {
ctx.fixture('example-project')
const data = await ctx.cli('generate', '--no-hints')

if (typeof data.signal === 'number' && data.signal !== 0) {
throw new Error(data.stderr + data.stdout)
}

const engineType = getClientEngineType()

if (engineType === ClientEngineType.Binary) {
expect(data.stdout).toMatchInlineSnapshot(`
"Prisma schema loaded from prisma/schema.prisma
✔ Generated Prisma Client (v0.0.0, engine=binary) to ./generated/client in XXXms
"
`)
}

expect(data.stdout).toMatchInlineSnapshot(`
"Prisma schema loaded from prisma/schema.prisma
✔ Generated Prisma Client (v0.0.0) to ./generated/client in XXXms
"
`)
})

it('should work and not show hints with --no-hints and --no-engine', async () => {
ctx.fixture('example-project')
const data = await ctx.cli('generate', '--no-hints', '--no-engine')

if (typeof data.signal === 'number' && data.signal !== 0) {
throw new Error(data.stderr + data.stdout)
}

expect(data.stdout).toMatchInlineSnapshot(`
"Prisma schema loaded from prisma/schema.prisma
✔ Generated Prisma Client (v0.0.0, engine=none) to ./generated/client in XXXms
"
`)
})

it('should warn when `url` is hardcoded', async () => {
ctx.fixture('hardcoded-url')
const data = await ctx.cli('generate')
Expand Down
1 change: 1 addition & 0 deletions packages/generator-helper/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export type GeneratorOptions = {
binaryPaths?: BinaryPaths
postinstall?: boolean
noEngine?: boolean
noHints?: boolean
allowNoModels?: boolean
envPaths?: EnvPaths
}
Expand Down
2 changes: 1 addition & 1 deletion packages/get-platform/src/test-utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { jestConsoleContext, jestContext, jestProcessContext, type BaseContext } from './jestContext'
export { type BaseContext, jestConsoleContext, jestContext, jestProcessContext } from './jestContext'

0 comments on commit a59dc6f

Please sign in to comment.