Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat, support multi-spinners #7240

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat, support multi-spinners
  • Loading branch information
davidfirst committed Apr 3, 2023
commit 628cb39f9dbaedd8e22e1fb0fcd5e8e68fb9a7df
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
"detective-amd": "3.0.1",
"detective-stylus": "1.0.0",
"doctrine": "2.1.0",
"dreidels": "0.6.1",
"enhanced-resolve": "4.5.0",
"execa": "2.1.0",
"find-up": "5.0.0",
Expand Down
96 changes: 56 additions & 40 deletions src/cli/loader/loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import ora, { Ora, PersistOptions } from 'ora';
import Spinnies from 'dreidels';

import { SPINNER_TYPE } from '../../constants';

const DEFAULT_SPINNER = 'default';

/**
* previous loader was "ora" (which doesn't support multi spinners)
* some differences:
* 1) when starting a new spinner, it immediately shows a loader that can't be easily "inactive".
* 2) when calling "stop()", it doesn't only stop the loader, but also persist the previous text.
*/
export class Loader {
private spinner: Ora | null;
private spinner: Spinnies | null;

get isStarted() {
return !!this.spinner;
Expand All @@ -17,69 +25,77 @@ export class Loader {
}

off(): Loader {
this.stop();
Object.keys(this.spinner?.spinners || {}).forEach((spinner) => {
this.spinner?.spinners[spinner].remove();
});
this.spinner = null;
return this;
}

start(text?: string): Loader {
if (this.spinner) {
this.spinner.start(text);
}
return this;
/**
* @deprecated
* no need to start a spinner. just log/spin whenever needed
*/
start(text?: string, spinnerName = DEFAULT_SPINNER) {
this.spin(text || ' ', spinnerName);
}

setText(text: string): Loader {
if (this.spinner) this.spinner.text = text;
return this;
private spin(text: string, spinnerName = DEFAULT_SPINNER) {
if (!this.spinner) return;
if (this.getSpinner(spinnerName)) this.getSpinner(spinnerName)?.text(text);
else this.spinner.add(spinnerName, { text });
}

setTextAndRestart(text: string): Loader {
if (this.spinner) {
this.spinner.stop();
this.spinner.text = text;
this.spinner.start();
}
return this;
setText(text: string, spinnerName = DEFAULT_SPINNER) {
this.spin(text, spinnerName);
}

setTextAndRestart(text: string, spinnerName = DEFAULT_SPINNER) {
this.spin(text, spinnerName);
}

get(): Ora | null {
get(): Spinnies | null {
return this.spinner;
}

stop(): Loader {
if (this.spinner) this.spinner.stop();
return this;
/**
* avoid using `this.spinner?.spinners.get()` method. it throws when the spinner was removed.
* (which automatically happens when calling fail/stop/succeed/warn/info)
*/
getSpinner(spinnerName = DEFAULT_SPINNER): ReturnType<Spinnies['get']> | undefined {
return this.spinner?.spinners[spinnerName];
}

succeed(text?: string): Loader {
if (this.spinner) this.spinner.succeed(text);
return this;
/**
* avoid calling `.stop()`, it persists the last message.
*/
stop(spinnerName = DEFAULT_SPINNER) {
this.getSpinner(spinnerName)?.remove();
}

fail(text?: string): Loader {
if (this.spinner) this.spinner.fail(text);
return this;
succeed(text?: string, spinnerName = DEFAULT_SPINNER) {
this.getSpinner(spinnerName)?.succeed({ text });
}

warn(text?: string): Loader {
if (this.spinner) this.spinner.warn(text);
return this;
fail(text?: string, spinnerName = DEFAULT_SPINNER) {
this.getSpinner(spinnerName)?.fail({ text });
}

info(text?: string): Loader {
if (this.spinner) this.spinner.info(text);
return this;
warn(text?: string, spinnerName = DEFAULT_SPINNER) {
this.getSpinner(spinnerName)?.warn({ text });
}

stopAndPersist(options?: PersistOptions): Loader {
if (this.spinner) this.spinner.stopAndPersist(options);
return this;
info(text?: string, spinnerName = DEFAULT_SPINNER) {
this.getSpinner(spinnerName)?.info({ text });
}

stopAndPersist(text?: string, spinnerName = DEFAULT_SPINNER) {
this.getSpinner(spinnerName)?.static({ text });
}

private createNewSpinner(): Ora {
// for some reason the stream defaults to stderr.
return ora({ spinner: SPINNER_TYPE, text: '', stream: process.stdout, discardStdin: false, hideCursor: false });
private createNewSpinner(): Spinnies {
const spinnies = new Spinnies({ spinner: SPINNER_TYPE });
return spinnies;
}
}

Expand Down