Skip to content

Commit

Permalink
Implement format support in BaseCommand::execute, general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rinatkhaziev committed Feb 13, 2024
1 parent 86f965c commit 8e21583
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
5 changes: 0 additions & 5 deletions src/commands/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class AppCommand extends BaseVIPCommand {
protected childCommands: BaseVIPCommand[] = [];

protected async execute( ...arg: unknown[] ): void {

Check failure on line 31 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Type Checker

The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?

Check failure on line 31 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Type Checker

The return type of an async function or method must be the global Promise<T> type. Did you mean to write 'Promise<void>'?
console.log( arg[ 0 ] );
let res;
try {
res = await app(
Expand Down Expand Up @@ -80,9 +79,5 @@ export class AppCommand extends BaseVIPCommand {
} );

return { header, data: clonedResponse.environments };

Check failure on line 81 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Type Checker

Type '{ header: ({ key: string; value: Maybe<number> | undefined; } | { key: string; value: Maybe<string> | undefined; })[]; data: Maybe<AppEnvironment>[]; }' is not assignable to type 'void'.

Check failure on line 81 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Type Checker

Type '{ header: ({ key: string; value: Maybe<number> | undefined; } | { key: string; value: Maybe<string> | undefined; })[]; data: Maybe<AppEnvironment>[]; }' is not assignable to type 'void'.

// console.log( formatData( header, 'keyValue' ) );

// console.log( formatData( clonedResponse.environments, "table" ) );
}
}
40 changes: 35 additions & 5 deletions src/lib/base-command.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import debugLib from 'debug';
import { prompt } from 'enquirer';

import { formatData } from './cli/format';
import Token from './token';
import { trackEvent, aliasUser } from './tracker';

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

// Needs to go inside the command
const debug = debugLib( '@automattic/vip:bin:vip' );
// Config
Expand Down Expand Up @@ -219,15 +219,16 @@ export abstract class BaseVIPCommand {
}
}

let ret;
let res;
// Invoke the command and send tracking information
const trackingParams = this.getTrackingParams( { args } );
// console.log( args );
// let [ _args, opts, command ] = args;
const command = args[ args.length - 1 ];
console.log( command.opts() );
const _opts = command.opts();
// console.log( command.opts() );

if ( command.opts()?.inspect && ! this.isDebugConfirmed ) {
if ( _opts?.inspect && ! this.isDebugConfirmed ) {
await prompt( {
type: 'confirm',
name: 'confirm',
Expand All @@ -238,7 +239,36 @@ export abstract class BaseVIPCommand {

try {
await trackEvent( `${ this.name }_execute`, trackingParams );
ret = await this.execute( ...args );
res = await this.execute( ...args );

if ( _opts.format && res ) {
if ( res.header ) {
if ( _opts.format !== 'json' ) {
console.log( formatData( res.header, 'keyValue' ) );
}
res = res.data;
}

res = res.map( row => {
const out = { ...row };
if ( out.__typename ) {
// Apollo injects __typename
delete out.__typename;
}

return out;
} );

await trackEvent( 'command_output', {
format: _opts.format,
} );

const formattedOut = formatData( res, _opts.format );

console.log( formattedOut );

return {};
}
await trackEvent( `${ this.name }_success`, trackingParams );
} catch ( error ) {
const err =
Expand Down
5 changes: 4 additions & 1 deletion src/lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ const makeVIPCommand = ( command: BaseVIPCommand ): Command => {
cmd.option( option.name, option.description );
}

cmd.option( '-d, --debug [component]', 'Show debug' ).option( '--inspect', 'Attach a debugger' );
cmd
.option( '-d, --debug [component]', 'Show debug' )
.option( '--inspect', 'Attach a debugger' )
.option( '--format [json|table|csv|ids]', 'Output format' );

cmd.action( async ( ...args: unknown[] ) => {
await registry.invokeCommand( name, ...args );
Expand Down

0 comments on commit 8e21583

Please sign in to comment.