@@ -11,7 +11,7 @@ export type CommandRunFn = <T extends Command>(this: ICommandClass<T>, argv?: st
11
11
12
12
export interface ICommandClass < T extends Command > {
13
13
run : CommandRunFn
14
- new ( argv : string [ ] , config : Config . IConfig ) : T
14
+ new ( argv : string [ ] , opts : Config . ICommandOptions & { config : Config . IConfig } ) : T
15
15
}
16
16
17
17
const g = global as any
@@ -42,7 +42,7 @@ export default abstract class Command {
42
42
let config
43
43
if ( opts . config && Config . isIConfig ( opts . config ) ) config = opts . config
44
44
else config = await Config . read ( { root : opts . root || parentModule ! } )
45
- cmd = new this ( argv , config )
45
+ cmd = new this ( argv , { ... opts , config} )
46
46
return await cmd . run ( )
47
47
} finally {
48
48
if ( cmd ) await cmd . finally ( )
@@ -55,8 +55,7 @@ export default abstract class Command {
55
55
return convertToCached ( this , opts )
56
56
}
57
57
58
- flags ! : flags . Output
59
- args ! : args . Output
58
+ config : Config . IConfig
60
59
61
60
// prevent setting things that need to be static
62
61
description ! : null
@@ -69,15 +68,17 @@ export default abstract class Command {
69
68
70
69
protected debug : ( ...args : any [ ] ) => void
71
70
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 )
76
74
this . debug ( 'init version: %s argv: %o' , this . ctor . _base , argv )
77
75
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 ( )
81
82
}
82
83
83
84
get ctor ( ) : typeof Command {
@@ -90,7 +91,7 @@ export default abstract class Command {
90
91
* actual command run code goes here
91
92
*/
92
93
abstract async run ( ) : Promise < void >
93
-
94
+ protected init ( ) : void { }
94
95
protected async finally ( ) {
95
96
try {
96
97
await cli . done ( )
0 commit comments