Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Separate commands implementation from specific CLI args parser. #1605

Draft
wants to merge 18 commits into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
281 changes: 272 additions & 9 deletions npm-shrinkwrap.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@
"nock": "13.5.1",
"prettier": "npm:wp-prettier@2.8.5",
"rimraf": "5.0.5",
"ts-node": "^10.9.1",
"typescript": "^5.2.2"
},
"dependencies": {
Expand All @@ -148,6 +149,7 @@
"check-disk-space": "3.4.0",
"cli-columns": "^4.0.0",
"cli-table3": "^0.6.3",
"commander": "^11.1.0",
"configstore": "5.0.1",
"copy-dir": "0.4.0",
"debug": "4.3.4",
Expand Down
90 changes: 90 additions & 0 deletions src/commands/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import chalk from 'chalk';

import app from '../lib/api/app';

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

View workflow job for this annotation

GitHub Actions / Lint

There should be no empty line within import group

import { trackEvent } from '../lib/tracker';

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

View workflow job for this annotation

GitHub Actions / Lint

`../lib/tracker` import should occur after import of `../lib/cli/format`
import { BaseVIPCommand } from '../lib/base-command';
import { getEnvIdentifier } from '../lib/cli/command';
import { formatData, formatSearchReplaceValues } from '../lib/cli/format';

Check warning on line 8 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Lint

'formatData' is defined but never used

Check warning on line 8 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Lint

'formatData' is defined but never used

Check warning on line 8 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Lint

'formatSearchReplaceValues' is defined but never used

Check warning on line 8 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Lint

'formatSearchReplaceValues' is defined but never used
Fixed Show fixed Hide fixed

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

Check warning on line 10 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Lint

'CommandOption' is defined but never used

Check warning on line 10 in src/commands/app.ts

View workflow job for this annotation

GitHub Actions / Lint

'CommandOption' is defined but never used

export class AppCommand extends BaseVIPCommand {
protected readonly name: string = 'app';

protected readonly usage: CommandUsage = {
description: 'List and modify your VIP applications',
examples: [
{
description: 'Example 1',
usage: 'vip app app',
},
{
description: 'Example 2',
usage: 'vip example ',
},
],
};

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

protected childCommands: BaseVIPCommand[] = [];

protected async execute( ...arg: unknown[] ): void {
console.log(arg[0]);

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

View workflow job for this annotation

GitHub Actions / Lint

Replace `arg[0]` with `·arg[·0·]·`
let res;
try {
res = await app(
arg[ 0 ],
'id,repo,name,environments{id,appId,name,type,branch,currentCommit,primaryDomain{name},launched}'
);
} catch ( err ) {
await trackEvent( 'app_command_fetch_error', {
error: `App ${ arg[ 0 ] } does not exist`,

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

View workflow job for this annotation

GitHub Actions / Lint

Invalid type "unknown" of template literal expression
} );

console.log( `App ${ chalk.blueBright( arg[ 0 ] ) } does not exist` );
return;
}

if ( ! res || ! res.environments ) {
await trackEvent( 'app_command_fetch_error', {
error: `App ${ arg[ 0 ] } does not exist`,

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

View workflow job for this annotation

GitHub Actions / Lint

Invalid type "unknown" of template literal expression
} );

console.log( `App ${ chalk.blueBright( arg[ 0 ] ) } does not exist` );
return;
}

await trackEvent( 'app_command_success' );

// Clone the read-only response object so we can modify it
const clonedResponse = Object.assign( {}, res );

const header = [
{ key: 'id', value: res.id },
{ key: 'name', value: res.name },
{ key: 'repo', value: res.repo },
];

clonedResponse.environments = clonedResponse.environments.map( env => {
const clonedEnv = Object.assign( {}, env );

clonedEnv.name = getEnvIdentifier( env );

// Use the short version of git commit hash
clonedEnv.currentCommit = clonedEnv.currentCommit.substring( 0, 7 );

// Flatten object
clonedEnv.primaryDomain = clonedEnv.primaryDomain.name;
delete clonedEnv.__typename;
return clonedEnv;
} );

return { header, data: clonedResponse.environments };

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

// console.log( formatData( clonedResponse.environments, "table" ) );
}
}
80 changes: 80 additions & 0 deletions src/commands/example-command.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { BaseVIPCommand } from '../lib/base-command';

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

export class ExampleCommand extends BaseVIPCommand {
protected readonly name: string = 'example';

protected readonly usage: CommandUsage = {
description: 'Example command',
examples: [
{
description: 'Example 1',
usage: 'vip example arg1 arg2',
},
{
description: 'Example 2',
usage: 'vip example --named=arg1 --also=arg2',
},
],
};

protected readonly commandOptions: CommandOption[] = [
{
name: '--slug <slug>, -s <slug>',
description: 'An env slug',
type: 'string',
required: true,
},
];

protected childCommands: BaseVIPCommand[] = [ new ExampleChildCommand() ];

protected execute( opts: unknown[] ): void {

Check warning on line 33 in src/commands/example-command.ts

View workflow job for this annotation

GitHub Actions / Lint

'opts' is defined but never used. Allowed unused args must match /^_/u
console.log( 'parent', this.getName() );
}
}

export class ExampleChildCommand extends BaseVIPCommand {
protected readonly name: string = 'child';
protected readonly usage: CommandUsage = {
description: 'Example child command',
examples: [
{
description: 'Example 1',
usage: 'vip example child arg1 arg2',
},
{
description: 'Example 2',
usage: 'vip example child --named=arg1 --also=arg2',
},
],
};

protected childCommands: BaseVIPCommand[] = [ new ExampleGrandChildCommand() ];

protected execute( opts: unknown[], ...args: unknown[] ): void {

Check warning on line 56 in src/commands/example-command.ts

View workflow job for this annotation

GitHub Actions / Lint

'opts' is defined but never used. Allowed unused args must match /^_/u

Check warning on line 56 in src/commands/example-command.ts

View workflow job for this annotation

GitHub Actions / Lint

'args' is defined but never used. Allowed unused args must match /^_/u
console.log( this.getName() );
}
}

export class ExampleGrandChildCommand extends BaseVIPCommand {
protected readonly name: string = 'grandchild';
protected readonly usage: CommandUsage = {
description: 'Example grandchild command',
examples: [
{
description: 'Example 1',
usage: 'vip example child arg1 arg2',
},
{
description: 'Example 2',
usage: 'vip example child --named=arg1 --also=arg2',
},
],
};

protected execute( opts: unknown[], ...args: unknown[] ): void {

Check warning on line 77 in src/commands/example-command.ts

View workflow job for this annotation

GitHub Actions / Lint

'opts' is defined but never used. Allowed unused args must match /^_/u
console.log( this.getName() );
}
}
Loading
Loading