-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
generateInFolder.ts
160 lines (141 loc) · 4.58 KB
/
generateInFolder.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
import Debug from '@prisma/debug'
import { getEnginesPath } from '@prisma/engines'
import { getNodeAPIName, getPlatform } from '@prisma/get-platform'
import {
ClientEngineType,
extractPreviewFeatures,
getClientEngineType,
getConfig,
getDMMF,
getPackedPackage,
mapPreviewFeatures,
} from '@prisma/sdk'
import copy from '@timsuchanek/copy'
import fs from 'fs'
import path from 'path'
import { performance } from 'perf_hooks'
import rimraf from 'rimraf'
import { promisify } from 'util'
import { generateClient } from '../generation/generateClient'
import { ensureTestClientQueryEngine } from './ensureTestClientQueryEngine'
const debug = Debug('prisma:generateInFolder')
const del = promisify(rimraf)
export interface GenerateInFolderOptions {
projectDir: string
useLocalRuntime?: boolean
transpile?: boolean
packageSource?: string
useBuiltRuntime?: boolean
}
export async function generateInFolder({
projectDir,
useLocalRuntime = false,
transpile = true,
packageSource,
useBuiltRuntime,
}: GenerateInFolderOptions): Promise<number> {
const before = performance.now()
if (!projectDir) {
throw new Error(`Project dir missing. Usage: ts-node examples/generate.ts examples/accounts`)
}
if (!fs.existsSync(projectDir)) {
throw new Error(`Path ${projectDir} does not exist`)
}
const schemaPath = getSchemaPath(projectDir)
const datamodel = fs.readFileSync(schemaPath, 'utf-8')
const config = await getConfig({ datamodel, ignoreEnvVarErrors: true })
const previewFeatures = mapPreviewFeatures(extractPreviewFeatures(config))
const clientGenerator = config.generators[0]
const clientEngineType = getClientEngineType(clientGenerator)
const dmmf = await getDMMF({
datamodel,
previewFeatures,
})
const outputDir = transpile
? path.join(projectDir, 'node_modules/@prisma/client')
: path.join(projectDir, '@prisma/client')
// if (transpile && config.generators[0]?.output) {
// outputDir = path.join(path.dirname(schemaPath), config.generators[0]?.output)
// }
await del(outputDir)
if (transpile) {
if (packageSource) {
await copy({
from: packageSource, // when using yarn pack and extracting it, it includes a folder called "package"
to: outputDir,
recursive: true,
parallelJobs: 20,
overwrite: true,
})
} else {
await getPackedPackage('@prisma/client', outputDir)
}
}
const platform = await getPlatform()
let runtimeDirs
if (useLocalRuntime) {
if (useBuiltRuntime) {
runtimeDirs = {
node: path.relative(outputDir, path.join(__dirname, '../../runtime')),
edge: path.relative(outputDir, path.join(__dirname, '../../runtime/edge')),
}
} else {
runtimeDirs = {
node: path.relative(outputDir, path.join(__dirname, '../runtime')),
edge: path.relative(outputDir, path.join(__dirname, '../runtime/edge')),
}
}
} else if (useBuiltRuntime) {
throw new Error(`Please provide useBuiltRuntime and useLocalRuntime at the same time or just useLocalRuntime`)
}
const enginesPath = getEnginesPath()
const queryEngineLibraryPath = path.join(enginesPath, getNodeAPIName(platform, 'fs'))
const queryEngineBinaryPath = path.join(
enginesPath,
`query-engine-${platform}${platform === 'windows' ? '.exe' : ''}`,
)
await ensureTestClientQueryEngine(clientEngineType, platform)
const binaryPaths =
clientEngineType === ClientEngineType.Library
? {
libqueryEngine: {
[platform]: queryEngineLibraryPath,
},
}
: {
queryEngine: {
[platform]: queryEngineBinaryPath,
},
}
// we make sure that we are in the project root
// this only applies to generated test clients
process.chdir(projectDir)
await generateClient({
binaryPaths,
datamodel,
dmmf,
...config,
outputDir,
runtimeDirs,
transpile,
testMode: true,
schemaPath,
copyRuntime: false,
generator: config.generators[0],
clientVersion: 'local',
engineVersion: 'local',
activeProvider: config.datasources[0].activeProvider,
})
const time = performance.now() - before
debug(`Done generating client in ${time}`)
return time
}
function getSchemaPath(projectDir: string): string {
if (fs.existsSync(path.join(projectDir, 'schema.prisma'))) {
return path.join(projectDir, 'schema.prisma')
}
if (fs.existsSync(path.join(projectDir, 'prisma/schema.prisma'))) {
return path.join(projectDir, 'prisma/schema.prisma')
}
throw new Error(`Could not find any schema.prisma in ${projectDir} or sub directories.`)
}