Skip to content

Commit

Permalink
fix: export makeTable
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Oct 22, 2024
1 parent 631d2cb commit 5fb127e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/exported.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export { toHelpSection, parseVarArgs } from './util.js';
export { Progress } from './ux/progress.js';
export { Spinner } from './ux/spinner.js';
export { Ux } from './ux/ux.js';
export { convertToNewTableAPI } from './ux/table.js';
export { convertToNewTableAPI, makeTable } from './ux/table.js';
export { StandardColors } from './ux/standardColors.js';

export { SfCommand, SfCommandInterface } from './sfCommand.js';
Expand Down
57 changes: 56 additions & 1 deletion src/ux/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
import { TableOptions } from '@oclif/table';
import { makeTable as oclifMakeTable, TableOptions } from '@oclif/table';
import { env } from '@salesforce/kit';

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

return { data: d, title: options?.title, borderStyle: 'headers-only-with-underline', columns: cols };
}

export function getTableDefaults<T extends Record<string, unknown>>(
options: TableOptions<T>
): Pick<TableOptions<T>, 'borderStyle' | 'noStyle' | 'headerOptions'> {
const borderStyles = [
'all',
'headers-only-with-outline',
'headers-only-with-underline',
'headers-only',
'horizontal-with-outline',
'horizontal',
'none',
'outline',
'vertical-with-outline',
'vertical',
];

const defaultStyle = 'vertical-with-outline';
const determineBorderStyle = (): TableOptions<T>['borderStyle'] => {
const envVar = env.getString('SF_TABLE_BORDER_STYLE', defaultStyle);
if (borderStyles.includes(envVar)) {
return envVar as TableOptions<T>['borderStyle'];
}

return defaultStyle;
};

return {
borderStyle: determineBorderStyle(),
noStyle: env.getBoolean('SF_NO_TABLE_STYLE', false),
headerOptions: {
...options.headerOptions,
formatter: 'capitalCase',
},
};
}

/**
* Generates a string representation of a table from the given options.
*
* Consumers should prefer to use the `table` method on the `Ux` class since that will
* respond appropriately to the presence of the `--json` flag.
*
* @template T - The type of the records in the table.
* @param {TableOptions<T>} options - The options to configure the table.
* @returns {string} The string representation of the table.
*/
export function makeTable<T extends Record<string, unknown>>(options: TableOptions<T>): string {
return oclifMakeTable({
...options,
// Don't allow anyone to override these properties
...getTableDefaults(options),
});
}
32 changes: 2 additions & 30 deletions src/ux/ux.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { ux } from '@oclif/core';
import { AnyJson } from '@salesforce/ts-types';
import terminalLink from 'terminal-link';
import { printTable, TableOptions } from '@oclif/table';
import { env } from '@salesforce/kit';
import { UxBase } from './base.js';
import { Spinner } from './spinner.js';
import styledObject from './styledObject.js';
import { getTableDefaults } from './table.js';

/**
* UX methods for plugins. Automatically suppress console output if outputEnabled is set to false.
Expand Down Expand Up @@ -76,39 +76,11 @@ export class Ux extends UxBase {
* @param options Table properties
*/
public table<T extends Record<string, unknown>>(options: TableOptions<T>): void {
const borderStyles = [
'all',
'headers-only-with-outline',
'headers-only-with-underline',
'headers-only',
'horizontal-with-outline',
'horizontal',
'none',
'outline',
'vertical-with-outline',
'vertical',
];

const defaultStyle = 'vertical-with-outline';
const determineBorderStyle = (): TableOptions<T>['borderStyle'] => {
const envVar = env.getString('SF_TABLE_BORDER_STYLE', defaultStyle);
if (borderStyles.includes(envVar)) {
return envVar as TableOptions<T>['borderStyle'];
}

return defaultStyle;
};

this.maybeNoop(() =>
printTable({
...options,
// Don't allow anyone to override these properties
borderStyle: determineBorderStyle(),
noStyle: env.getBoolean('SF_NO_TABLE_STYLE', false),
headerOptions: {
...options.headerOptions,
formatter: 'capitalCase',
},
...getTableDefaults(options),
})
);
}
Expand Down

0 comments on commit 5fb127e

Please sign in to comment.