/
CLI.ts
164 lines (140 loc) · 4.31 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
164
import chalk from 'chalk'
import {
Command,
Commands,
arg,
isError,
format,
HelpError,
unknownCommand,
logger,
} from '@prisma/sdk'
import { Version } from './Version'
import { link } from '@prisma/sdk'
import { ensureBinariesExist } from '@prisma/engines'
/**
* 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-feature': 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(
`${chalk.red('prisma lift')} has been renamed to ${chalk.green(
'prisma migrate',
)}`,
)
}
// warn if "introspect"
else if (cmdName === 'introspect') {
logger.warn('')
logger.warn(
`${chalk.bold(
`The ${chalk.underline(
'prisma introspect',
)} command is deprecated. Please use ${chalk.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 = argsForCmd = [
...args._.slice(1),
`--preview-feature=${args['--preview-feature']}`,
]
} else if (args['--early-access-feature']) {
argsForCmd = argsForCmd = [
...args._.slice(1),
`--early-access-feature=${args['--early-access-feature']}`,
]
} 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${chalk.bold.red(`!`)} ${error}\n${CLI.help}`)
}
return CLI.help
}
private static help = format(`
${
process.platform === 'win32' ? '' : chalk.bold.green('◭ ')
}Prisma is a modern DB toolkit to query, migrate and model your database (${link(
'https://prisma.io',
)})
${chalk.bold('Usage')}
${chalk.dim('$')} prisma [command]
${chalk.bold('Commands')}
init Setup 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
format Format your schema
${chalk.bold('Flags')}
--preview-feature Run Preview Prisma commands
${chalk.bold('Examples')}
Setup a new Prisma project
${chalk.dim('$')} prisma init
Generate artifacts (e.g. Prisma Client)
${chalk.dim('$')} prisma generate
Browse your data
${chalk.dim('$')} prisma studio
Create migrations from your Prisma schema, apply them to the database, generate artifacts (e.g. Prisma Client)
${chalk.dim('$')} prisma migrate dev
Pull the schema from an existing database, updating the Prisma schema
${chalk.dim('$')} prisma db pull
Push the Prisma schema state to the database
${chalk.dim('$')} prisma db push
`)
}