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 all 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:[email protected]",
"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
83 changes: 83 additions & 0 deletions src/commands/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import chalk from 'chalk';

import app from '../lib/api/app';
import { BaseVIPCommand } from '../lib/base-command';
import { getEnvIdentifier } from '../lib/cli/command';
import { trackEvent } from '../lib/tracker';

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

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 {

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>'?
let res;
try {
res = await app(
arg[ 0 ],

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

View workflow job for this annotation

GitHub Actions / Type Checker

Argument of type 'unknown' is not assignable to parameter of type 'string | number'.

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

View workflow job for this annotation

GitHub Actions / Type Checker

Argument of type 'unknown' is not assignable to parameter of type 'string | number'.
'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 40 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 49 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 => {

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

View workflow job for this annotation

GitHub Actions / Type Checker

'clonedResponse.environments' is possibly 'null' or 'undefined'.

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

View workflow job for this annotation

GitHub Actions / Type Checker

'clonedResponse.environments' is possibly 'null' or 'undefined'.
const clonedEnv = Object.assign( {}, env );

clonedEnv.name = getEnvIdentifier( env );

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

View workflow job for this annotation

GitHub Actions / Type Checker

Argument of type 'Maybe<AppEnvironment>' is not assignable to parameter of type 'string'.

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

View workflow job for this annotation

GitHub Actions / Type Checker

Argument of type 'Maybe<AppEnvironment>' is not assignable to parameter of type 'string'.

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

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

View workflow job for this annotation

GitHub Actions / Type Checker

'clonedEnv.currentCommit' is possibly 'null' or 'undefined'.

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

View workflow job for this annotation

GitHub Actions / Type Checker

'clonedEnv.currentCommit' is possibly 'null' or 'undefined'.

// Flatten object
clonedEnv.primaryDomain = clonedEnv.primaryDomain.name;

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

View workflow job for this annotation

GitHub Actions / Type Checker

Type 'string' is not assignable to type 'Domain'.

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

View workflow job for this annotation

GitHub Actions / Type Checker

'clonedEnv.primaryDomain' is possibly 'null' or 'undefined'.

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

View workflow job for this annotation

GitHub Actions / Type Checker

Type 'string' is not assignable to type 'Domain'.

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

View workflow job for this annotation

GitHub Actions / Type Checker

'clonedEnv.primaryDomain' is possibly 'null' or 'undefined'.
delete clonedEnv.__typename;
return clonedEnv;
} );

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'.
}
}
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

Check warning on line 77 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() );
}
}
Loading
Loading