import chalk from 'chalk' ; import type { Command , Commands } from '@prisma/sdk' ; import { 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 , ensureBinaries ) { return new CLI ( cmds , ensureBinaries ) ; } private constructor ( private readonly cmds , private readonly ensureBinaries ) { } async parse ( argv ) { const args = arg ( argv , { '--help' : Boolean , '-h' : '--help' , '--version' : Boolean , '-v' : '--version' , '--json' : Boolean , '--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 ) ; } if ( args . _ . length === 0 || args [ '--help' ] ) { return this . help ( ) ; } const cmdName = args . _ [ 0 ] ; if ( cmdName === 'lift' ) { throw new Error ( `${ chalk . red ( 'prisma lift' ) } has been renamed to ${ chalk . green ( 'prisma migrate' ) }` ) ; } 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 ( this . ensureBinaries . includes ( cmdName ) ) { await ensureBinariesExist ( ) ; } let argsForCmd ; 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-feature' ] ) { argsForCmd = [ ... args . _ . slice ( 1 ) , `--early-access-feature=${ args [ '--early-access-feature' ] }` ] ; } else { argsForCmd = args . _ . slice ( 1 ) ; } return cmd . parse ( argsForCmd ) ; } return unknownCommand ( this . help ( ) as string , args . _ [ 0 ] ) ; } public help ( error ? ) { 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
  ` ) ; }