Skip to content
This repository was archived by the owner on Aug 22, 2023. It is now read-only.

Commit 029a620

Browse files
committed
fix: add init function
1 parent 233597a commit 029a620

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

src/command.ts

+13-12
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export type CommandRunFn = <T extends Command>(this: ICommandClass<T>, argv?: st
1111

1212
export interface ICommandClass<T extends Command> {
1313
run: CommandRunFn
14-
new (argv: string[], config: Config.IConfig): T
14+
new (argv: string[], opts: Config.ICommandOptions & {config: Config.IConfig}): T
1515
}
1616

1717
const g = global as any
@@ -42,7 +42,7 @@ export default abstract class Command {
4242
let config
4343
if (opts.config && Config.isIConfig(opts.config)) config = opts.config
4444
else config = await Config.read({root: opts.root || parentModule!})
45-
cmd = new this(argv, config)
45+
cmd = new this(argv, {...opts, config})
4646
return await cmd.run()
4747
} finally {
4848
if (cmd) await cmd.finally()
@@ -55,8 +55,7 @@ export default abstract class Command {
5555
return convertToCached(this, opts)
5656
}
5757

58-
flags!: flags.Output
59-
args!: args.Output
58+
config: Config.IConfig
6059

6160
// prevent setting things that need to be static
6261
description!: null
@@ -69,15 +68,17 @@ export default abstract class Command {
6968

7069
protected debug: (...args: any[]) => void
7170

72-
constructor(public argv: string[], public config: Config.IConfig) {
73-
g['http-call'] = g['http-call'] || {}
74-
g['http-call']!.userAgent = config.userAgent
75-
this.debug = require('debug')(this.ctor.id ? `${config.bin}:${this.ctor.id}` : config.bin)
71+
constructor(public argv: string[], public opts: Config.ICommandOptions & {config: Config.IConfig}) {
72+
this.config = opts.config
73+
this.debug = require('debug')(this.ctor.id ? `${this.config.bin}:${this.ctor.id}` : this.config.bin)
7674
this.debug('init version: %s argv: %o', this.ctor._base, argv)
7775
cli.config.context.command = _.compact([this.ctor.id, ...argv]).join(' ')
78-
cli.config.context.version = config.userAgent
79-
if (config.debug) cli.config.debug = true
80-
cli.config.errlog = config.errlog
76+
cli.config.context.version = this.config.userAgent
77+
if (this.config.debug) cli.config.debug = true
78+
cli.config.errlog = this.config.errlog
79+
g['http-call'] = g['http-call'] || {}
80+
g['http-call']!.userAgent = this.config.userAgent
81+
this.init()
8182
}
8283

8384
get ctor(): typeof Command {
@@ -90,7 +91,7 @@ export default abstract class Command {
9091
* actual command run code goes here
9192
*/
9293
abstract async run(): Promise<void>
93-
94+
protected init(): void {}
9495
protected async finally() {
9596
try {
9697
await cli.done()

0 commit comments

Comments
 (0)