( command: SmartThingsCommand<FormatAndWriteListFlags>, config: FormatAndWriteListConfig<L>, list: L[], options?: FormatAndWriteListOptions, )
| 92 | * @param options See FormatAndWriteListOptions for details. |
| 93 | */ |
| 94 | export async function formatAndWriteList<L extends object>( |
| 95 | command: SmartThingsCommand<FormatAndWriteListFlags>, |
| 96 | config: FormatAndWriteListConfig<L>, |
| 97 | list: L[], |
| 98 | options?: FormatAndWriteListOptions, |
| 99 | ): Promise<void> { |
| 100 | const includeIndex = !!options?.includeIndex |
| 101 | const forUserQuery = options?.forUserQuery |
| 102 | let commonFormatter: OutputFormatter<L[]> |
| 103 | if (list.length === 0) { |
| 104 | const pluralName = config.pluralItemName ?? (config.itemName ? `${config.itemName}s` : 'items') |
| 105 | commonFormatter = () => options?.customNotFoundMessage ?? `no ${pluralName} found` |
| 106 | } else if ('buildListTableOutput' in config) { |
| 107 | const buildListTableOutput = config.buildListTableOutput |
| 108 | commonFormatter = data => buildListTableOutput(data) |
| 109 | } else if ('listTableFieldDefinitions' in config) { |
| 110 | commonFormatter = listTableFormatter<L>(command.tableGenerator, config.listTableFieldDefinitions, includeIndex) |
| 111 | } else if (config.sortKeyName) { |
| 112 | commonFormatter = |
| 113 | listTableFormatter<L>(command.tableGenerator, [config.sortKeyName, config.primaryKeyName], includeIndex) |
| 114 | } else { |
| 115 | commonFormatter = listTableFormatter<L>(command.tableGenerator, [config.primaryKeyName], includeIndex) |
| 116 | } |
| 117 | const outputFormatter = forUserQuery |
| 118 | ? commonFormatter |
| 119 | : buildOutputFormatter(command.flags, command.cliConfig, undefined, commonFormatter) |
| 120 | await writeOutput(outputFormatter(list), forUserQuery ? undefined : command.flags.output) |
| 121 | } |
no test coverage detected