-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcommon.ts
314 lines (277 loc) · 9.74 KB
/
common.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import chalk from 'chalk'
import { EventEmitter } from 'events'
import { program, Command as Commander, Option } from 'commander'
import yargsParser from 'yargs-parser'
import { CloudBaseError } from '../error'
import { ICommandContext } from '../types'
import { Credential, execWithLoading } from '@cloudbase/toolbox'
import {
usageStore,
collectUsage,
loadingFactory,
getNotification,
getCloudBaseConfig,
authSupevisor,
getPrivateSettings
} from '../utils'
import { login } from '../auth'
import { beaconAction } from '../utils/report'
type PrivateCredential = Pick<Credential, 'secretId' | 'secretKey'>
interface ICommandOption {
flags: string
desc: string
hideHelp?: boolean
}
export interface ICommandOptions {
// 废弃的命令
deprecateCmd?: string
// 基础资源命令
cmd: string
// 嵌套子命令
childCmd?:
| string
| {
cmd: string
desc: string
}
childSubCmd?: string
// 命令选项
options: ICommandOption[]
// 命令描述
desc: string
// 使用命令时是否必须要传入 EnvId
requiredEnvId?: boolean
// 多数命令都需要登陆,不需要登陆的命令需要特别声明
withoutAuth?: boolean
// 当需要登录时而用户未登录,自动运行 tcb login
autoRunLogin?: boolean
}
type CommandConstructor = new () => Command
const registrableCommands: {
Command: CommandConstructor
decoratorOptions: ICommandDecoratorOptions
}[] = []
const cmdMap = new Map()
interface ICommandDecoratorOptions {
supportPrivate: boolean | 'only'
}
const defaultCmdDecoratorOpts: ICommandDecoratorOptions = {
supportPrivate: false
}
// 装饰器收集命令
export function ICommand(
options: ICommandDecoratorOptions = defaultCmdDecoratorOpts
): ClassDecorator {
return (target: any) => {
registrableCommands.push({ Command: target, decoratorOptions: options })
}
}
// 注册命令
export async function registerCommands() {
const args = yargsParser(process.argv.slice(2))
const config = await getCloudBaseConfig(args.configFile)
const isPrivate = getPrivateSettings(config, args?._?.[0]?.toString())
registrableCommands.forEach(({ Command, decoratorOptions }) => {
if (isPrivate) {
// 私有化的
if (decoratorOptions.supportPrivate) {
const command = new Command()
command.init()
}
} else {
// 非私有化的
if (decoratorOptions.supportPrivate !== 'only') {
const command = new Command()
command.init()
}
}
})
}
// 命令基类
export abstract class Command extends EventEmitter {
on(
event: 'preHandle' | 'afterHandle',
listener: (ctx: ICommandContext, args: any[]) => void
): this
// eslint-disable-next-line
on(event: string, listener: (ctx: ICommandContext, args: any[]) => void): this {
super.on(event, listener)
return this
}
// 初始化命令
public init() {
const { cmd, childCmd, childSubCmd, deprecateCmd } = this.options
// 不能使用 new Commander 重复声明同一个命令,需要缓存 cmd 实例
let instance: Commander
// 子命令
if (cmdMap.has(cmd)) {
instance = cmdMap.get(cmd)
} else {
// 新命令或原有的旧命令格式
instance = program.command(cmd) as Commander
// @ts-expect-error 这里是用来自定义commander fallback 帮助信息
instance._helpDescription = '输出帮助信息'
instance.addHelpCommand('help [command]', '查看命令帮助信息')
cmdMap.set(cmd, instance)
}
if (childCmd) {
let cmdKey: string
let cmdName: string
let desc: string
if (typeof childCmd === 'string') {
cmdKey = `${cmd}-${childCmd}`
cmdName = childCmd
} else {
cmdKey = `${cmd}-${childCmd.cmd}`
cmdName = childCmd.cmd
desc = childCmd.desc
}
if (cmdMap.has(cmdKey)) {
instance = cmdMap.get(cmdKey)
} else {
instance = instance.command(cmdName) as Commander
// @ts-expect-error 这里是用来自定义commander fallback 帮助信息
instance._helpDescription = '查看命令帮助信息'
desc && instance.description(desc)
cmdMap.set(cmdKey, instance)
}
if (childSubCmd) {
instance = instance.command(childSubCmd) as Commander
}
}
this.createProgram(instance, false)
if (deprecateCmd) {
// 构建新的命令提示
const newCmd = [cmd, childCmd, childSubCmd]
.filter((_) => _)
.map((item) => {
if (typeof item === 'string') return item
return item.cmd
})
.join(' ')
this.createProgram(program.command(deprecateCmd) as Commander, true, newCmd)
}
}
private createProgram(instance: Commander, deprecate: boolean, newCmd?: string) {
const {
cmd,
childCmd,
desc,
options,
requiredEnvId = true,
withoutAuth = false,
autoRunLogin = false
} = this.options
instance.storeOptionsAsProperties(false)
options.forEach((option) => {
const { hideHelp } = option
if (hideHelp) {
instance.addOption(new Option(option.flags, option.desc).hideHelp())
} else {
instance.option(option.flags, option.desc)
}
})
instance.description(desc)
// tcb <cmd> params options
instance.action(async (...args) => {
// 命令的参数
const params = args.slice(0, -1)
const cmdOptions = instance.opts()
const parentOptions = program.opts()
const config = await getCloudBaseConfig(parentOptions?.configFile)
const envId = cmdOptions?.envId || config?.envId
const privateSettings = getPrivateSettings(config, cmd)
let loginState: Credential | PrivateCredential
if (privateSettings) {
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions
loginState = privateSettings.credential as PrivateCredential
} else {
loginState = (await authSupevisor.getLoginState()) as Credential
}
// 校验登陆态
if (!withoutAuth && !loginState) {
if (autoRunLogin) {
console.log(chalk.bold.yellowBright('无有效身份信息,将自动为您打开授权页面。'))
const execResult = await execWithLoading(() => login(), {
startTip: '请在浏览器中打开的授权页面进行授权...',
successTip: '授权登录成功!'
})
loginState = execResult.credential
} else {
throw new CloudBaseError('无有效身份信息,请使用 cloudbase login 登录')
}
}
if (!envId && requiredEnvId) {
throw new CloudBaseError(
'未识别到有效的环境 Id,请使用 cloudbaserc 配置文件进行操作或通过 -e 参数指定环境 Id'
)
}
// 增加上报公参
beaconAction.addAdditionalParams({
login_uin: loginState?.['uin'],
envId: envId || loginState?.['envId']
})
// 上报执行的命令信息
beaconAction.report('tcb_cli_exec_command', {
cmd,
childCmd,
desc
})
const ctx: ICommandContext = {
cmd,
envId,
config,
params,
options: cmdOptions,
hasPrivateSettings: Boolean(privateSettings)
}
// 处理前
this.emit('preHandle', ctx, args.slice(0, -1))
await this.preHandle()
// 废弃警告
if (deprecate) {
console.log(
chalk.bold.yellowBright(
'\n',
`⚠️ 此命令将被废弃,请使用新的命令 tcb ${newCmd} 代替`
),
'\n'
)
}
// 命令处理
await this.execute(ctx)
this.emit('afterHandle', ctx, args)
// 上报数据
this.afterHandle(ctx)
})
}
private async preHandle() {
const loading = loadingFactory()
try {
loading.start('数据加载中...')
const res = await getNotification()
loading.stop()
if (!res) return
const { title, content } = res
console.log(chalk.bold.cyan(title))
console.log(content, '\n')
} catch (e) {
loading.stop()
}
}
private async afterHandle(ctx) {
try {
const { cmd } = ctx
const agree = await usageStore.get('agreeCollect')
// 不同意上报、不上报使用数据
if (!agree) return
await collectUsage(cmd)
} catch (e) {
// 上报错误
}
}
// 执行命令
abstract execute(...args: any[]): void
// 获取命令参数
abstract get options(): ICommandOptions
}