Skip to content

Commit 5fb127e

Browse files
committed
fix: export makeTable
1 parent 631d2cb commit 5fb127e

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

src/exported.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export { toHelpSection, parseVarArgs } from './util.js';
99
export { Progress } from './ux/progress.js';
1010
export { Spinner } from './ux/spinner.js';
1111
export { Ux } from './ux/ux.js';
12-
export { convertToNewTableAPI } from './ux/table.js';
12+
export { convertToNewTableAPI, makeTable } from './ux/table.js';
1313
export { StandardColors } from './ux/standardColors.js';
1414

1515
export { SfCommand, SfCommandInterface } from './sfCommand.js';

src/ux/table.ts

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
* Licensed under the BSD 3-Clause license.
55
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
66
*/
7-
import { TableOptions } from '@oclif/table';
7+
import { makeTable as oclifMakeTable, TableOptions } from '@oclif/table';
8+
import { env } from '@salesforce/kit';
89

910
type Column<T extends Record<string, unknown>> = {
1011
extended: boolean;
@@ -56,3 +57,57 @@ export function convertToNewTableAPI<T extends Record<string, unknown>>(
5657

5758
return { data: d, title: options?.title, borderStyle: 'headers-only-with-underline', columns: cols };
5859
}
60+
61+
export function getTableDefaults<T extends Record<string, unknown>>(
62+
options: TableOptions<T>
63+
): Pick<TableOptions<T>, 'borderStyle' | 'noStyle' | 'headerOptions'> {
64+
const borderStyles = [
65+
'all',
66+
'headers-only-with-outline',
67+
'headers-only-with-underline',
68+
'headers-only',
69+
'horizontal-with-outline',
70+
'horizontal',
71+
'none',
72+
'outline',
73+
'vertical-with-outline',
74+
'vertical',
75+
];
76+
77+
const defaultStyle = 'vertical-with-outline';
78+
const determineBorderStyle = (): TableOptions<T>['borderStyle'] => {
79+
const envVar = env.getString('SF_TABLE_BORDER_STYLE', defaultStyle);
80+
if (borderStyles.includes(envVar)) {
81+
return envVar as TableOptions<T>['borderStyle'];
82+
}
83+
84+
return defaultStyle;
85+
};
86+
87+
return {
88+
borderStyle: determineBorderStyle(),
89+
noStyle: env.getBoolean('SF_NO_TABLE_STYLE', false),
90+
headerOptions: {
91+
...options.headerOptions,
92+
formatter: 'capitalCase',
93+
},
94+
};
95+
}
96+
97+
/**
98+
* Generates a string representation of a table from the given options.
99+
*
100+
* Consumers should prefer to use the `table` method on the `Ux` class since that will
101+
* respond appropriately to the presence of the `--json` flag.
102+
*
103+
* @template T - The type of the records in the table.
104+
* @param {TableOptions<T>} options - The options to configure the table.
105+
* @returns {string} The string representation of the table.
106+
*/
107+
export function makeTable<T extends Record<string, unknown>>(options: TableOptions<T>): string {
108+
return oclifMakeTable({
109+
...options,
110+
// Don't allow anyone to override these properties
111+
...getTableDefaults(options),
112+
});
113+
}

src/ux/ux.ts

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import { ux } from '@oclif/core';
1010
import { AnyJson } from '@salesforce/ts-types';
1111
import terminalLink from 'terminal-link';
1212
import { printTable, TableOptions } from '@oclif/table';
13-
import { env } from '@salesforce/kit';
1413
import { UxBase } from './base.js';
1514
import { Spinner } from './spinner.js';
1615
import styledObject from './styledObject.js';
16+
import { getTableDefaults } from './table.js';
1717

1818
/**
1919
* UX methods for plugins. Automatically suppress console output if outputEnabled is set to false.
@@ -76,39 +76,11 @@ export class Ux extends UxBase {
7676
* @param options Table properties
7777
*/
7878
public table<T extends Record<string, unknown>>(options: TableOptions<T>): void {
79-
const borderStyles = [
80-
'all',
81-
'headers-only-with-outline',
82-
'headers-only-with-underline',
83-
'headers-only',
84-
'horizontal-with-outline',
85-
'horizontal',
86-
'none',
87-
'outline',
88-
'vertical-with-outline',
89-
'vertical',
90-
];
91-
92-
const defaultStyle = 'vertical-with-outline';
93-
const determineBorderStyle = (): TableOptions<T>['borderStyle'] => {
94-
const envVar = env.getString('SF_TABLE_BORDER_STYLE', defaultStyle);
95-
if (borderStyles.includes(envVar)) {
96-
return envVar as TableOptions<T>['borderStyle'];
97-
}
98-
99-
return defaultStyle;
100-
};
101-
10279
this.maybeNoop(() =>
10380
printTable({
10481
...options,
10582
// Don't allow anyone to override these properties
106-
borderStyle: determineBorderStyle(),
107-
noStyle: env.getBoolean('SF_NO_TABLE_STYLE', false),
108-
headerOptions: {
109-
...options.headerOptions,
110-
formatter: 'capitalCase',
111-
},
83+
...getTableDefaults(options),
11284
})
11385
);
11486
}

0 commit comments

Comments
 (0)