import type { PackageJson } from 'type-fest' ; import * as path from 'path' ; import * as fs from 'fs-extra' ; import { Cli } from 'clipanion' ; import { CommandContext } from './base-command' ; import * as commands from './commands' ; import { Logger } from './logger' ; param cli function registerCommands ( cli ) { Object . values ( commands ) . forEach ( ( command ) => cli . register ( command ) ) ; } async function main ( ) { const cwd = process . cwd ( ) ; const { name , version } = ( await fs . readJSON ( path . join ( __dirname , '..' , 'package.json' ) ) ) as PackageJson ; const logger = new Logger ( { stdout : process . stdout , stderr : process . stderr , } ) ; async function run ( ) { const cli = new Cli < CommandContext > ( { binaryLabel : name , binaryName : name , binaryVersion : version , } ) ; registerCommands ( cli ) ; try { await exec ( cli ) ; } catch ( error ) { process . stdout . write ( cli . error ( error ) ) ; process . exitCode = 1 ; } } async function exec ( cli ) { const command = cli . process ( process . argv . slice ( 2 ) ) ; cli . runExit ( command , { stdin : process . stdin , stdout : process . stdout , stderr : process . stderr , cwd , version : version as string , logger , } ) ; } return run ( ) . catch ( ( error ) => { process . stdout . write ( error . stack || error . message ) ; process . exitCode = 1 ; } ) ; } main ( ) ;