forked from prisma/prisma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CLI.ts
163 lines (131 loc) · 4.94 KB
/
CLI.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
import { ensureBinariesExist } from '@prisma/engines'
import type { Command, Commands } from '@prisma/internals'
import { arg, drawBox, format, HelpError, isError, link, logger, unknownCommand } from '@prisma/internals'
import { bold, dim, green, red, underline } from 'kleur/colors'
import { Version } from './Version'
/**
* CLI command
*/
export class CLI implements Command {
static new(cmds: Commands, ensureBinaries: string[]): CLI {
return new CLI(cmds, ensureBinaries)
}
private constructor(private readonly cmds: Commands, private readonly ensureBinaries: string[]) {}
async parse(argv: string[]): Promise<string | Error> {
const args = arg(argv, {
'--help': Boolean,
'-h': '--help',
'--version': Boolean,
'-v': '--version',
'--json': Boolean, // for -v
'--experimental': Boolean,
'--preview-feature': Boolean,
'--early-access': Boolean,
'--telemetry-information': String,
})
if (isError(args)) {
return this.help(args.message)
}
if (args['--version']) {
await ensureBinariesExist()
return Version.new().parse(argv)
}
// display help for help flag or no subcommand
if (args._.length === 0 || args['--help']) {
return this.help()
}
// check if we have that subcommand
const cmdName = args._[0]
// Throw if "lift"
if (cmdName === 'lift') {
throw new Error(`${red('prisma lift')} has been renamed to ${green('prisma migrate')}`)
}
// warn if "introspect"
else if (cmdName === 'introspect') {
logger.warn('')
logger.warn(
`${bold(
`The ${underline('prisma introspect')} command is deprecated. Please use ${green('prisma db pull')} instead.`,
)}`,
)
logger.warn('')
}
const cmd = this.cmds[cmdName]
if (cmd) {
// if we have that subcommand, let's ensure that the binary is there in case the command needs it
if (this.ensureBinaries.includes(cmdName)) {
await ensureBinariesExist()
}
let argsForCmd: string[]
if (args['--experimental']) {
argsForCmd = [...args._.slice(1), `--experimental=${args['--experimental']}`]
} else if (args['--preview-feature']) {
argsForCmd = [...args._.slice(1), `--preview-feature=${args['--preview-feature']}`]
} else if (args['--early-access']) {
argsForCmd = [...args._.slice(1), `--early-access=${args['--early-access']}`]
} else {
argsForCmd = args._.slice(1)
}
return cmd.parse(argsForCmd)
}
// unknown command
return unknownCommand(this.help() as string, args._[0])
}
public help(error?: string) {
if (error) {
return new HelpError(`\n${bold(red(`!`))} ${error}\n${CLI.help}`)
}
return CLI.help
}
private static tryPdpMessage = `Optimize performance through connection pooling and caching with Prisma Accelerate
and capture real-time events from your database with Prisma Pulse.
Learn more at ${link('https://pris.ly/cli/pdp')}`
private static boxedTryPdpMessage = drawBox({
height: this.tryPdpMessage.split('\n').length,
width: 0, // calculated automatically
str: this.tryPdpMessage,
horizontalPadding: 2,
})
private static help = format(`
${
process.platform === 'win32' ? '' : bold(green('◭ '))
}Prisma is a modern DB toolkit to query, migrate and model your database (${link('https://prisma.io')})
${bold('Usage')}
${dim('$')} prisma [command]
${bold('Commands')}
init Set up Prisma for your app
generate Generate artifacts (e.g. Prisma Client)
db Manage your database schema and lifecycle
migrate Migrate your database
studio Browse your data with Prisma Studio
validate Validate your Prisma schema
format Format your Prisma schema
version Displays Prisma version info
debug Displays Prisma debug info
${bold('Flags')}
--preview-feature Run Preview Prisma commands
--help, -h Show additional information about a command
${this.boxedTryPdpMessage}
${bold('Examples')}
Set up a new Prisma project
${dim('$')} prisma init
Generate artifacts (e.g. Prisma Client)
${dim('$')} prisma generate
Browse your data
${dim('$')} prisma studio
Create migrations from your Prisma schema, apply them to the database, generate artifacts (e.g. Prisma Client)
${dim('$')} prisma migrate dev
Pull the schema from an existing database, updating the Prisma schema
${dim('$')} prisma db pull
Push the Prisma schema state to the database
${dim('$')} prisma db push
Validate your Prisma schema
${dim('$')} prisma validate
Format your Prisma schema
${dim('$')} prisma format
Display Prisma version info
${dim('$')} prisma version
Display Prisma debug info
${dim('$')} prisma debug
`)
}