-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathbase-command.ts
More file actions
60 lines (54 loc) · 2.37 KB
/
base-command.ts
File metadata and controls
60 lines (54 loc) · 2.37 KB
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
import { Command } from '@contentstack/cli-command';
import { ArgInput, FlagInput, Flags, Interfaces, configHandler, createLogContext } from '@contentstack/cli-utilities';
export type Args<T extends typeof Command> = Interfaces.InferredArgs<T['args']>;
export type Flags<T extends typeof Command> = Interfaces.InferredFlags<(typeof BaseCommand)['baseFlags'] & T['flags']>;
export abstract class BaseCommand<T extends typeof Command> extends Command {
public contextDetails!: {
command: string;
module: string;
userId: string;
email: string;
sessionId: string;
apiKey: string;
orgId: string;
authenticationMethod: string;
};
protected args!: Args<T>;
protected flags!: Flags<T>;
static args: ArgInput<{ [arg: string]: any }>;
/**
* The `init` function initializes the command by parsing arguments and flags, registering search
* plugins, registering the configuration, and initializing the logger.
*/
public async init(): Promise<void> {
await super.init();
// Init logger context
this.contextDetails = {
...createLogContext(this.context?.info?.command || 'config', '', configHandler.get('authenticationMethod')),
};
}
/**
* The catch function is used to handle errors from a command, either by adding custom logic or
* returning the parent class error handling.
* @param err - The `err` parameter is of type `Error & { exitCode?: number }`. This means that it is
* an object that extends the `Error` class and may also have an optional property `exitCode` of type
* `number`.
* @returns The parent class error handling is being returned.
*/
protected async catch(err: Error & { exitCode?: number }): Promise<any> {
// add any custom logic to handle errors from the command
// or simply return the parent class error handling
return super.catch(err);
}
/**
* The `finally` function is called after the `run` and `catch` functions, regardless of whether or not
* an error occurred.
* @param {Error | undefined} _ - The parameter "_" represents an error object or undefined.
* @returns The `finally` method is returning the result of calling the `finally` method of the
* superclass, which is a promise.
*/
protected async finally(_: Error | undefined): Promise<any> {
// called after run and catch regardless of whether or not the command errored
return super.finally(_);
}
}