Skip to content

Commit

Permalink
feat: add spinner methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley committed Dec 17, 2021
1 parent 14be413 commit f209f8f
Show file tree
Hide file tree
Showing 2 changed files with 47 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
37 changes: 37 additions & 0 deletions src/ux.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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 a spinner on the console.
*/
public stop(msg?: string): void {
if (!this.jsonEnabled) cli.action.stop(msg);
}

/**
* Pause a spinner on the console.
*/
public pause(fn: () => unknown, icon?: string): void {
if (!this.jsonEnabled) cli.action.pause(fn, icon);
}
}

0 comments on commit f209f8f

Please sign in to comment.