Skip to content

Commit

Permalink
Merge pull request #21 from salesforcecli/mdonnalley/spinner
Browse files Browse the repository at this point in the history
feat: add spinner methods
  • Loading branch information
peternhale authored Jan 3, 2022
2 parents 14be413 + 441c55a commit 0b9fb37
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/sfCommand.ts
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -31,8 +32,15 @@ export abstract class SfCommand<T> 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.
Expand Down
51 changes: 51 additions & 0 deletions src/ux.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 0b9fb37

Please sign in to comment.