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 all commits
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,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
87 changes: 47 additions & 40 deletions scopes/harmony/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import loader from '@teambit/legacy/dist/cli/loader';
import loader, { DEFAULT_SPINNER } from '@teambit/legacy/dist/cli/loader/loader';
import logger, { IBitLogger } from '@teambit/legacy/dist/logger/logger';
import chalk from 'chalk';

Expand Down Expand Up @@ -44,14 +44,14 @@ export class Logger implements IBitLogger {
* single status-line on the bottom of the screen.
* the text is replaced every time this method is called.
*/
setStatusLine(text: string) {
loader.setTextAndRestart(text);
setStatusLine(text: string, spinner = DEFAULT_SPINNER) {
loader.setTextAndRestart(text, spinner);
}
/**
* remove the text from the last line on the screen.
*/
clearStatusLine() {
loader.stop();
clearStatusLine(spinner = DEFAULT_SPINNER) {
loader.stop(spinner);
}
/**
* print to the screen. if message is empty, print the last logged message.
Expand All @@ -62,43 +62,40 @@ export class Logger implements IBitLogger {
// eslint-disable-next-line no-console
console.log(message, ...meta);
} else {
loader.stopAndPersist({ text: message });
}
}

consoleWarn(message?: string, ...meta: any[]) {
if (message) this.warn(message, ...meta);
if (!loader.isStarted && logger.shouldWriteToConsole) {
// eslint-disable-next-line no-console
console.warn(message, ...meta);
} else {
loader.stopAndPersist({ text: message });
}
}

consoleError(message?: string, ...meta: any[]) {
if (message) this.error(message, ...meta);
if (!loader.isStarted && logger.shouldWriteToConsole) {
// eslint-disable-next-line no-console
console.error(message, ...meta);
} else {
loader.stopAndPersist({ text: message });
loader.stopAndPersist(message);
}
}

/**
* print to the screen as a title, with bold text.
*/
consoleTitle(message: string) {
consoleTitle(message: string, spinner = DEFAULT_SPINNER) {
this.info(message);
loader.stopAndPersist({ text: chalk.bold(message) });
loader.stopAndPersist(message, spinner);
}
/**
* print to the screen with a green `✔` prefix. if message is empty, print the last logged message.
*/
consoleSuccess(message?: string) {
consoleSuccess(message?: string, spinner = DEFAULT_SPINNER) {
if (message) this.info(message);
loader.succeed(message);
loader.succeed(message, spinner);
}
/**
* print to the screen with a red `✖` prefix. if message is empty, print the last logged message.
*/
consoleFailure(message?: string, spinner = DEFAULT_SPINNER) {
if (message) this.error(message);
loader.fail(message, spinner);
}
/**
* print to the screen with a red `⚠` prefix. if message is empty, print the last logged message.
*/
consoleWarning(message?: string, spinner = DEFAULT_SPINNER) {
if (message) {
this.warn(message);
message = chalk.yellow(message);
}
loader.warn(message, spinner);
}

/**
Expand All @@ -117,21 +114,31 @@ export class Logger implements IBitLogger {
}

/**
* print to the screen with a red `✖` prefix. if message is empty, print the last logged message.
* @deprecated
* try using consoleWarning. if not possible, rename this method to a clearer one
*/
consoleFailure(message?: string) {
if (message) this.error(message);
loader.fail(message);
consoleWarn(message?: string, ...meta: any[]) {
if (message) this.warn(message, ...meta);
if (!loader.isStarted && logger.shouldWriteToConsole) {
// eslint-disable-next-line no-console
console.warn(message, ...meta);
} else {
loader.stopAndPersist(message);
}
}

/**
* print to the screen with a red `⚠` prefix. if message is empty, print the last logged message.
* @deprecated
* try using consoleFailure. if not possible, rename this method to a clearer one
*/
consoleWarning(message?: string) {
if (message) {
this.warn(message);
message = chalk.yellow(message);
consoleError(message?: string, ...meta: any[]) {
if (message) this.error(message, ...meta);
if (!loader.isStarted && logger.shouldWriteToConsole) {
// eslint-disable-next-line no-console
console.error(message, ...meta);
} else {
loader.stopAndPersist(message);
}
loader.warn(message);
}

private colorMessage(message: string) {
Expand Down
107 changes: 67 additions & 40 deletions src/cli/loader/loader.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import ora, { Ora, PersistOptions } from 'ora';
import Spinnies from 'dreidels';
import { stdout } from 'process';

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

export 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 +26,87 @@ export class Loader {
}

off(): Loader {
this.stop();
if (!this.spinner) return this;
this.removeAll();
this.spinner = null;
return this;
}

start(text?: string): Loader {
if (this.spinner) {
this.spinner.start(text);
}
return this;
private removeAll() {
if (!this.spinner) return;
const spinners = this.spinner;
Object.keys(spinners.spinners).forEach((name) => {
spinners.spinners[name].removeAllListeners();
delete spinners.spinners[name];
});
spinners.checkIfActiveSpinners();
spinners.updateSpinnerState();
}

setText(text: string): Loader {
if (this.spinner) this.spinner.text = 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);
}

setTextAndRestart(text: string): Loader {
if (this.spinner) {
this.spinner.stop();
this.spinner.text = text;
this.spinner.start();
}
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 });
}

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, stream: stdout });
return spinnies;
}
}

Expand Down