|
4 | 4 | * Licensed under the BSD 3-Clause license.
|
5 | 5 | * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
6 | 6 | */
|
7 |
| -import { TableOptions } from '@oclif/table'; |
| 7 | +import { makeTable as oclifMakeTable, TableOptions } from '@oclif/table'; |
| 8 | +import { env } from '@salesforce/kit'; |
8 | 9 |
|
9 | 10 | type Column<T extends Record<string, unknown>> = {
|
10 | 11 | extended: boolean;
|
@@ -56,3 +57,57 @@ export function convertToNewTableAPI<T extends Record<string, unknown>>(
|
56 | 57 |
|
57 | 58 | return { data: d, title: options?.title, borderStyle: 'headers-only-with-underline', columns: cols };
|
58 | 59 | }
|
| 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 | +} |
0 commit comments