/
list.ts
180 lines (135 loc) · 5 KB
/
list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import Command, { Flags, cliux } from '../../base'
import Table, { type HorizontalAlignment } from 'cli-table3'
import type { Export, QueryPageSize, QueryParamsList } from '@commercelayer/sdk'
import { clApi, clColor, clConfig, clOutput, clText, clUtil } from '@commercelayer/cli-core'
import type { CommandError } from '@oclif/core/lib/interfaces'
const MAX_EXPORTS = 1000
export default class ExportsList extends Command {
static description = 'list all the created exports'
static aliases = ['exp:list']
static examples = [
'$ commercelayer exports',
'$ cl exports:list -A',
'$ cl exp:list',
]
static flags = {
all: Flags.boolean({
char: 'A',
description: `show all exports instead of first ${clConfig.api.page_max_size} only`,
exclusive: ['limit'],
}),
type: Flags.string({
char: 't',
description: 'the type of resource exported',
options: clConfig.exports.types as string[],
multiple: false,
}),
status: Flags.string({
char: 's',
description: 'the export job status',
options: clConfig.exports.statuses as string[],
multiple: false,
}),
limit: Flags.integer({
char: 'l',
description: 'limit number of exports in output',
exclusive: ['all'],
}),
}
async run(): Promise<any> {
const { flags } = await this.parse(ExportsList)
if (flags.limit && (flags.limit < 1)) this.error(clColor.italic('Limit') + ' must be a positive integer')
this.commercelayerInit(flags)
try {
let pageSize = clConfig.api.page_max_size as QueryPageSize
const tableData = []
let currentPage = 0
let pageCount = 1
let itemCount = 0
let totalItems = 1
if (flags.limit) pageSize = Math.min(flags.limit, pageSize) as QueryPageSize
cliux.action.start('Fetching exports')
let delay = 0
while (currentPage < pageCount) {
const params: QueryParamsList<Export> = {
pageSize,
pageNumber: ++currentPage,
sort: ['-started_at'],
filters: {},
}
if (params?.filters) {
if (flags.type) params.filters.resource_type_eq = flags.type
if (flags.status) params.filters.status_eq = flags.status
}
// eslint-disable-next-line no-await-in-loop
const exports = await this.cl.exports.list(params)
if (exports?.length) {
tableData.push(...exports)
currentPage = exports.meta.currentPage
if (currentPage === 1) {
pageCount = this.computeNumPages(flags, exports.meta)
totalItems = exports.meta.recordCount
delay = clApi.requestRateLimitDelay({ resourceType: this.cl.exports.type(), totalRequests: pageCount })
}
itemCount += exports.length
if (delay > 0) await clUtil.sleep(delay)
}
}
cliux.action.stop()
this.log()
if (tableData?.length) {
const table = new Table({
head: ['ID', 'Resource type', 'Status', 'Items', 'Format', 'Dry data', 'Started at'],
// colWidths: [100, 200],
style: {
head: ['brightYellow'],
compact: false,
},
})
// let index = 0
table.push(...tableData.map(e => [
// { content: ++index, hAlign: 'right' as HorizontalAlignment },
clColor.blueBright(e.id || ''),
e.resource_type || '',
{ content: this.exportStatus(e.status), hAlign: 'center' as HorizontalAlignment },
{ content: e.records_count, hAlign: 'center' as HorizontalAlignment },
{ content: e.format, hAlign: 'center' as HorizontalAlignment },
{ content: (e.dry_data? clText.symbols.check.small : ''), hAlign: 'center' as HorizontalAlignment },
clOutput.localeDate(e.started_at || ''),
]))
this.log(table.toString())
this.footerMessage(flags, itemCount, totalItems)
} else this.log(clColor.italic('No exports found'))
this.log()
return tableData
} catch (error) {
this.handleError(error as CommandError, flags)
}
}
private footerMessage(flags: any, itemCount: number, totalItems: number): void {
this.log()
this.log(`Total displayed exports: ${clColor.yellowBright(String(itemCount))}`)
this.log(`Total export count: ${clColor.yellowBright(String(totalItems))}`)
if (itemCount < totalItems) {
if (flags.all || ((flags.limit || 0) > MAX_EXPORTS)) {
this.log()
this.warn(`The maximum number of exports that can be displayed is ${clColor.yellowBright(String(MAX_EXPORTS))}`)
} else
if (!flags.limit) {
this.log()
const displayedMsg = `Only ${clColor.yellowBright(String(itemCount))} of ${clColor.yellowBright(String(totalItems))} records are displayed`
if (totalItems < MAX_EXPORTS) this.warn(`${displayedMsg}, to see all existing items run the command with the ${clColor.cli.flag('--all')} flag enabled`)
else this.warn(`${displayedMsg}, to see more items (max ${MAX_EXPORTS}) run the command with the ${clColor.cli.flag('--limit')} flag enabled`)
}
}
}
private computeNumPages(flags: any, meta: any): number {
let numRecord = 25
if (flags.all) numRecord = meta.recordCount
else
if (flags.limit) numRecord = flags.limit
numRecord = Math.min(MAX_EXPORTS, numRecord)
const numPages = Math.ceil(numRecord / 25)
return numPages
}
}