Skip to content

Commit

Permalink
Add dynamic text for logger.success and logger.error
Browse files Browse the repository at this point in the history
  • Loading branch information
BuZZ-T committed Aug 15, 2021
1 parent 81ba2f6 commit 2f4f43a
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 21 deletions.
2 changes: 1 addition & 1 deletion constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const LOAD_CONFIG: LoggerConfig = {
export const READ_CONFIG: LoggerConfig = {
start: 'Reading local storage file from filesystem',
success: 'Successfully loaded localstore.json from filesystem',
fail: 'Error loading localstore.json from filesystem',
fail: reason => `Error loading localstore.json from filesystem: ${reason}`,
}

export const LOCAL_STORE_FILE = 'localstore.json'
Expand Down
6 changes: 4 additions & 2 deletions interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ export interface IMetadataResponse {

export interface LoggerConfig {
start: string
success: string
fail: string
success: string | TextFunction
fail: string | TextFunction
}

export type Store = {
Expand All @@ -92,3 +92,5 @@ export type Store = {
export type StoreSize = {
[p in OS]: number
}

export type TextFunction = (key: string) => string
33 changes: 21 additions & 12 deletions loggerSpinner.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as chalk from 'chalk'

import { LoggerConfig } from './interfaces'
import { LoggerConfig, TextFunction } from './interfaces'
import { isTextFunction } from './utils'

export class LoggerSpinner {

Expand All @@ -11,20 +12,20 @@ export class LoggerSpinner {
private readonly WARN_FN = (msg: string) => chalk.yellow(`! ${msg}`)

private startText: string | undefined
private successText: string | undefined
private errorText: string | undefined
private successText: string | undefined | TextFunction
private errorText: string | undefined | TextFunction
private stdio: NodeJS.WriteStream
private timer: ReturnType<typeof setTimeout> | null = null

public constructor() {
this.stdio = process.stdout
public constructor(stdio: NodeJS.WriteStream) {
this.stdio = stdio
}

private clearLine(): LoggerSpinner {
try {
this.stdio.clearLine(0)
this.stdio.cursorTo(0)
} catch {
} catch(e) {
// this might fail when piping stdout to /dev/null. Just ignore it in this case
}
return this
Expand All @@ -51,8 +52,12 @@ export class LoggerSpinner {
public start(loggingConfig: LoggerConfig): LoggerSpinner {
const { start, success, fail } = loggingConfig
this.startText = start
this.successText = this.SUCCESS_FN(success)
this.errorText = this.ERROR_FN(fail || this.DEFAULT_ERROR)
this.successText = isTextFunction(success)
? (text: string) => this.SUCCESS_FN(success(text))
: this.SUCCESS_FN(success)
this.errorText = isTextFunction(fail)
? (text: string) => this.ERROR_FN(fail(text))
: this.ERROR_FN(fail)

this.stop()
let count = 0
Expand Down Expand Up @@ -82,19 +87,23 @@ export class LoggerSpinner {
.newline()
}

public success(): LoggerSpinner {
public success(text?: string): LoggerSpinner {
return this.clearLine()
.stop()
.write(this.successText || '')
.write(isTextFunction(this.successText)
? this.successText(text || '')
: this.successText || '')
.newline()
}

public error(text?: string): LoggerSpinner {
return this.clearLine()
.stop()
.write(text ? this.ERROR_FN(text) : this.errorText || '')
.write(isTextFunction(this.errorText)
? this.errorText(text || '')
: this.errorText || '')
.newline()
}
}

export const logger = new LoggerSpinner()
export const logger = new LoggerSpinner(process.stdout)

0 comments on commit 2f4f43a

Please sign in to comment.