Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 44 additions & 0 deletions cli/src/cli-table.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Table, { type Cell, type TableConstructorOptions } from 'cli-table3';
import { wrapText } from './wrap-text.js';

export interface CLITableOptions extends Omit<TableConstructorOptions, 'wordWrap' | 'wrapOnWordBoundary'> {
columnOverhead?: number;
}

/**
* A cli-table3 wrapper that automatically wraps cell text based on colWidths.
* This replaces cli-table3's built-in wordWrap which deadlocks on large inputs (#2619).
*/
export class CLITable {
private table: InstanceType<typeof Table>;
private colWidths: (number | null)[] | undefined;
private columnOverhead: number;

constructor(options: CLITableOptions = {}) {
const { columnOverhead, ...tableOptions } = options;
this.table = new Table(tableOptions);
this.colWidths = tableOptions.colWidths;

const { style } = this.table.options;
this.columnOverhead = columnOverhead ?? style['padding-left'] + style['padding-right'];
}

push(...rows: Cell[][]): void {
for (const row of rows) {
const wrapped = this.colWidths
? row.map((cell, i) => {
const width = this.colWidths![i];
if (typeof cell === 'string' && typeof width === 'number') {
return wrapText(cell, width - this.columnOverhead);
}
return cell;
})
: row;
this.table.push(wrapped);
}
}

toString(): string {
return this.table.toString();
}
}
Comment on lines +12 to +44
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do export ... as Table instead of changing every import to CLITable this would shrink the diff by a lot

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@endigma I made it different on purpose, since exporting as Table could be confused with the original cli-table3 package, but I can change it feel strongly about reducing the diff?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more a nit than anything, up to you

11 changes: 4 additions & 7 deletions cli/src/commands/contract/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { readFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import Table from 'cli-table3';
import { Command, program } from 'commander';
import { resolve } from 'pathe';
import pc from 'picocolors';
import ora from 'ora';
import { CLITable } from '../../../cli-table.js';
import { getBaseHeaders } from '../../../core/config.js';
import { BaseCommandOptions } from '../../../core/types/types.js';

Expand Down Expand Up @@ -88,15 +88,14 @@ export default (opts: BaseCommandOptions) => {
case EnumStatusCode.ERR_SUBGRAPH_COMPOSITION_FAILED: {
spinner.fail('Contract created but with composition errors.');

const compositionErrorsTable = new Table({
const compositionErrorsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('FEATURE_FLAG')),
pc.bold(pc.white('ERROR_MESSAGE')),
],
colWidths: [30, 30, 30, 120],
wordWrap: true,
});

for (const compositionError of resp.compositionErrors) {
Expand All @@ -115,14 +114,13 @@ export default (opts: BaseCommandOptions) => {
"The contract was created, but the composition hasn't been deployed, so it's not accessible to the router. Check the errors listed below for details.",
);

const deploymentErrorsTable = new Table({
const deploymentErrorsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('ERROR_MESSAGE')),
],
colWidths: [30, 30, 120],
wordWrap: true,
});

for (const deploymentError of resp.deploymentErrors) {
Expand All @@ -146,15 +144,14 @@ export default (opts: BaseCommandOptions) => {
}

if (!options.suppressWarnings && resp.compositionWarnings.length > 0) {
const compositionWarningsTable = new Table({
const compositionWarningsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('FEATURE_FLAG')),
pc.bold(pc.white('WARNING_MESSAGE')),
],
colWidths: [30, 30, 30, 120],
wordWrap: true,
});

console.log(pc.yellow(`The following warnings were produced while composing the federated graph:`));
Expand Down
11 changes: 4 additions & 7 deletions cli/src/commands/contract/commands/update.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { existsSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import Table from 'cli-table3';
import { Command, program } from 'commander';
import pc from 'picocolors';
import ora from 'ora';
import { resolve } from 'pathe';
import { CLITable } from '../../../cli-table.js';
import { getBaseHeaders } from '../../../core/config.js';
import { BaseCommandOptions } from '../../../core/types/types.js';

Expand Down Expand Up @@ -85,15 +85,14 @@ export default (opts: BaseCommandOptions) => {
case EnumStatusCode.ERR_SUBGRAPH_COMPOSITION_FAILED: {
spinner.fail('Contract updated but with composition errors.');

const compositionErrorsTable = new Table({
const compositionErrorsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('FEATURE_FLAG')),
pc.bold(pc.white('ERROR_MESSAGE')),
],
colWidths: [30, 30, 30, 120],
wordWrap: true,
});

for (const compositionError of resp.compositionErrors) {
Expand All @@ -112,14 +111,13 @@ export default (opts: BaseCommandOptions) => {
"The contract was updated, but the updated composition hasn't been deployed, so it's not accessible to the router. Check the errors listed below for details.",
);

const deploymentErrorsTable = new Table({
const deploymentErrorsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('ERROR_MESSAGE')),
],
colWidths: [30, 30, 120],
wordWrap: true,
});

for (const deploymentError of resp.deploymentErrors) {
Expand All @@ -143,15 +141,14 @@ export default (opts: BaseCommandOptions) => {
}

if (!options.suppressWarnings && resp.compositionWarnings.length > 0) {
const compositionWarningsTable = new Table({
const compositionWarningsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('FEATURE_FLAG')),
pc.bold(pc.white('WARNING_MESSAGE')),
],
colWidths: [30, 30, 30, 120],
wordWrap: true,
});

console.log(pc.yellow(`The following warnings were produced while composing the federated graph:`));
Expand Down
5 changes: 2 additions & 3 deletions cli/src/commands/feature-flag/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { writeFile } from 'node:fs/promises';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import { Command, program } from 'commander';
import pc from 'picocolors';
import Table from 'cli-table3';
import { joinLabel } from '@wundergraph/cosmo-shared';
import { resolve } from 'pathe';
import { CLITable } from '../../../cli-table.js';
import { getBaseHeaders } from '../../../core/config.js';
import { BaseCommandOptions } from '../../../core/types/types.js';

Expand Down Expand Up @@ -60,7 +60,7 @@ export default (opts: BaseCommandOptions) => {
return;
}

const featureFlagsTable = new Table({
const featureFlagsTable = new CLITable({
head: [
pc.bold(pc.white('NAME')),
pc.bold(pc.white('NAMESPACE')),
Expand All @@ -69,7 +69,6 @@ export default (opts: BaseCommandOptions) => {
pc.bold(pc.white('UPDATED_AT')),
],
colWidths: [20, 20, 30, 15, 30],
wordWrap: true,
});

for (const ff of resp.featureFlags) {
Expand Down
6 changes: 2 additions & 4 deletions cli/src/commands/graph/common/version/commands/get.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import { Command, program } from 'commander';
import pc from 'picocolors';
import Table from 'cli-table3';
import { CLITable } from '../../../../../cli-table.js';
import { getBaseHeaders } from '../../../../../core/config.js';
import { CommonGraphCommandOptions } from '../../../../../core/types/types.js';

Expand Down Expand Up @@ -33,10 +33,8 @@ export default (opts: CommonGraphCommandOptions) => {
program.error(message);
}

const versionsTable = new Table({
const versionsTable = new CLITable({
head: [pc.bold(pc.white('GRAPH NAME')), pc.bold(pc.white('NAMESPACE')), pc.bold(pc.white('VERSION'))],
wordWrap: true,
wrapOnWordBoundary: false,
});

versionsTable.push([name, response.graph.namespace, response.graph.routerCompatibilityVersion]);
Expand Down
11 changes: 3 additions & 8 deletions cli/src/commands/graph/common/version/commands/set.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ROUTER_COMPATIBILITY_VERSIONS } from '@wundergraph/composition';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import Table from 'cli-table3';
import { Command, program } from 'commander';
import ora from 'ora';
import pc from 'picocolors';
import { CLITable } from '../../../../../cli-table.js';
import { getBaseHeaders } from '../../../../../core/config.js';
import { CommonGraphCommandOptions } from '../../../../../core/types/types.js';
import { handleCompositionResult } from '../../../../../handle-composition-result.js';
Expand Down Expand Up @@ -61,24 +61,19 @@ export default (opts: CommonGraphCommandOptions) => {
`${options.version} is not a valid router compatibility version. Please input one of the following valid versions:`,
)}`,
);
const validVersionsTable = new Table({
wordWrap: true,
wrapOnWordBoundary: false,
});
const validVersionsTable = new CLITable();

validVersionsTable.push([pc.bold(pc.white('VERSION')), ...ROUTER_COMPATIBILITY_VERSIONS]);
program.error(validVersionsTable.toString());
}

const versionsTable = new Table({
const versionsTable = new CLITable({
head: [
pc.bold(pc.white('GRAPH NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('PREVIOUS VERSION')),
pc.bold(pc.white('NEW VERSION')),
],
wordWrap: true,
wrapOnWordBoundary: false,
});

versionsTable.push([name, options.namespace || 'default', response.previousVersion, response.newVersion]);
Expand Down
11 changes: 4 additions & 7 deletions cli/src/commands/graph/federated-graph/commands/check.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import Table from 'cli-table3';
import { Command, program } from 'commander';
import logSymbols from 'log-symbols';
import pc from 'picocolors';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import { joinLabel } from '@wundergraph/cosmo-shared';
import { CLITable } from '../../../../cli-table.js';
import { BaseCommandOptions } from '../../../../core/types/types.js';
import { getBaseHeaders } from '../../../../core/config.js';
import { limitMaxValue } from '../../../../constants.js';
Expand Down Expand Up @@ -50,27 +50,24 @@ export default (opts: BaseCommandOptions) => {
},
);

const compositionErrorsTable = new Table({
const compositionErrorsTable = new CLITable({
head: [pc.bold(pc.white('ERROR_MESSAGE'))],
colWidths: [120],
wordWrap: true,
});

const compositionWarningsTable = new Table({
const compositionWarningsTable = new CLITable({
head: [pc.bold(pc.white('WARNING_MESSAGE'))],
colWidths: [120],
wordWrap: true,
});

const matchedSubgraphsTable = new Table({
const matchedSubgraphsTable = new CLITable({
head: [
pc.bold(pc.white('NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('URL')),
pc.bold(pc.white('LABELS')),
],
colWidths: [30, 30, 40, 50],
wordWrap: true,
});

switch (resp.response?.code) {
Expand Down
11 changes: 4 additions & 7 deletions cli/src/commands/graph/federated-graph/commands/create.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { readFile } from 'node:fs/promises';
import { existsSync } from 'node:fs';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import Table from 'cli-table3';
import { Command, program } from 'commander';
import { resolve } from 'pathe';
import pc from 'picocolors';
import ora from 'ora';
import { CLITable } from '../../../../cli-table.js';
import { getBaseHeaders } from '../../../../core/config.js';
import { BaseCommandOptions } from '../../../../core/types/types.js';

Expand Down Expand Up @@ -79,15 +79,14 @@ export default (opts: BaseCommandOptions) => {
case EnumStatusCode.ERR_SUBGRAPH_COMPOSITION_FAILED: {
spinner.warn('Federated Graph was created but with composition errors.');

const compositionErrorsTable = new Table({
const compositionErrorsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('FEATURE_FLAG')),
pc.bold(pc.white('ERROR_MESSAGE')),
],
colWidths: [30, 30, 30, 120],
wordWrap: true,
});

console.log(
Expand All @@ -113,14 +112,13 @@ export default (opts: BaseCommandOptions) => {
"The Federated Graph was set up, but the updated composition hasn't been deployed, so it's not accessible to the router. Check the errors listed below for details.",
);

const deploymentErrorsTable = new Table({
const deploymentErrorsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('ERROR_MESSAGE')),
],
colWidths: [30, 30, 120],
wordWrap: true,
});

for (const deploymentError of resp.deploymentErrors) {
Expand All @@ -146,15 +144,14 @@ export default (opts: BaseCommandOptions) => {
}

if (!options.suppressWarnings && resp.compositionWarnings.length > 0) {
const compositionWarningsTable = new Table({
const compositionWarningsTable = new CLITable({
head: [
pc.bold(pc.white('FEDERATED_GRAPH_NAME')),
pc.bold(pc.white('NAMESPACE')),
pc.bold(pc.white('FEATURE_FLAG')),
pc.bold(pc.white('WARNING_MESSAGE')),
],
colWidths: [30, 30, 30, 120],
wordWrap: true,
});

console.log(pc.yellow(`The following warnings were produced while composing the federated graph:`));
Expand Down
5 changes: 2 additions & 3 deletions cli/src/commands/graph/federated-graph/commands/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { writeFile } from 'node:fs/promises';
import { Command, program } from 'commander';
import pc from 'picocolors';
import { EnumStatusCode } from '@wundergraph/cosmo-connect/dist/common/common_pb';
import Table from 'cli-table3';
import logSymbols from 'log-symbols';
import { resolve } from 'pathe';
import { CLITable } from '../../../../cli-table.js';
import { BaseCommandOptions } from '../../../../core/types/types.js';
import { getBaseHeaders } from '../../../../core/config.js';

Expand Down Expand Up @@ -92,7 +92,7 @@ export default (opts: BaseCommandOptions) => {
return;
}

const graphsTable = new Table({
const graphsTable = new CLITable({
head: [
pc.bold(pc.white('NAME')),
pc.bold(pc.white('NAMESPACE')),
Expand All @@ -104,7 +104,6 @@ export default (opts: BaseCommandOptions) => {
],
colAligns: ['left', 'left', 'left', 'left', 'center', 'left', 'center'],
colWidths: [25, 25, 40, 70, 15, 30, 15],
wordWrap: true,
});

for (const graph of filteredGraphs) {
Expand Down
Loading
Loading