-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathengineCommands.ts
262 lines (232 loc) · 6.83 KB
/
engineCommands.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
import chalk from 'chalk'
import execa from 'execa'
import path from 'path'
import { DMMF, DataSource, GeneratorConfig } from '@prisma/generator-helper'
import tmpWrite from 'temp-write'
import fs from 'fs'
import { promisify } from 'util'
import Debug from '@prisma/debug'
import { resolveBinary } from './resolveBinary'
const debug = Debug('engineCommands')
const unlink = promisify(fs.unlink)
const MAX_BUFFER = 1000 * 1000 * 1000
export interface ConfigMetaFormat {
datasources: DataSource[]
generators: GeneratorConfig[]
}
/**
* This annotation is used for `node-file-trace`
* See https://github.com/zeit/node-file-trace/issues/104
*/
path.join(__dirname, '../query-engine-darwin')
path.join(__dirname, '../introspection-engine-darwin')
path.join(__dirname, '../query-engine-debian-openssl-1.0.x')
path.join(__dirname, '../introspection-engine-debian-openssl-1.0.x')
path.join(__dirname, '../query-engine-debian-openssl-1.1.x')
path.join(__dirname, '../introspection-engine-debian-openssl-1.1.x')
path.join(__dirname, '../query-engine-rhel-openssl-1.0.x')
path.join(__dirname, '../introspection-engine-rhel-openssl-1.0.x')
path.join(__dirname, '../query-engine-rhel-openssl-1.0.x')
path.join(__dirname, '../introspection-engine-rhel-openssl-1.0.x')
export type GetDMMFOptions = {
datamodel?: string
cwd?: string
prismaPath?: string
datamodelPath?: string
retry?: number
}
export async function getDMMF({
datamodel,
cwd = process.cwd(),
prismaPath: queryEnginePath,
datamodelPath,
retry = 4,
}: GetDMMFOptions): Promise<DMMF.Document> {
queryEnginePath = queryEnginePath || (await resolveBinary('query-engine'))
let result
try {
let tempDatamodelPath: string | undefined = datamodelPath
if (!tempDatamodelPath) {
try {
tempDatamodelPath = await tmpWrite(datamodel!)
} catch (err) {
throw new Error(
chalk.redBright.bold('Get DMMF ') +
'unable to write temp data model path',
)
}
}
const options = {
cwd,
env: {
...process.env,
PRISMA_DML_PATH: tempDatamodelPath,
RUST_BACKTRACE: '1',
...(process.env.NO_COLOR ? {} : { CLICOLOR_FORCE: '1' }),
},
maxBuffer: MAX_BUFFER,
}
result = await execa(
queryEnginePath,
['--enable-raw-queries', 'cli', 'dmmf'],
options,
)
if (!datamodelPath) {
await unlink(tempDatamodelPath)
}
if (result.stdout.includes('Please wait until the') && retry > 0) {
debug('Retrying after "Please wait until"')
await new Promise((r) => setTimeout(r, 5000))
return getDMMF({
datamodel,
cwd,
prismaPath: queryEnginePath,
datamodelPath,
retry: retry - 1,
})
}
const firstCurly = result.stdout.indexOf('{')
const stdout = result.stdout.slice(firstCurly)
return JSON.parse(stdout)
} catch (e) {
debug('getDMMF failed', e)
// If this unlikely event happens, try it at least once more
if (
e.message.includes('Command failed with exit code 26 (ETXTBSY)') &&
retry > 0
) {
await new Promise((resolve) => setTimeout(resolve, 500))
debug('Retrying after ETXTBSY')
return getDMMF({
datamodel,
cwd,
prismaPath: queryEnginePath,
datamodelPath,
retry: retry - 1,
})
}
const output = e.stderr || e.stdout
if (output) {
let json
try {
json = JSON.parse(output)
} catch (e) {
//
}
let message = (json && json.message) || output
if (
message.includes(
'debian-openssl-1.1.x: error while loading shared libraries: libssl.so.1.1: cannot open shared object file: No such file or directory',
) ||
message.includes(
'debian-openssl-1.0.x: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory',
)
) {
message += `\n${chalk.green(
`Your linux installation misses the openssl package. You can install it like so:\n`,
)}${chalk.green.bold(
'apt-get -qy update && apt-get -qy install openssl',
)}`
}
throw new Error(chalk.redBright.bold('Schema parsing\n') + message)
}
if (e.message.includes('in JSON at position')) {
throw new Error(
`Problem while parsing the query engine response at ${queryEnginePath}. ${result.stdout}\n${e.stack}`,
)
}
throw new Error(e)
}
}
export type GetConfigOptions = {
datamodel?: string
cwd?: string
prismaPath?: string
datamodelPath?: string
retry?: number
ignoreEnvVarErrors?: boolean
}
export async function getConfig({
datamodel,
cwd = process.cwd(),
prismaPath: queryEnginePath,
datamodelPath,
ignoreEnvVarErrors,
}: GetConfigOptions): Promise<ConfigMetaFormat> {
queryEnginePath = queryEnginePath || (await resolveBinary('query-engine'))
let tempDatamodelPath: string | undefined = datamodelPath
if (!tempDatamodelPath) {
try {
tempDatamodelPath = await tmpWrite(datamodel!)
} catch (err) {
throw new Error(
chalk.redBright.bold('Get DMMF ') +
'unable to write temp data model path',
)
}
}
const args = ignoreEnvVarErrors ? ['--ignoreEnvVarErrors'] : []
try {
const result = await execa(
queryEnginePath,
['cli', 'get-config', ...args],
{
cwd,
env: {
...process.env,
PRISMA_DML_PATH: tempDatamodelPath,
RUST_BACKTRACE: '1',
},
maxBuffer: MAX_BUFFER,
},
)
if (!datamodelPath) {
await unlink(tempDatamodelPath)
}
return JSON.parse(result.stdout)
} catch (e) {
if (e.stderr) {
throw new Error(chalk.redBright.bold('Get config ') + e.stderr)
}
if (e.stdout) {
throw new Error(chalk.redBright.bold('Get config ') + e.stdout)
}
throw new Error(chalk.redBright.bold('Get config ') + e)
}
}
type FormatOptions = {
schemaPath: string
}
export async function formatSchema({
schemaPath,
}: FormatOptions): Promise<string> {
if (!fs.existsSync(schemaPath)) {
throw new Error(`Schema at ${schemaPath} does not exist.`)
}
const prismaFmtPath = await resolveBinary('prisma-fmt')
const showColors = !process.env.NO_COLOR && process.stdout.isTTY
const options = {
env: {
...process.env,
RUST_BACKTRACE: '1',
...(showColors ? { CLICOLOR_FORCE: '1' } : {}),
},
maxBuffer: MAX_BUFFER,
}
const result = await execa(
prismaFmtPath,
['format', '-i', schemaPath],
options,
)
return result.stdout
}
export async function getVersion(enginePath?: string): Promise<string> {
enginePath = enginePath || (await resolveBinary('query-engine'))
const result = await execa(enginePath, ['--version'], {
env: {
...process.env,
},
maxBuffer: MAX_BUFFER,
})
return result.stdout
}