Skip to content

Commit 330376c

Browse files
authored
feat(@142vip/utils): 工具增加initCommand封装,提供可配置命令初始化 (#492)
1 parent 961584f commit 330376c

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

packages/utils/src/pkgs/commander.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
import { Command } from 'commander'
22

3+
export interface VipCommanderDetailOptions {
4+
command: string
5+
aliases: string[]
6+
summary: string
7+
description: string
8+
}
9+
10+
export interface VipCommanderOptions {
11+
dryRun?: boolean
12+
trace?: boolean
13+
vip?: boolean
14+
help?: boolean
15+
}
316
/**
417
* 终端交互
518
* 参考:https://www.npmjs.com/package/commander
@@ -8,15 +21,57 @@ export class VipCommander extends Command {
821
constructor(name: string, version: string, description?: string) {
922
super(name)
1023
// 查看版本
11-
this.version(version, '-v --version', 'VipCommander Version By @142vip')
24+
this.version(version, '-v,--version', 'VipCommander Version By @142vip')
1225

1326
// 添加描述
1427
if (description != null) {
1528
this.description(description)
1629
}
1730

31+
/**
32+
* 增加默认的一些参数
33+
*/
34+
// this
35+
// .option('--dry-run', '试运行', false)
36+
// .option('--vip', '@142vip组织专用功能', false)
37+
// .option('--logger', '开启日志追踪模式', false)
38+
39+
// 对命令增加help方法
40+
// this.helpCommand(true)
41+
}
42+
43+
/**
44+
* 对命令初始化,增加aliases,summary,description等信息
45+
*/
46+
public initCommand(options: VipCommanderDetailOptions, {
47+
dryRun = true,
48+
trace = true,
49+
vip = false,
50+
help = false,
51+
}: VipCommanderOptions): VipCommander {
52+
const vipCommander = this.command(options.command, options.description)
53+
.aliases(options.aliases)
54+
.summary(options.summary)
55+
.description(options.description)
56+
57+
if (trace) {
58+
vipCommander.option('--trace', '开启日志追踪模式', false)
59+
}
60+
61+
if (dryRun) {
62+
vipCommander.option('--dry-run', '试运行', false)
63+
}
64+
65+
if (vip) {
66+
vipCommander.option('--vip', '@142vip组织专用功能', false)
67+
}
68+
1869
// 对命令增加help方法
19-
this.helpCommand(true)
70+
if (help) {
71+
vipCommander.helpCommand(true)
72+
}
73+
74+
return vipCommander
2075
}
2176

2277
// 对description、summary方法重新,增加颜色处理

0 commit comments

Comments
 (0)