A base command that provided common functionality for all sf commands. Functionality includes:

  • JSON support
  • progress bars
  • spinners
  • prompts
  • stylized output (JSON, url, objects, headers)
  • lifecycle events
  • configuration variables help section
  • environment variables help section
  • error codes help section

All implementations of this class need to implement the run() method.

Additionally, all implementations of this class need to provide a generic type that describes the JSON output.

See example implementation.

import { SfCommand } from '@salesforce/sf-plugins-core';
export type MyJsonOutput = { success: boolean };
export default class MyCommand extends SfCommand<MyJsonOutput> {
public async run(): Promise<MyJsonOutput> {
return { success: true };
}
}

Type Parameters

  • T

Hierarchy

  • Command
    • SfCommand

Constructors

Properties

configAggregator: ConfigAggregator

ConfigAggregator instance for accessing global and local configuration.

progress: Progress

Add a progress bar to the console. Progress

project?: SfProject
spinner: Spinner

Add a spinner to the console. Spinner

baseFlags: {
    flags-dir: OptionFlag<undefined | string, CustomOptions>;
} = ...
configurationVariablesSection?: HelpSection

Add a CONFIGURATION VARIABLES section to the help output.

import { SfCommand, toHelpSection } from '@salesforce/sf-plugins-core';
import { OrgConfigProperties } from '@salesforce/core';
export default class MyCommand extends SfCommand {
public static configurationVariablesSection = toHelpSection(
'CONFIGURATION VARIABLES',
OrgConfigProperties.TARGET_ORG,
OrgConfigProperties.ORG_API_VERSION,
);
}
enableJsonFlag: boolean = true
envVariablesSection?: HelpSection

Add an Environment VARIABLES section to the help output.

import { SfCommand, toHelpSection } from '@salesforce/sf-plugins-core';
import { EnvironmentVariable } from '@salesforce/core';
export default class MyCommand extends SfCommand {
public static envVariablesSection = toHelpSection(
'ENVIRONMENT VARIABLES',
EnvironmentVariable.SF_TARGET_ORG,
EnvironmentVariable.SF_USE_PROGRESS_BAR,
);
}
errorCodes?: HelpSection

Add an ERROR CODES section to the help output.

import { SfCommand, toHelpSection } from '@salesforce/sf-plugins-core';
export default class MyCommand extends SfCommand {
public static errorCodes = toHelpSection(
'ERROR CODES',
{ 0: 'Success', 1: 'Failure' },
);
}
requiresProject: boolean

Set to true if the command must be executed inside a Salesforce project directory.

If set to true the command will throw an error if the command is executed outside of a Salesforce project directory. Additionally, this.project will be set to the current Salesforce project (SfProject).

Methods

  • Prompt user for yes/no confirmation. Avoid calling in --json scenarios and always provide a --no-prompt option for scripting

    Parameters

    Returns Promise<boolean>

  • Determine if the command is being run with the --json flag in a command that supports it.

    Returns boolean

    true if the command supports json and the --json flag is present

  • Parameters

    • json: unknown

    Returns void

  • Warn user about sensitive information (access tokens, etc...) before logging to the console.

    Parameters

    • Optionalmsg: string

      The message to log.

    Returns void

  • Log a success message that has the standard success message color applied.

    Parameters

    • message: string

      The message to log.

    Returns void

  • actual command run code goes here

    Returns Promise<T>

  • Prompt user for yes/no confirmation. Avoid calling in --json scenarios and always provide a --no-prompt option for scripting

    Parameters

    Returns Promise<string>

  • Log stylized header to the console. Will automatically be suppressed when --json flag is present.

    Parameters

    • text: string

      the text to display as a header.

    Returns void

  • Log stylized JSON to the console. Will automatically be suppressed when --json flag is present.

    Parameters

    • obj: AnyJson

      The JSON to log.

    Returns void

  • Log stylized object to the console. Will automatically be suppressed when --json flag is present.

    Parameters

    • obj: AnyJson

      The object to log.

    Returns void

  • Display a table on the console. Will automatically be suppressed when --json flag is present.

    Type Parameters

    • R extends Record<string, unknown>

    Parameters

    • options: TableOptions<R>

    Returns void

  • Log a stylized url to the console. Will automatically be suppressed when --json flag is present.

    Parameters

    • text: string

      The text to display for the url.

    • uri: string

      The url to display.

    • params: {} = {}

      Returns void

    ""