@@ -47,30 +47,24 @@ process.stdout.on('error', (err: any) => {
47
47
*/
48
48
49
49
export abstract class Command {
50
+ private static readonly _base = `${ pjson . name } @${ pjson . version } `
50
51
/** An array of aliases for this command. */
51
52
public static aliases : string [ ] = [ ]
52
-
53
53
/** An order-dependent object of arguments for the command */
54
54
public static args : ArgInput = { }
55
-
56
55
public static baseFlags : FlagInput
57
-
58
56
/**
59
57
* Emit deprecation warning when a command alias is used
60
58
*/
61
59
static deprecateAliases ?: boolean
62
-
63
60
public static deprecationOptions ?: Deprecation
64
-
65
61
/**
66
62
* A full description of how to use the command.
67
63
*
68
64
* If no summary, the first line of the description will be used as the summary.
69
65
*/
70
66
public static description : string | undefined
71
-
72
67
public static enableJsonFlag = false
73
-
74
68
/**
75
69
* An array of examples to show at the end of the command's help.
76
70
*
@@ -86,35 +80,24 @@ export abstract class Command {
86
80
* ```
87
81
*/
88
82
public static examples : Command . Example [ ]
89
-
90
83
/** A hash of flags for the command */
91
84
public static flags : FlagInput
92
-
93
85
public static hasDynamicHelp = false
94
-
95
86
public static help : string | undefined
96
-
97
87
/** Hide the command from help */
98
88
public static hidden : boolean
99
-
100
89
/** An array of aliases for this command that are hidden from help. */
101
90
public static hiddenAliases : string [ ] = [ ]
102
-
103
91
/** A command ID, used mostly in error or verbose reporting. */
104
92
public static id : string
105
-
106
93
public static plugin : Plugin | undefined
107
-
108
94
public static readonly pluginAlias ?: string
109
95
public static readonly pluginName ?: string
110
96
public static readonly pluginType ?: string
111
-
112
97
/** Mark the command as a given state (e.g. beta or deprecated) in help */
113
98
public static state ?: 'beta' | 'deprecated' | string
114
-
115
99
/** When set to false, allows a variable amount of arguments */
116
100
public static strict = true
117
-
118
101
/**
119
102
* The tweet-sized description for your class, used in a parent-commands
120
103
* sub-command listing and as the header for the command help.
@@ -124,13 +107,9 @@ export abstract class Command {
124
107
* An override string (or strings) for the default usage documentation.
125
108
*/
126
109
public static usage : string | string [ ] | undefined
127
-
128
110
protected debug : ( ...args : any [ ] ) => void
129
-
130
111
public id : string | undefined
131
112
132
- private static readonly _base = `${ pjson . name } @${ pjson . version } `
133
-
134
113
public constructor (
135
114
public argv : string [ ] ,
136
115
public config : Config ,
@@ -145,11 +124,6 @@ export abstract class Command {
145
124
}
146
125
}
147
126
148
- /**
149
- * actual command run code goes here
150
- */
151
- public abstract run ( ) : Promise < any >
152
-
153
127
/**
154
128
* instantiate and run the command
155
129
*
@@ -188,6 +162,26 @@ export abstract class Command {
188
162
return this . constructor as typeof Command
189
163
}
190
164
165
+ protected async _run < T > ( ) : Promise < T > {
166
+ let err : Error | undefined
167
+ let result : T | undefined
168
+ try {
169
+ // remove redirected env var to allow subsessions to run autoupdated client
170
+ this . removeEnvVar ( 'REDIRECTED' )
171
+ await this . init ( )
172
+ result = await this . run ( )
173
+ } catch ( error : any ) {
174
+ err = error
175
+ await this . catch ( error )
176
+ } finally {
177
+ await this . finally ( err )
178
+ }
179
+
180
+ if ( result && this . jsonEnabled ( ) ) this . logJson ( this . toSuccessJson ( result ) )
181
+
182
+ return result as T
183
+ }
184
+
191
185
protected async catch ( err : CommandError ) : Promise < any > {
192
186
process . exitCode = process . exitCode ?? err . exitCode ?? 1
193
187
if ( this . jsonEnabled ( ) ) {
@@ -221,7 +215,7 @@ export abstract class Command {
221
215
222
216
protected async init ( ) : Promise < any > {
223
217
this . debug ( 'init version: %s argv: %o' , this . ctor . _base , this . argv )
224
- const g : any = global
218
+ const g : any = globalThis
225
219
g [ 'http-call' ] = g [ 'http-call' ] || { }
226
220
g [ 'http-call' ] ! . userAgent = this . config . userAgent
227
221
this . warnIfCommandDeprecated ( )
@@ -243,9 +237,9 @@ export abstract class Command {
243
237
const jsonIndex = this . argv . indexOf ( '--json' )
244
238
return passThroughIndex === - 1
245
239
? // If '--' is not present, then check for `--json` in this.argv
246
- jsonIndex > - 1
240
+ jsonIndex !== - 1
247
241
: // If '--' is present, return true only the --json flag exists and is before the '--'
248
- jsonIndex > - 1 && jsonIndex < passThroughIndex
242
+ jsonIndex !== - 1 && jsonIndex < passThroughIndex
249
243
}
250
244
251
245
public log ( message = '' , ...args : any [ ] ) : void {
@@ -293,6 +287,11 @@ export abstract class Command {
293
287
return results
294
288
}
295
289
290
+ /**
291
+ * actual command run code goes here
292
+ */
293
+ public abstract run ( ) : Promise < any >
294
+
296
295
protected toErrorJson ( err : unknown ) : any {
297
296
return { error : err }
298
297
}
@@ -350,26 +349,6 @@ export abstract class Command {
350
349
}
351
350
}
352
351
353
- protected async _run < T > ( ) : Promise < T > {
354
- let err : Error | undefined
355
- let result : T | undefined
356
- try {
357
- // remove redirected env var to allow subsessions to run autoupdated client
358
- this . removeEnvVar ( 'REDIRECTED' )
359
- await this . init ( )
360
- result = await this . run ( )
361
- } catch ( error : any ) {
362
- err = error
363
- await this . catch ( error )
364
- } finally {
365
- await this . finally ( err )
366
- }
367
-
368
- if ( result && this . jsonEnabled ( ) ) this . logJson ( this . toSuccessJson ( result ) )
369
-
370
- return result as T
371
- }
372
-
373
352
private removeEnvVar ( envVar : string ) : void {
374
353
const keys : string [ ] = [ ]
375
354
try {
0 commit comments