Skip to content

Commit

Permalink
WIP - more argument parsing, trying to handle @app.env accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
rinatkhaziev committed Dec 8, 2023
1 parent a731299 commit ef0e85b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/commands/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class AppCommand extends BaseVIPCommand {
],
};

protected readonly commandOptions: CommandOption[] = [];
// protected readonly commandOptions: CommandOption[] = [];

protected childCommands: BaseVIPCommand[] = [];

Expand Down
45 changes: 42 additions & 3 deletions src/lib/base-command.ts
Original file line number Diff line number Diff line change
@@ -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';

Check failure on line 8 in src/lib/base-command.ts

View workflow job for this annotation

GitHub Actions / Lint

`./command-registry` import should occur before import of `../lib/cli/envAlias`
import { trackEvent } from './tracker';

Check failure on line 9 in src/lib/base-command.ts

View workflow job for this annotation

GitHub Actions / Lint

`./tracker` import should occur before import of `../lib/cli/envAlias`

import type { CommandOption, CommandArgument, CommandUsage } from './types/commands';

Check failure on line 11 in src/lib/base-command.ts

View workflow job for this annotation

GitHub Actions / Lint

There should be at least one empty line between import groups
import { Command } from 'commander';

Check failure on line 12 in src/lib/base-command.ts

View workflow job for this annotation

GitHub Actions / Lint

`commander` import should occur before import of `debug`

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,
},
];

Expand Down Expand Up @@ -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 =
Expand Down
14 changes: 11 additions & 3 deletions src/lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 );
} );
Expand All @@ -58,13 +62,17 @@ 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() );
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 );

0 comments on commit ef0e85b

Please sign in to comment.