diff --git a/src/sfCommand.ts b/src/sfCommand.ts index 315f81c6..c5430419 100644 --- a/src/sfCommand.ts +++ b/src/sfCommand.ts @@ -1,11 +1,12 @@ /* - * Copyright (c) 2020, salesforce.com, inc. + * Copyright (c) 2021, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ -import { Command, HelpSection, Interfaces } from '@oclif/core'; +import { Command, Config, HelpSection, Interfaces } from '@oclif/core'; import { Messages } from '@salesforce/core'; +import { Spinner } from './ux'; Messages.importMessagesDirectory(__dirname); const messages = Messages.loadMessages('@salesforce/sf-plugins-core', 'messages'); @@ -31,8 +32,15 @@ export abstract class SfCommand extends Command { public static envVariablesSection?: HelpSection; public static errorCodes?: HelpSection; + public spinner: Spinner; + private warnings: SfCommand.Warning[] = []; + public constructor(argv: string[], config: Config) { + super(argv, config); + this.spinner = new Spinner(this.jsonEnabled()); + } + /** * Log warning to users. If --json is enabled, then the warning * will be added to the json output under the warnings property. diff --git a/src/ux.ts b/src/ux.ts new file mode 100644 index 00000000..ea1ee265 --- /dev/null +++ b/src/ux.ts @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2021, salesforce.com, inc. + * All rights reserved. + * Licensed under the BSD 3-Clause license. + * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause + */ + +import { cli } from 'cli-ux'; + +/** + * This class is a light wrapper around cli.action that allows us to + * automatically suppress any actions if `--json` flag is present. + */ +export class Spinner { + public constructor(private jsonEnabled: boolean) {} + + /** + * Start a spinner on the console. + */ + public start(action: string, status?: string, opts?: { stdout?: boolean }): void { + if (!this.jsonEnabled) cli.action.start(action, status, opts); + } + + /** + * Stop the spinner on the console. + */ + public stop(msg?: string): void { + if (!this.jsonEnabled) cli.action.stop(msg); + } + + /** + * Set the status of the current spinner. + */ + public set status(status: string | undefined) { + if (!this.jsonEnabled) cli.action.status = status; + } + + /** + * Get the status of the current spinner. + */ + public get status(): string | undefined { + return cli.action.status; + } + + /** + * Pause the spinner on the console. + */ + public pause(fn: () => unknown, icon?: string): void { + if (!this.jsonEnabled) cli.action.pause(fn, icon); + } +}