|
1 | 1 | import {ux} from '@oclif/core'; |
2 | | -import cliui from 'cliui'; |
3 | | -import {InstanceCommand} from '@salesforce/b2c-tooling/cli'; |
| 2 | +import {InstanceCommand, createTable, type ColumnDef} from '@salesforce/b2c-tooling/cli'; |
4 | 3 | import {listCodeVersions, type CodeVersion, type CodeVersionResult} from '@salesforce/b2c-tooling/operations/code'; |
5 | 4 | import {t} from '../../i18n/index.js'; |
6 | 5 |
|
| 6 | +const COLUMNS: Record<string, ColumnDef<CodeVersion>> = { |
| 7 | + id: { |
| 8 | + header: 'ID', |
| 9 | + get: (v) => v.id || '-', |
| 10 | + }, |
| 11 | + active: { |
| 12 | + header: 'Active', |
| 13 | + get: (v) => (v.active ? 'Yes' : 'No'), |
| 14 | + }, |
| 15 | + rollback: { |
| 16 | + header: 'Rollback', |
| 17 | + get: (v) => (v.rollback ? 'Yes' : 'No'), |
| 18 | + }, |
| 19 | + lastModified: { |
| 20 | + header: 'Last Modified', |
| 21 | + get: (v) => (v.last_modification_time ? new Date(v.last_modification_time).toLocaleString() : '-'), |
| 22 | + }, |
| 23 | + cartridges: { |
| 24 | + header: 'Cartridges', |
| 25 | + get: (v) => String(v.cartridges?.length ?? 0), |
| 26 | + }, |
| 27 | +}; |
| 28 | + |
| 29 | +const DEFAULT_COLUMNS = ['id', 'active', 'rollback', 'lastModified', 'cartridges']; |
| 30 | + |
7 | 31 | export default class CodeList extends InstanceCommand<typeof CodeList> { |
8 | 32 | static description = t('commands.code.list.description', 'List code versions on a B2C Commerce instance'); |
9 | 33 |
|
@@ -41,42 +65,8 @@ export default class CodeList extends InstanceCommand<typeof CodeList> { |
41 | 65 | return result; |
42 | 66 | } |
43 | 67 |
|
44 | | - this.printVersionsTable(versions); |
| 68 | + createTable(COLUMNS).render(versions, DEFAULT_COLUMNS); |
45 | 69 |
|
46 | 70 | return result; |
47 | 71 | } |
48 | | - |
49 | | - private printVersionsTable(versions: CodeVersion[]): void { |
50 | | - const ui = cliui({width: process.stdout.columns || 80}); |
51 | | - |
52 | | - // Header |
53 | | - ui.div( |
54 | | - {text: 'ID', width: 25, padding: [0, 2, 0, 0]}, |
55 | | - {text: 'Active', width: 10, padding: [0, 2, 0, 0]}, |
56 | | - {text: 'Rollback', width: 10, padding: [0, 2, 0, 0]}, |
57 | | - {text: 'Last Modified', width: 25, padding: [0, 2, 0, 0]}, |
58 | | - {text: 'Cartridges', padding: [0, 0, 0, 0]}, |
59 | | - ); |
60 | | - |
61 | | - // Separator |
62 | | - ui.div({text: '─'.repeat(80), padding: [0, 0, 0, 0]}); |
63 | | - |
64 | | - // Rows |
65 | | - for (const version of versions) { |
66 | | - const lastModified = version.last_modification_time |
67 | | - ? new Date(version.last_modification_time).toLocaleString() |
68 | | - : '-'; |
69 | | - const cartridgeCount = version.cartridges?.length ?? 0; |
70 | | - |
71 | | - ui.div( |
72 | | - {text: version.id || '', width: 25, padding: [0, 2, 0, 0]}, |
73 | | - {text: version.active ? 'Yes' : 'No', width: 10, padding: [0, 2, 0, 0]}, |
74 | | - {text: version.rollback ? 'Yes' : 'No', width: 10, padding: [0, 2, 0, 0]}, |
75 | | - {text: lastModified, width: 25, padding: [0, 2, 0, 0]}, |
76 | | - {text: String(cartridgeCount), padding: [0, 0, 0, 0]}, |
77 | | - ); |
78 | | - } |
79 | | - |
80 | | - ux.stdout(ui.toString()); |
81 | | - } |
82 | 72 | } |
0 commit comments