| 8 | * see public one in packages/cli/ directory |
| 9 | */ |
| 10 | export class CLI implements Command { |
| 11 | static new(cmds: Commands): CLI { |
| 12 | return new CLI(cmds) |
| 13 | } |
| 14 | |
| 15 | private constructor(private readonly cmds: Commands) {} |
| 16 | |
| 17 | async parse(argv: string[], config: PrismaConfigInternal, baseDir: string): Promise<string | Error> { |
| 18 | const args = arg(argv, { |
| 19 | '--help': Boolean, |
| 20 | '-h': '--help', |
| 21 | '--config': String, |
| 22 | '--json': Boolean, // for -v |
| 23 | '--experimental': Boolean, |
| 24 | '--preview-feature': Boolean, |
| 25 | '--early-access': Boolean, |
| 26 | '--telemetry-information': String, |
| 27 | }) |
| 28 | |
| 29 | if (isError(args)) { |
| 30 | return this.help(args.message) |
| 31 | } |
| 32 | |
| 33 | // display help for help flag or no subcommand |
| 34 | if (args._.length === 0 || args['--help']) { |
| 35 | return this.help() |
| 36 | } |
| 37 | |
| 38 | // check if we have that subcommand |
| 39 | const cmdName = args._[0] |
| 40 | const cmd = this.cmds[cmdName] |
| 41 | if (cmd) { |
| 42 | let argsForCmd: string[] |
| 43 | if (args['--experimental']) { |
| 44 | argsForCmd = [...args._.slice(1), `--experimental=${args['--experimental']}`] |
| 45 | } else if (args['--preview-feature']) { |
| 46 | argsForCmd = [...args._.slice(1), `--preview-feature=${args['--preview-feature']}`] |
| 47 | } else if (args['--early-access']) { |
| 48 | argsForCmd = [...args._.slice(1), `--early-access=${args['--early-access']}`] |
| 49 | } else { |
| 50 | argsForCmd = args._.slice(1) |
| 51 | } |
| 52 | |
| 53 | return cmd.parse(argsForCmd, config, baseDir) |
| 54 | } |
| 55 | // unknown command |
| 56 | return unknownCommand(this.help() as string, args._[0]) |
| 57 | } |
| 58 | |
| 59 | public help(error?: string) { |
| 60 | if (error) { |
| 61 | return new HelpError(`\n${bold(red(`!`))} ${error}\n${CLI.help}`) |
| 62 | } |
| 63 | return CLI.help |
| 64 | } |
| 65 | |
| 66 | private static help = format(`This is the internal CLI for @prisma/migrate`) |
| 67 | } |