From ef0e85b9b0e2c8bc347c5ce52d74afb3ae98ef32 Mon Sep 17 00:00:00 2001 From: Rinat Khaziev Date: Fri, 8 Dec 2023 16:44:27 -0600 Subject: [PATCH] WIP - more argument parsing, trying to handle @app.env accordingly --- src/commands/app.ts | 2 +- src/lib/base-command.ts | 45 ++++++++++++++++++++++++++++++++++++++--- src/lib/command.ts | 14 ++++++++++--- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/src/commands/app.ts b/src/commands/app.ts index 565f875f1..596fed283 100644 --- a/src/commands/app.ts +++ b/src/commands/app.ts @@ -26,7 +26,7 @@ export class AppCommand extends BaseVIPCommand { ], }; - protected readonly commandOptions: CommandOption[] = []; + // protected readonly commandOptions: CommandOption[] = []; protected childCommands: BaseVIPCommand[] = []; diff --git a/src/lib/base-command.ts b/src/lib/base-command.ts index 02b92e5df..68107c9d8 100644 --- a/src/lib/base-command.ts +++ b/src/lib/base-command.ts @@ -1,19 +1,41 @@ +import args from 'args'; +import chalk from 'chalk'; +import debugLib from 'debug'; +import { prompt } from 'enquirer'; +import gql from 'graphql-tag'; + +import { parseEnvAliasFromArgv } from '../lib/cli/envAlias'; import { CommandRegistry } from './command-registry'; import { trackEvent } from './tracker'; import type { CommandOption, CommandArgument, CommandUsage } from './types/commands'; +import { Command } from 'commander'; export abstract class BaseVIPCommand { protected name: string = 'vip'; + protected isDebugConfirmed: boolean = false; - protected readonly commandOptions: CommandOption[] = []; + protected readonly commandOptions: CommandOption[] = [ + { + name: 'app', + description: 'Application id or slug', + type: 'string', + required: false, + }, + { + name: 'env', + description: 'Application environment', + type: 'string', + required: false, + }, + ]; protected readonly commandArguments: CommandArgument[] = [ { name: 'app', description: 'Application id or slug', type: 'string', - required: true, + required: false, }, ]; @@ -43,12 +65,29 @@ export abstract class BaseVIPCommand { return true; } + // args length can vary based the number of arguments and options the command defines, the command itsrlf is always the last argument + // Can some of this logic be moved out to a hook? public async run( ...args: unknown[] ): Promise< void > { + let ret; // Invoke the command and send tracking information const trackingParams = this.getTrackingParams( { args } ); + // console.log( args ); + // let [ _args, opts, command ] = args; + let command = args[ args.length - 1 ]; + console.log( command.opts() ); + + if ( command.opts()?.inspect && ! this.isDebugConfirmed ) { + await prompt( { + type: 'confirm', + name: 'confirm', + message: "Attach the debugger, once you see 'Debugger attached' above hit 'y' to continue", + } ); + this.isDebugConfirmed = true; + } + try { await trackEvent( `${ this.name }_execute`, trackingParams ); - this.execute( ...args ); + ret = await this.execute( ...args ); await trackEvent( `${ this.name }_success`, trackingParams ); } catch ( error ) { const err = diff --git a/src/lib/command.ts b/src/lib/command.ts index c067da9a5..04d240698 100644 --- a/src/lib/command.ts +++ b/src/lib/command.ts @@ -5,6 +5,8 @@ import { CommandRegistry } from './command-registry'; import { description, version } from '../../package.json'; import { ExampleCommand } from '../commands/example-command'; import { AppCommand } from '../commands/app'; +import { parse } from 'args'; +import { parseEnvAliasFromArgv } from './cli/envAlias'; /** * Base Command from which every subcommand should inherit. @@ -34,6 +36,8 @@ const makeVIPCommand = ( command: BaseVIPCommand ): Command => { cmd.option( option.name, option.description ); } + cmd.option( '-d, --debug [component]', 'Show debug' ).option( '--inspect', 'Attach a debugger' ); + cmd.action( async ( ...args: unknown[] ) => { await registry.invokeCommand( name, ...args ); } ); @@ -58,8 +62,7 @@ program .name( 'vip' ) .description( description ) .version( version ) - .configureHelp( { showGlobalOptions: true } ) - .option( '--debug, -d', 'Show debug' ); + .configureHelp( { showGlobalOptions: true } ); const registry = CommandRegistry.getInstance(); registry.registerCommand( new ExampleCommand() ); @@ -67,4 +70,9 @@ registry.registerCommand( new AppCommand() ); [ ...registry.getCommands().values() ].map( command => processCommand( program, command ) ); -program.parse( process.argv ); +let { argv, ...appAlias } = parseEnvAliasFromArgv( process.argv ); + +// very very stupid +argv.push( `@${ Object.values( appAlias ).filter( e => e ).join( '.' ) }` ); + +program.parse( argv, appAlias );