-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.ts
102 lines (64 loc) · 2.84 KB
/
count.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
import { type KeyValString, clColor, clApi, clUtil, clFilter } from '@commercelayer/cli-core'
import { CommerceLayer, type CommerceLayerClient, type ListResponse, type Resource } from '@commercelayer/sdk'
import { BaseCommand, Flags, Args, cliux } from '../../base'
export default class ResourcesCount extends BaseCommand {
static description = 'count the number of existent resources'
static aliases = ['count', 'res:count', 'rs:count']
static examples = [
'commercelayer resources:count customers',
'cl count customers -w customer_group_name_eq=<customer-group-name>'
]
static flags = {
...BaseCommand.flags,
where: Flags.string({
char: 'w',
multiple: true,
description: 'comma separated list of query filters'
})
}
static args = {
resource: Args.string({ name: 'resource', description: 'the resource type', required: true })
}
public async run(): Promise<void> {
const { args, flags } = await this.parse(ResourcesCount)
const resource = this.checkResource(args.resource)
const organization = flags.organization
const domain = flags.domain
const accessToken = flags.accessToken
const cl = CommerceLayer({ organization, domain, accessToken, userAgent: clUtil.userAgent(this.config) })
const resSdk: any = cl[resource.api as keyof CommerceLayerClient]
this.checkOperation(resSdk)
const filters = this.whereFlag(flags.where)
const humanized = clApi.humanizeResource(resource.api as string)
this.log()
if (!flags.doc) cliux.action.start(`Counting ${humanized}`)
const res = await resSdk.list({ filters, pageNumber: 1, pageSize: 1 }) as ListResponse<Resource>
if (res?.recordCount) cliux.action.stop(clColor.yellowBright(res.recordCount.toLocaleString()))
else {
cliux.action.stop(clColor.msg.error('error'))
this.error(`\nError counting ${humanized}`)
}
this.log()
}
protected checkOperation(sdk: any): boolean {
if (!sdk.count) this.error(`${clColor.msg.error('Count')} operation not supported for resource ${clColor.api.resource(sdk.type())}`)
return true
}
whereFlag(flag: string[] | undefined): KeyValString {
const wheres: KeyValString = {}
if (flag && (flag.length > 0)) {
flag.forEach(f => {
const wt = f.split('=')
if (wt.length < 2) this.error(`Filter flag must be in the form ${clColor.style.attribute('predicate=value')}`)
const w = wt[0]
if (!clFilter.available(w)) this.error(`Invalid query filter: ${clColor.style.error(w)}`, {
suggestions: [`Execute command ${clColor.style.command('resources:filters')} to get a full list of all available filter predicates`],
ref: 'https://docs.commercelayer.io/api/filtering-data#list-of-predicates',
})
const v = wt[1]
wheres[w] = v
})
}
return wheres
}
}