-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1080 from contentstack/development
Merge Development to staging
- Loading branch information
Showing
47 changed files
with
25,108 additions
and
20,559 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { Command } from '@contentstack/cli-command'; | ||
import { FlagInput, Flags, Interfaces, LoggerService } 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 logger!: LoggerService; | ||
protected args!: Args<T>; | ||
protected flags!: Flags<T>; | ||
|
||
// NOTE define flags that can be inherited by any command that extends BaseCommand | ||
static baseFlags: FlagInput = {}; | ||
|
||
/** | ||
* 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(); | ||
const { args, flags } = await this.parse({ | ||
flags: this.ctor.flags, | ||
baseFlags: (super.ctor as typeof BaseCommand).baseFlags, | ||
args: this.ctor.args, | ||
strict: this.ctor.strict, | ||
}); | ||
this.flags = flags as Flags<T>; | ||
this.args = args as Args<T>; | ||
|
||
// Init logger | ||
this.logger = new LoggerService(process.cwd(), 'cli-log'); | ||
} | ||
|
||
/** | ||
* 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(_); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.