-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathdefault.ts
More file actions
125 lines (116 loc) · 3.88 KB
/
default.ts
File metadata and controls
125 lines (116 loc) · 3.88 KB
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
import Command, { flags as oclifFlags } from '@oclif/command'
import inquirer, { DistinctQuestion } from 'inquirer'
import inquirerDatepicker from 'inquirer-datepicker'
import { Logger } from 'winston'
import { LoggingService } from '../services/logging'
import { BagIdParserService } from '../services/parsers/BagIdParserService'
import { BucketIdParserService } from '../services/parsers/BucketIdParserService'
import { ConfigParserService } from '../services/parsers/ConfigParserService'
import { ReadonlyConfig } from '../types/config'
import ExitCodes from './ExitCodes'
export const flags = {
...oclifFlags,
integerArr: oclifFlags.build({
parse: (value: string) => {
const arr: number[] = value.split(',').map((v) => {
if (!/^-?\d+$/.test(v)) {
throw new Error(`Expected comma-separated integers, but received: ${value}`)
}
return parseInt(v)
})
return arr
},
}),
bagId: oclifFlags.build({
char: 'b',
parse: (value: string) => {
const parser = new BagIdParserService(value)
return parser.parse()
},
description: `Bag ID. Format: {bag_type}:{sub_type}:{id}.
- Bag types: 'static', 'dynamic'
- Sub types: 'static:council', 'static:wg', 'dynamic:member', 'dynamic:channel'
- Id:
- absent for 'static:council'
- working group name for 'static:wg'
- integer for 'dynamic:member' and 'dynamic:channel'
Examples:
- static:council
- static:wg:storage
- dynamic:member:4`,
}),
bucketId: oclifFlags.build({
char: 'B',
parse: (value: string) => {
return BucketIdParserService.parseBucketId(value)
},
description: `Distribution bucket ID in {familyId}:{bucketIndex} format.`,
}),
}
export default abstract class DefaultCommandBase extends Command {
protected appConfig!: ReadonlyConfig
protected logging!: LoggingService
protected autoConfirm!: boolean
private logger!: Logger
static flags = {
yes: flags.boolean({
required: false,
default: false,
description: 'Answer "yes" to any prompt, skipping any manual confirmations',
char: 'y',
}),
configPath: flags.string({
required: false,
default: process.env.CONFIG_PATH || './config.yml',
description: 'Path to config JSON/YAML file (relative to current working directory)',
char: 'c',
}),
}
async init(): Promise<void> {
const { configPath, yes } = this.parse(this.constructor as typeof DefaultCommandBase).flags
const configParser = new ConfigParserService(configPath)
this.appConfig = configParser.parse() as ReadonlyConfig
this.logging = LoggingService.withCLIConfig()
this.logger = this.logging.createLogger('CLI')
this.autoConfirm = !!(process.env.AUTO_CONFIRM === 'true' || parseInt(process.env.AUTO_CONFIRM || '') || yes)
inquirer.registerPrompt('datepicker', inquirerDatepicker)
}
public log(message: string, ...meta: unknown[]): void {
this.logger.info(message, ...meta)
}
public output(value: unknown): void {
console.log(value)
}
async requireConfirmation(
message = 'Are you sure you want to execute this action?',
defaultVal = false
): Promise<void> {
if (this.autoConfirm) {
return
}
const { confirmed } = await inquirer.prompt([{ type: 'confirm', name: 'confirmed', message, default: defaultVal }])
if (!confirmed) {
this.exit(ExitCodes.OK)
}
}
async datePrompt(question: DistinctQuestion): Promise<Date> {
const { result } = await inquirer.prompt([
{
...question,
type: 'datepicker',
name: 'result',
clearable: true,
default: new Date().toISOString(),
},
])
const date = new Date(result)
return date
}
async finally(err: unknown): Promise<void> {
if (!err) this.exit(ExitCodes.OK)
if (process.env.DEBUG === 'true') {
console.error(err)
}
super.finally(err as Error)
}
}