From 5fb127e0a061d6127fcad7e005fc1b6679afa73d Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Tue, 22 Oct 2024 13:35:42 -0600 Subject: [PATCH] fix: export makeTable --- src/exported.ts | 2 +- src/ux/table.ts | 57 ++++++++++++++++++++++++++++++++++++++++++++++++- src/ux/ux.ts | 32 ++------------------------- 3 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/exported.ts b/src/exported.ts index cb711af0..99619dfd 100644 --- a/src/exported.ts +++ b/src/exported.ts @@ -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'; diff --git a/src/ux/table.ts b/src/ux/table.ts index c7790819..c4e4791b 100644 --- a/src/ux/table.ts +++ b/src/ux/table.ts @@ -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> = { extended: boolean; @@ -56,3 +57,57 @@ export function convertToNewTableAPI>( return { data: d, title: options?.title, borderStyle: 'headers-only-with-underline', columns: cols }; } + +export function getTableDefaults>( + options: TableOptions +): Pick, '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['borderStyle'] => { + const envVar = env.getString('SF_TABLE_BORDER_STYLE', defaultStyle); + if (borderStyles.includes(envVar)) { + return envVar as TableOptions['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} options - The options to configure the table. + * @returns {string} The string representation of the table. + */ +export function makeTable>(options: TableOptions): string { + return oclifMakeTable({ + ...options, + // Don't allow anyone to override these properties + ...getTableDefaults(options), + }); +} diff --git a/src/ux/ux.ts b/src/ux/ux.ts index 7abca9f7..a6ddb307 100644 --- a/src/ux/ux.ts +++ b/src/ux/ux.ts @@ -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. @@ -76,39 +76,11 @@ export class Ux extends UxBase { * @param options Table properties */ public table>(options: TableOptions): 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['borderStyle'] => { - const envVar = env.getString('SF_TABLE_BORDER_STYLE', defaultStyle); - if (borderStyles.includes(envVar)) { - return envVar as TableOptions['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), }) ); }