-
Notifications
You must be signed in to change notification settings - Fork 0
/
last.ts
76 lines (50 loc) · 1.84 KB
/
last.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
import type { CommerceLayerClient } from '@commercelayer/sdk'
import Command, { BaseCommand } from '../../base'
import { clApi, clColor, clText } from '@commercelayer/cli-core'
export default class ResourcesLast extends BaseCommand {
static description = 'show the last id of a resource type'
static aliases = ['last', 'res:last']
static examples = [
'$ commercelayer resources:last customers',
'$ commercelayer last customer',
'$ cl res:last customers'
]
static flags = {
...BaseCommand.flags
}
static args = {
...Command.args
}
async run(): Promise<any> {
const { args, flags } = await this.parse(ResourcesLast)
const resource = this.checkResource(args.resource, { singular: true })
try {
const last = this.lastResources(flags.organization)[resource.api]
let id = clColor.dim('none')
let label = ''
if (last) { // Retrieve last resource
id = clColor.yellowBright(last)
const cl = this.initCommerceLayer(flags)
const resSdk: any = cl[resource.api as keyof CommerceLayerClient]
const res = await resSdk.retrieve(last).catch(() => { /* do nothing */ })
if (res) {
const fields = ['reference', 'name', 'code', 'number', 'email', 'label', 'description']
for (const f of fields) {
if (f in res) {
const v = res[f]
const value = v?.includes(' ') ? `'${v}'` : v
if (value) {
label = `${clText.capitalize(clApi.humanizeResource(resource.name))} ${f}: ${clColor.cli.value(value)}`
break
}
}
}
}
}
this.log(`\n${clColor.api.resource(resource.api)} last ID: ${id}\n`)
if (label) this.log(`[ ${label} ]\n`)
} catch (error: any) {
this.printError(error, flags, args)
}
}
}