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
Prev Previous commit
Next Next commit
add the new API into logger aspect
  • Loading branch information
davidfirst committed Apr 3, 2023
commit 96b4a1fda73e6361a9298951600de968d9e83181
85 changes: 46 additions & 39 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 @@ -66,39 +66,36 @@ export class Logger implements IBitLogger {
}
}

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);
}
}

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);
}
}

/**
* print to the screen as a title, with bold text.
*/
consoleTitle(message: string) {
consoleTitle(message: string, spinner = DEFAULT_SPINNER) {
this.info(message);
loader.stopAndPersist(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
18 changes: 14 additions & 4 deletions src/cli/loader/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Spinnies from 'dreidels';

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

const DEFAULT_SPINNER = 'default';
export const DEFAULT_SPINNER = 'default';

/**
* previous loader was "ora" (which doesn't support multi spinners)
Expand All @@ -25,13 +25,23 @@ export class Loader {
}

off(): Loader {
Object.keys(this.spinner?.spinners || {}).forEach((spinner) => {
this.spinner?.spinners[spinner].remove();
});
if (!this.spinner) return this;
this.removeAll();
this.spinner = null;
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();
}

/**
* @deprecated
* no need to start a spinner. just log/spin whenever needed
Expand Down