forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin.ts
More file actions
executable file
·252 lines (220 loc) · 7.53 KB
/
bin.ts
File metadata and controls
executable file
·252 lines (220 loc) · 7.53 KB
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
#!/usr/bin/env tsx
import path from 'node:path'
import { context, trace } from '@opentelemetry/api'
import { AsyncLocalStorageContextManager } from '@opentelemetry/context-async-hooks'
import { BasicTracerProvider } from '@opentelemetry/sdk-trace-base'
import { InjectFormatters } from '@prisma/config'
import { Debug } from '@prisma/debug'
import { enginesVersion } from '@prisma/engines'
import { download } from '@prisma/fetch-engine'
import { arg, handlePanic, HelpError, isRustPanic, link } from '@prisma/internals'
import {
DbCommand,
DbExecute,
DbPull,
DbPush,
// DbDrop,
DbSeed,
getDatabaseVersionSafe,
MigrateCommand,
MigrateDeploy,
MigrateDev,
MigrateDiff,
MigrateReset,
MigrateResolve,
MigrateStatus,
} from '@prisma/migrate'
import { bold, dim, red, yellow } from 'kleur/colors'
import { CLI } from './CLI'
import { DebugInfo } from './DebugInfo'
import { Format } from './Format'
import { Generate } from './Generate'
import { Init } from './Init'
import { Mcp } from './mcp/MCP'
import { Platform } from './platform/_Platform'
import { Status } from './Status'
import { Studio } from './Studio'
/*
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/@prisma+studio-pcw@0.456.0/node_modules/@prisma/studio-pcw/dist/index.js
*/
// import { Studio } from './Studio'
import { SubCommand } from './SubCommand'
import { Telemetry } from './Telemetry'
import { redactCommandArray } from './utils/checkpoint'
import { loadOrInitializeCommandState } from './utils/commandState'
import { UserFacingError } from './utils/errors'
import { loadConfig } from './utils/loadConfig'
import { Validate } from './Validate'
import { Version } from './Version'
context.setGlobalContextManager(new AsyncLocalStorageContextManager())
trace.setGlobalTracerProvider(new BasicTracerProvider())
const debug = Debug('prisma:cli:bin')
const packageJson = require('../package.json')
const commandArray = process.argv.slice(2)
process.removeAllListeners('warning')
process.once('SIGINT', () => {
process.exitCode = 130
// no further downstream listeners for SIGINT, exit immediately with the code set above.
if (process.listenerCount('SIGINT') === 0) {
process.exit()
}
// otherwise, let the downstream listeners handle it.
})
// Parse CLI arguments
const args = arg(
commandArray,
{
'--config': String,
},
false,
true,
)
/**
* Main function
*/
async function main(): Promise<number> {
// create a new CLI with our subcommands
const cli = CLI.new(
{
init: Init.new(),
mcp: Mcp.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(),
}),
generate: Generate.new(),
version: Version.new(),
validate: Validate.new(),
format: Format.new(),
telemetry: Telemetry.new(),
debug: DebugInfo.new(),
dev: new SubCommand('@prisma/cli-dev'),
studio: Studio.new(),
platform: Platform.$.new({ status: Status.new() }),
},
['version', 'init', 'migrate', 'db', 'generate', 'validate', 'format', 'telemetry'],
download,
)
await loadOrInitializeCommandState().catch((err) => {
debug(`Failed to initialize the command state: ${err}`)
})
const configFile = args['--config']
const configDir = configFile ? path.resolve(configFile, '..') : process.cwd()
const configEither = await loadConfig(configFile)
if (configEither instanceof HelpError) {
console.error(configEither.message)
return 1
}
const { config, diagnostics: configDiagnostics } = configEither
// Diagnostics like informational logs and warnings are logged to stderr, to not interfere with structured output
// in some of the commands consuming the Prisma config.
// See: https://www.gnu.org/software/libc/manual/html_node/Standard-Streams.html
const configDiagnosticFormatters: InjectFormatters = {
// This fixes https://github.com/prisma/prisma/issues/27609.
log: (data) => process.stderr.write(data + '\n'),
warn: (data) => console.warn(`${yellow(bold('warn'))} ${data}`),
dim: (data) => dim(data),
// `terminal-link` is not easily installable in `@prisma/config` without introducing ESM/CJS incompatibility issues, or
// circular dependencies (requiring yet another `@prisma` package),
link: (data) => link(data),
}
for (const configDiagnostic of configDiagnostics) {
configDiagnostic.value(configDiagnosticFormatters)()
}
try {
const startCliExec = performance.now()
// Execute the command
const result = await cli.parse(commandArray, config, configDir)
const endCliExec = performance.now()
const cliExecElapsedTime = endCliExec - startCliExec
debug(`Execution time for executing "await cli.parse(commandArray)": ${cliExecElapsedTime} ms`)
if (result instanceof Error) {
if (result instanceof HelpError || result instanceof UserFacingError) {
console.error(result.message)
} else {
console.error(result)
}
return 1
}
// Success
console.log(result)
return 0
} catch (error) {
if (isRustPanic(error)) {
await handlePanic({
error,
cliVersion: packageJson.version,
enginesVersion,
command: redactCommandArray([...commandArray]).join(' '),
getDatabaseVersionSafe: (args) => getDatabaseVersionSafe(args, config, configDir),
})
}
throw error
}
}
/**
* 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 (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/schema-engine-darwin')
// Windows
path.join(__dirname, '../../engines/schema-engine-windows.exe')
// 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/schema-engine-debian-openssl-1.1.x')
// 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/schema-engine-rhel-openssl-1.0.x')
// Red Hat Enterprise Linux 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/schema-engine-rhel-openssl-3.0.x')