forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.ts
executable file
·259 lines (237 loc) · 7.81 KB
/
bin.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
#!/usr/bin/env ts-node
import Debug from '@prisma/debug'
import { enginesVersion } from '@prisma/engines'
import { arg, handlePanic, HelpError, isCurrentBinInstalledGlobally, isError, isRustPanic } from '@prisma/internals'
import {
DbCommand,
DbExecute,
DbPull,
DbPush,
// DbDrop,
DbSeed,
getDatabaseVersionSafe,
MigrateCommand,
MigrateDeploy,
MigrateDev,
MigrateDiff,
MigrateReset,
MigrateResolve,
MigrateStatus,
} from '@prisma/migrate'
import { bold, red } from 'kleur/colors'
import path from 'path'
import { CLI } from './CLI'
import { DebugInfo } from './DebugInfo'
import { Format } from './Format'
import { Generate } from './Generate'
import { Init } from './Init'
import { Platform } from './platform'
/*
When running bin.ts with ts-node with DEBUG="*"
This error shows and blocks the execution
Quick hack is to comment the Studio import and usage to use the CLI without building it...
prisma:cli Error: Cannot find module '@prisma/internals'
prisma:cli Require stack:
prisma:cli - /Users/j42/Dev/prisma-meow/node_modules/.pnpm/@[email protected]/node_modules/@prisma/studio-pcw/dist/index.js
*/
import { Studio } from './Studio'
import { Telemetry } from './Telemetry'
import { redactCommandArray, runCheckpointClientCheck } from './utils/checkpoint'
import { detectPrisma1 } from './utils/detectPrisma1'
import { printUpdateMessage } from './utils/printUpdateMessage'
import { Validate } from './Validate'
import { Version } from './Version'
// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-unsafe-assignment
const packageJson = require('../package.json')
const commandArray = process.argv.slice(2)
process.removeAllListeners('warning')
// Listen to Ctr + C and exit
process.once('SIGINT', () => {
process.exit(130)
})
// Parse CLI arguments
const args = arg(
commandArray,
{
'--schema': String,
'--telemetry-information': String,
},
false,
true,
)
// Redact the command options and make it a string
const redactedCommandAsString = redactCommandArray([...commandArray]).join(' ')
const isPrismaInstalledGlobally = isCurrentBinInstalledGlobally()
/**
* Main function
*/
async function main(): Promise<number> {
// create a new CLI with our subcommands
detectPrisma1()
const cli = CLI.new(
{
init: Init.new(),
platform: Platform.$.new({
workspace: Platform.Workspace.$.new({
show: Platform.Workspace.Show.new(),
}),
auth: Platform.Auth.$.new({
login: Platform.Auth.Login.new(),
logout: Platform.Auth.Logout.new(),
show: Platform.Auth.Show.new(),
}),
project: Platform.Project.$.new({
create: Platform.Project.Create.new(),
delete: Platform.Project.Delete.new(),
show: Platform.Project.Show.new(),
}),
accelerate: Platform.Accelerate.$.new({
enable: Platform.Accelerate.Enable.new(),
disable: Platform.Accelerate.Disable.new(),
}),
apikey: Platform.APIKey.$.new({
create: Platform.APIKey.Create.new(),
delete: Platform.APIKey.Delete.new(),
show: Platform.APIKey.Show.new(),
}),
}),
migrate: MigrateCommand.new({
dev: MigrateDev.new(),
status: MigrateStatus.new(),
resolve: MigrateResolve.new(),
reset: MigrateReset.new(),
deploy: MigrateDeploy.new(),
diff: MigrateDiff.new(),
}),
db: DbCommand.new({
execute: DbExecute.new(),
pull: DbPull.new(),
push: DbPush.new(),
// drop: DbDrop.new(),
seed: DbSeed.new(),
}),
/**
* @deprecated since version 2.30.0, use `db pull` instead (renamed)
*/
introspect: DbPull.new(),
studio: Studio.new(),
generate: Generate.new(),
version: Version.new(),
validate: Validate.new(),
format: Format.new(),
telemetry: Telemetry.new(),
debug: DebugInfo.new(),
},
['version', 'init', 'migrate', 'db', 'introspect', 'studio', 'generate', 'validate', 'format', 'telemetry'],
)
// Execute the command
const result = await cli.parse(commandArray)
// Did it error?
if (result instanceof HelpError) {
console.error(result.message)
// TODO: We could do like Bash (and other)
// = return an exit status of 2 to indicate incorrect usage like invalid options or missing arguments.
// https://tldp.org/LDP/abs/html/exitcodes.html
return 1
} else if (isError(result)) {
console.error(result)
return 1
}
// Success
console.log(result)
/**
* Prepare data and run the Checkpoint Client
* See function for more info
*/
const checkResult = await runCheckpointClientCheck({
command: redactedCommandAsString,
isPrismaInstalledGlobally,
schemaPath: args['--schema'],
telemetryInformation: args['--telemetry-information'],
version: packageJson.version,
})
// if the result is cached and CLI outdated, show the `Update available` message
const shouldHide = process.env.PRISMA_HIDE_UPDATE_MESSAGE
if (checkResult && checkResult.status === 'ok' && checkResult.data.outdated && !shouldHide) {
printUpdateMessage(checkResult)
}
return 0
}
/**
* Run our program
*/
if (eval('require.main === module')) {
main()
.then((code) => {
if (code !== 0) {
process.exit(code)
}
})
.catch((err) => {
// Sindre's pkg p-map & co are using AggregateError, it is an iterator.
if (typeof err[Symbol.iterator] === 'function') {
for (const individualError of err) {
handleIndividualError(individualError)
}
} else {
handleIndividualError(err)
}
})
}
function handleIndividualError(error: Error): void {
if (isRustPanic(error)) {
handlePanic({
error,
cliVersion: packageJson.version,
enginesVersion,
command: redactedCommandAsString,
getDatabaseVersionSafe,
})
.catch((e) => {
if (Debug.enabled('prisma')) {
console.error(bold(red('Error: ')) + e.stack)
} else {
console.error(bold(red('Error: ')) + e.message)
}
})
.finally(() => {
process.exit(1)
})
} else {
if (Debug.enabled('prisma')) {
console.error(bold(red('Error: ')) + error.stack)
} else {
console.error(bold(red('Error: ')) + error.message)
}
process.exit(1)
}
}
/**
* Annotations for `pkg` so it bundles things correctly with yarn's hoisting
* `node_modules/prisma/build/index.js` needs to get to:
* `node_modules/@prisma/engines`
*/
// macOS
path.join(__dirname, '../../engines/query-engine-darwin')
path.join(__dirname, '../../engines/schema-engine-darwin')
// Windows
path.join(__dirname, '../../engines/query-engine-windows.exe')
path.join(__dirname, '../../engines/schema-engine-windows.exe')
// Debian openssl-1.0.x
path.join(__dirname, '../../engines/query-engine-debian-openssl-1.0.x')
path.join(__dirname, '../../engines/schema-engine-debian-openssl-1.0.x')
// Debian openssl-1.1.x
path.join(__dirname, '../../engines/query-engine-debian-openssl-1.1.x')
path.join(__dirname, '../../engines/schema-engine-debian-openssl-1.1.x')
// Debian openssl-3.0.x
path.join(__dirname, '../../engines/query-engine-debian-openssl-3.0.x')
path.join(__dirname, '../../engines/schema-engine-debian-openssl-3.0.x')
// Red Hat Enterprise Linux openssl-1.0.x
path.join(__dirname, '../../engines/query-engine-rhel-openssl-1.0.x')
path.join(__dirname, '../../engines/schema-engine-rhel-openssl-1.0.x')
// Red Hat Enterprise Linux openssl-1.1.x
path.join(__dirname, '../../engines/query-engine-rhel-openssl-1.1.x')
path.join(__dirname, '../../engines/schema-engine-rhel-openssl-1.1.x')
// Red Hat Enterprise Linux openssl-3.0.x
path.join(__dirname, '../../engines/query-engine-rhel-openssl-3.0.x')
path.join(__dirname, '../../engines/schema-engine-rhel-openssl-3.0.x')