forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin.ts
executable file
·117 lines (108 loc) · 3.16 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
#!/usr/bin/env tsx
import Debug from '@prisma/debug'
import { enginesVersion } from '@prisma/engines-version'
import { handlePanic, HelpError, isError } from '@prisma/internals'
import { bold, red } from 'kleur/colors'
import { CLI } from './CLI'
import { DbCommand } from './commands/DbCommand'
import { DbExecute } from './commands/DbExecute'
import { DbPull } from './commands/DbPull'
import { DbPush } from './commands/DbPush'
// import { DbDrop } from './commands/DbDrop'
import { DbSeed } from './commands/DbSeed'
import { MigrateCommand } from './commands/MigrateCommand'
import { MigrateDeploy } from './commands/MigrateDeploy'
import { MigrateDev } from './commands/MigrateDev'
import { MigrateDiff } from './commands/MigrateDiff'
import { MigrateReset } from './commands/MigrateReset'
import { MigrateResolve } from './commands/MigrateResolve'
import { MigrateStatus } from './commands/MigrateStatus'
import { getDatabaseVersionSafe } from './utils/getDatabaseVersionSafe'
process.on('uncaughtException', (e) => {
console.log(e)
})
process.on('unhandledRejection', (e, promise) => {
console.log(String(e), String(promise))
})
// Listen to Ctr + C and exit
process.once('SIGINT', () => {
process.exit(130)
})
const commandArray = process.argv.slice(2)
const packageJson = eval(`require('../package.json')`)
/**
* Main function
*/
async function main(): Promise<number> {
// create a new CLI with our subcommands
const cli = CLI.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(),
}),
})
// Execute the command
const result = await cli.parse(commandArray)
// Did it error?
if (result instanceof HelpError) {
console.error(result)
// 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)
return 0
}
/**
* Run our program
*/
main()
.then((code) => {
if (code !== 0) {
process.exit(code)
}
})
.catch((error) => {
if (error.rustStack) {
handlePanic({
error,
cliVersion: packageJson.version,
enginesVersion,
command: commandArray.join(' '),
getDatabaseVersionSafe,
})
.catch((e) => {
if (Debug.enabled('migrate')) {
console.error(red(bold('Error: ')) + e.stack)
} else {
console.error(red(bold('Error: ')) + e.message)
}
})
.finally(() => {
process.exit(1)
})
} else {
if (Debug.enabled('migrate')) {
console.error(red(bold('Error: ')) + error.stack)
} else {
console.error(red(bold('Error: ')) + error.message)
}
process.exit(1)
}
})