/
getGenerators.ts
667 lines (600 loc) · 20.8 KB
/
getGenerators.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
import Debug from '@prisma/debug'
import { fixBinaryTargets, printGeneratorConfig } from '@prisma/engine-core'
import { enginesVersion } from '@prisma/engines'
import {
BinaryDownloadConfiguration,
download,
DownloadOptions,
EngineTypes,
} from '@prisma/fetch-engine'
import {
BinaryPaths,
EngineType,
GeneratorConfig,
GeneratorManifest,
GeneratorOptions,
} from '@prisma/generator-helper'
import { getPlatform, Platform } from '@prisma/get-platform'
import chalk from 'chalk'
import fs from 'fs'
import makeDir from 'make-dir'
import pMap from 'p-map'
import path from 'path'
import { getConfig, getDMMF } from './engineCommands'
import { Generator } from './Generator'
import { engineVersions } from './getAllVersions'
import { pick } from './pick'
import {
GeneratorPaths,
predefinedGeneratorResolvers,
} from './predefinedGeneratorResolvers'
import { resolveOutput } from './resolveOutput'
import { extractPreviewFeatures } from './utils/extractPreviewFeatures'
import { mapPreviewFeatures } from './utils/mapPreviewFeatures'
import { missingDatasource } from './utils/missingDatasource'
import { missingModelMessage } from './utils/missingGeneratorMessage'
import { parseEnvValue } from './utils/parseEnvValue'
import { printConfigWarnings } from './utils/printConfigWarnings'
const debug = Debug('prisma:getGenerators')
export type ProviderAliases = { [alias: string]: GeneratorPaths }
type BinaryPathsOverride = {
[P in EngineType]?: string
}
export type GetGeneratorOptions = {
schemaPath: string
providerAliases?: ProviderAliases
cliVersion?: string
version?: string
printDownloadProgress?: boolean
baseDir?: string // useful in tests to resolve the base dir from which `output` is resolved
overrideGenerators?: GeneratorConfig[]
skipDownload?: boolean
binaryPathsOverride?: BinaryPathsOverride
}
/**
* Makes sure that all generators have the binaries they deserve and returns a
* `Generator` class per generator defined in the schema.prisma file.
* In other words, this is basically a generator factory function.
* @param schemaPath Path to schema.prisma
* @param aliases Aliases like `prisma-client-js` -> `node_modules/@prisma/client/generator-build/index.js`
*/
export async function getGenerators({
schemaPath,
providerAliases: aliases, // do you get the pun?
version,
cliVersion,
printDownloadProgress,
baseDir = path.dirname(schemaPath),
overrideGenerators,
skipDownload,
binaryPathsOverride,
}: GetGeneratorOptions): Promise<Generator[]> {
if (!schemaPath) {
throw new Error(
`schemaPath for getGenerators got invalid value ${schemaPath}`,
)
}
if (!fs.existsSync(schemaPath)) {
throw new Error(`${schemaPath} does not exist`)
}
const platform = await getPlatform()
let prismaPath: string | undefined = binaryPathsOverride?.queryEngine
// overwrite query engine if the version is provided
if (version && !prismaPath) {
const potentialPath = eval(`require('path').join(__dirname, '..')`)
// for pkg we need to make an exception
if (!potentialPath.startsWith('/snapshot/')) {
const downloadParams: DownloadOptions = {
binaries: {
'query-engine': potentialPath,
},
binaryTargets: [platform],
showProgress: false,
version,
skipDownload,
}
const binaryPathsWithEngineType = await download(downloadParams)
prismaPath = binaryPathsWithEngineType['query-engine']![platform]
}
}
const datamodel = fs.readFileSync(schemaPath, 'utf-8')
const config = await getConfig({
datamodel,
datamodelPath: schemaPath,
prismaPath,
ignoreEnvVarErrors: true,
})
if (config.datasources.length === 0) {
throw new Error(missingDatasource)
}
printConfigWarnings(config.warnings)
// TODO: This needs a better abstraction, but we don't have any better right now
const experimentalFeatures = mapPreviewFeatures(
extractPreviewFeatures(config),
)
const dmmf = await getDMMF({
datamodel,
datamodelPath: schemaPath,
prismaPath,
enableExperimental: experimentalFeatures,
})
if (dmmf.datamodel.models.length === 0) {
throw new Error(missingModelMessage)
}
const generatorConfigs = overrideGenerators || config.generators
await validateGenerators(generatorConfigs)
const runningGenerators: Generator[] = []
try {
// 1. Get all generators
const generators = await pMap(
generatorConfigs,
async (generator, index) => {
let generatorPath = parseEnvValue(generator.provider)
let paths: GeneratorPaths | undefined
// as of now mostly used by studio
const providerValue = parseEnvValue(generator.provider)
if (aliases && aliases[providerValue]) {
generatorPath = aliases[providerValue].generatorPath
paths = aliases[providerValue]
} else if (predefinedGeneratorResolvers[providerValue]) {
paths = await predefinedGeneratorResolvers[providerValue](
baseDir,
cliVersion,
)
generatorPath = paths.generatorPath
}
const generatorInstance = new Generator(
generatorPath,
generator,
paths?.isNode,
)
await generatorInstance.init()
// resolve output path
if (generator.output) {
generator.output = {
value: path.resolve(baseDir, parseEnvValue(generator.output)),
fromEnvVar: null,
}
generator.isCustomOutput = true
} else if (paths) {
generator.output = {
value: paths.outputPath,
fromEnvVar: null,
}
} else {
if (
!generatorInstance.manifest ||
!generatorInstance.manifest.defaultOutput
) {
throw new Error(
`Can't resolve output dir for generator ${chalk.bold(
generator.name,
)} with provider ${chalk.bold(generator.provider)}.
The generator needs to either define the \`defaultOutput\` path in the manifest or you need to define \`output\` in the datamodel.prisma file.`,
)
}
generator.output = {
value: await resolveOutput({
defaultOutput: generatorInstance.manifest.defaultOutput,
baseDir,
}),
fromEnvVar: 'null',
}
}
const options: GeneratorOptions = {
datamodel,
datasources: config.datasources,
generator,
dmmf,
otherGenerators: skipIndex(generatorConfigs, index),
schemaPath,
version: version || enginesVersion, // this version makes no sense anymore and should be ignored
}
// we set the options here a bit later after instantiating the Generator,
// as we need the generator manifest to resolve the `output` dir
generatorInstance.setOptions(options)
runningGenerators.push(generatorInstance)
return generatorInstance
},
{
stopOnError: false, // needed so we can first make sure all generators are created properly, then cleaned up properly
},
)
// 2. Check, if all required generators are there.
// Generators can say in their "requiresGenerators" property in the manifest, which other generators they depend on
// This has mostly been introduced for 3rd party generators, which rely on `prisma-client-js`.
const generatorProviders: string[] = generatorConfigs.map((g) =>
parseEnvValue(g.provider),
)
for (const g of generators) {
if (
g?.manifest?.requiresGenerators &&
g?.manifest?.requiresGenerators.length > 0
) {
for (const neededGenerator of g?.manifest?.requiresGenerators) {
if (!generatorProviders.includes(neededGenerator)) {
throw new Error(
`Generator "${g.manifest.prettyName}" requires generator "${neededGenerator}", but it is missing in your schema.prisma.
Please add it to your schema.prisma:
generator gen {
provider = "${neededGenerator}"
}
`,
)
}
}
}
}
// 3. Download all binaries and binary targets needed
const neededVersions = Object.create(null)
for (const g of generators) {
if (
g.manifest?.requiresEngines &&
Array.isArray(g.manifest?.requiresEngines) &&
g.manifest.requiresEngines.length > 0
) {
const neededVersion = getEngineVersionForGenerator(g.manifest, version)
if (!neededVersions[neededVersion]) {
neededVersions[neededVersion] = { engines: [], binaryTargets: [] }
}
for (const engine of g.manifest?.requiresEngines) {
if (!neededVersions[neededVersion].engines.includes(engine)) {
neededVersions[neededVersion].engines.push(engine)
}
}
if (
g.options?.generator?.binaryTargets &&
g.options?.generator?.binaryTargets.length > 0
) {
for (let binaryTarget of g.options?.generator?.binaryTargets) {
if (binaryTarget === 'native') {
binaryTarget = platform
}
if (
!neededVersions[neededVersion].binaryTargets.includes(
binaryTarget,
)
) {
neededVersions[neededVersion].binaryTargets.push(binaryTarget)
}
}
}
}
}
debug({ neededVersions })
const binaryPathsByVersion = await getBinaryPathsByVersion({
neededVersions,
platform,
version,
printDownloadProgress,
skipDownload,
binaryPathsOverride,
})
for (const generator of generators) {
if (generator.manifest && generator.manifest.requiresEngines) {
const engineVersion = getEngineVersionForGenerator(
generator.manifest,
version,
)
const binaryPaths = binaryPathsByVersion[engineVersion]
// pick only the engines that we need for this generator
const generatorBinaryPaths = pick(
binaryPaths,
generator.manifest.requiresEngines,
)
debug({ generatorBinaryPaths })
generator.setBinaryPaths(generatorBinaryPaths)
// in case cli engine version !== client engine version
// we need to re-generate the dmmf and pass it in to the generator
if (
engineVersion !== version &&
generator.options &&
generator.manifest.requiresEngines.includes('queryEngine') &&
generatorBinaryPaths.queryEngine &&
generatorBinaryPaths.queryEngine[platform]
) {
const customDmmf = await getDMMF({
datamodel,
datamodelPath: schemaPath,
prismaPath: generatorBinaryPaths.queryEngine[platform],
enableExperimental: experimentalFeatures,
})
const options = { ...generator.options, dmmf: customDmmf }
debug(generator.manifest.prettyName)
debug(options)
generator.setOptions(options)
}
}
}
return generators
} catch (e) {
// make sure all generators that are already running are being stopped
runningGenerators.forEach((g) => g.stop())
throw e
}
}
type GetBinaryPathsByVersionInput = {
neededVersions: Record<string, any>
platform: string
version?: string
printDownloadProgress?: boolean
skipDownload?: boolean
binaryPathsOverride?: BinaryPathsOverride
}
async function getBinaryPathsByVersion({
neededVersions,
platform,
version,
printDownloadProgress,
skipDownload,
binaryPathsOverride,
}: GetBinaryPathsByVersionInput): Promise<Record<string, BinaryPaths>> {
const binaryPathsByVersion: Record<string, BinaryPaths> = Object.create(null)
// make sure, that at least the current platform is being fetched
for (const currentVersion in neededVersions) {
// ensure binaryTargets are set correctly
const neededVersion = neededVersions[currentVersion]
if (neededVersion.binaryTargets.length === 0) {
neededVersion.binaryTargets.push(platform)
if (neededVersion.binaryTargets.length === 0) {
neededVersion.binaryTargets = [platform]
}
}
if (
process.env.NETLIFY &&
!neededVersion.binaryTargets.includes('rhel-openssl-1.0.x')
) {
neededVersion.binaryTargets.push('rhel-openssl-1.0.x')
}
// download
let binaryTargetBaseDir = eval(`require('path').join(__dirname, '..')`)
if (version !== currentVersion) {
binaryTargetBaseDir = path.join(
binaryTargetBaseDir,
`./engines/${currentVersion}/`,
)
await makeDir(binaryTargetBaseDir).catch((e) => console.error(e))
}
const binariesConfig: BinaryDownloadConfiguration = neededVersion.engines.reduce(
(acc, curr) => {
// only download the binary, of not already covered by the `binaryPathsOverride`
if (!binaryPathsOverride?.[curr]) {
acc[engineTypeToBinaryType(curr)] = binaryTargetBaseDir
}
return acc
},
Object.create(null),
)
binaryPathsByVersion[currentVersion] = {}
if (Object.values(binariesConfig).length > 0) {
const downloadParams: DownloadOptions = {
binaries: binariesConfig,
binaryTargets: neededVersion.binaryTargets,
showProgress:
typeof printDownloadProgress === 'boolean'
? printDownloadProgress
: true,
version:
currentVersion && currentVersion !== 'latest'
? currentVersion
: enginesVersion,
skipDownload,
}
const binaryPathsWithEngineType = await download(downloadParams)
const binaryPaths = mapKeys(
binaryPathsWithEngineType,
binaryTypeToEngineType,
)
binaryPathsByVersion[currentVersion] = binaryPaths
}
if (binaryPathsOverride) {
const overrideBinaries = Object.keys(binaryPathsOverride)
const binariesCoveredByOverride = neededVersion.engines.filter((e) =>
overrideBinaries.includes(e),
)
if (binariesCoveredByOverride.length > 0) {
for (const bin of binariesCoveredByOverride) {
binaryPathsByVersion[currentVersion][bin] = {
[platform]: binaryPathsOverride[bin],
}
}
}
}
}
return binaryPathsByVersion
}
/**
* Shortcut for getGenerators, if there is only one generator defined. Useful for testing.
* @param schemaPath path to schema.prisma
* @param aliases Aliases like `photonjs` -> `node_modules/photonjs/gen.js`
* @param version Version of the binary, commit hash of https://github.com/prisma/prisma-engine/commits/master
* @param printDownloadProgress `boolean` to print download progress or not
*/
export async function getGenerator(
options: GetGeneratorOptions,
): Promise<Generator> {
const generators = await getGenerators(options)
return generators[0]
}
export function skipIndex<T = any>(arr: T[], index: number): T[] {
return [...arr.slice(0, index), ...arr.slice(index + 1)]
}
export const knownBinaryTargets: Platform[] = [
'native',
'darwin',
'debian-openssl-1.0.x',
'debian-openssl-1.1.x',
'linux-arm-openssl-1.0.x',
'linux-arm-openssl-1.1.x',
'rhel-openssl-1.0.x',
'rhel-openssl-1.1.x',
'linux-musl',
'linux-nixos',
'windows',
'freebsd11',
'freebsd12',
'openbsd',
'netbsd',
'arm',
]
const oldToNewBinaryTargetsMapping = {
'linux-glibc-libssl1.0.1': 'debian-openssl-1.0.x',
'linux-glibc-libssl1.0.2': 'debian-openssl-1.0.x',
'linux-glibc-libssl1.1.0': 'debian-openssl1.1.x',
}
async function validateGenerators(
generators: GeneratorConfig[],
): Promise<void> {
const platform = await getPlatform()
for (const generator of generators) {
if (parseEnvValue(generator.provider) === 'photonjs') {
throw new Error(`Oops! Photon has been renamed to Prisma Client. Please make the following adjustments:
1. Rename ${chalk.red('provider = "photonjs"')} to ${chalk.green(
'provider = "prisma-client-js"',
)} in your ${chalk.bold('schema.prisma')} file.
2. Replace your ${chalk.bold('package.json')}'s ${chalk.red(
'@prisma/photon',
)} dependency to ${chalk.green('@prisma/client')}
3. Replace ${chalk.red(
"import { Photon } from '@prisma/photon'",
)} with ${chalk.green(
"import { PrismaClient } from '@prisma/client'",
)} in your code.
4. Run ${chalk.green('prisma generate')} again.
`)
}
if (generator.config.platforms) {
throw new Error(
`The \`platforms\` field on the generator definition is deprecated. Please rename it to \`binaryTargets\`.`,
)
}
if (generator.config.pinnedBinaryTargets) {
throw new Error(
`The \`pinnedBinaryTargets\` field on the generator definition is deprecated.
Please use the PRISMA_QUERY_ENGINE_BINARY env var instead to pin the binary target.`,
)
}
if (generator.binaryTargets) {
for (const binaryTarget of generator.binaryTargets) {
if (oldToNewBinaryTargetsMapping[binaryTarget]) {
throw new Error(
`Binary target ${chalk.red.bold(
binaryTarget,
)} is deprecated. Please use ${chalk.green.bold(
oldToNewBinaryTargetsMapping[binaryTarget],
)} instead.`,
)
}
if (!knownBinaryTargets.includes(binaryTarget as Platform)) {
throw new Error(
`Unknown binary target ${chalk.red(
binaryTarget,
)} in generator ${chalk.bold(generator.name)}.
Possible binaryTargets: ${chalk.greenBright(knownBinaryTargets.join(', '))}`,
)
}
}
const binaryTargets =
generator.binaryTargets && generator.binaryTargets.length > 0
? generator.binaryTargets
: ['native']
const resolvedBinaryTargets = binaryTargets.map((p) =>
p === 'native' ? platform : p,
)
if (!resolvedBinaryTargets.includes(platform)) {
if (generator) {
console.log(`${chalk.yellow(
'Warning:',
)} Your current platform \`${chalk.bold(
platform,
)}\` is not included in your generator's \`binaryTargets\` configuration ${JSON.stringify(
generator.binaryTargets,
)}.
To fix it, use this generator config in your ${chalk.bold('schema.prisma')}:
${chalk.greenBright(
printGeneratorConfig({
...generator,
binaryTargets: fixBinaryTargets(
generator.binaryTargets as any[],
platform,
),
}),
)}
${chalk.gray(
`Note, that by providing \`native\`, Prisma Client automatically resolves \`${platform}\`.
Read more about deploying Prisma Client: ${chalk.underline(
'https://github.com/prisma/prisma/blob/master/docs/core/generators/prisma-client-js.md',
)}`,
)}\n`)
} else {
console.log(
`${chalk.yellow('Warning')} The binaryTargets ${JSON.stringify(
binaryTargets,
)} don't include your local platform ${platform}, which you can also point to with \`native\`.
In case you want to fix this, you can provide ${chalk.greenBright(
`binaryTargets: ${JSON.stringify(['native', ...(binaryTargets || [])])}`,
)} in the schema.prisma file.`,
)
}
}
}
}
}
function engineTypeToBinaryType(
engineType: EngineType,
): keyof BinaryDownloadConfiguration {
if (engineType === 'introspectionEngine') {
return EngineTypes.introspectionEngine
}
if (engineType === 'migrationEngine') {
return EngineTypes.migrationEngine
}
if (engineType === 'queryEngine') {
return EngineTypes.queryEngine
}
if (engineType === 'libqueryEngineNapi') {
return EngineTypes.libqueryEngineNapi
}
if (engineType === 'prismaFmt') {
return EngineTypes.prismaFmt
}
throw new Error(`Could not convert engine type ${engineType}`)
}
function binaryTypeToEngineType(binaryType: string): string {
if (binaryType === EngineTypes.introspectionEngine) {
return 'introspectionEngine'
}
if (binaryType === EngineTypes.migrationEngine) {
return 'migrationEngine'
}
if (binaryType === EngineTypes.libqueryEngineNapi) {
return 'libqueryEngineNapi'
}
if (binaryType === EngineTypes.queryEngine) {
return 'queryEngine'
}
if (binaryType === EngineTypes.prismaFmt) {
return 'prismaFmt'
}
throw new Error(`Could not convert binary type ${binaryType}`)
}
function mapKeys<T extends object>(
obj: T,
mapper: (key: keyof T) => string,
): any {
return Object.entries(obj).reduce((acc, [key, value]) => {
acc[mapper(key as keyof T)] = value
return acc
}, {})
}
function getEngineVersionForGenerator(
manifest?: GeneratorManifest,
defaultVersion?: string | undefined,
): string {
let neededVersion: string = manifest!.requiresEngineVersion!
if (manifest?.version && engineVersions[manifest?.version]) {
neededVersion = engineVersions[manifest?.version]
}
neededVersion = neededVersion ?? defaultVersion // default to CLI version otherwise, if not provided
return neededVersion ?? 'latest'
}