Skip to content

Commit

Permalink
feat(helper): add logger utils (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope authored Jan 29, 2024
1 parent fadf122 commit a92c710
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions tools/helper/src/node/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './logger.js'
export * from './packageManager.js'
76 changes: 76 additions & 0 deletions tools/helper/src/node/utils/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { colors, ora } from 'vuepress/utils'

type Ora = ReturnType<typeof ora>

/**
* Logger utils
*/
export class Logger {
constructor(
/**
* Plugin/Theme name
*/
private name = '',
) {}

private init(text: string): Ora {
return ora({
prefixText: colors.blue(`${this.name}: `) || '',
text,
})
}

/**
* Create a loading spinner with text
*/
load(text: string): {
succeed: (text?: string) => void
fail: (text?: string) => void
} {
const instance = this.init(text)

return {
succeed: (text?: string) => instance.succeed(text),
fail: (text?: string) => instance.succeed(text),
}
}

/**
* Log info msg
*
* @param text Hint text
* @returns Ora Instance
*/
info(text = '', ...args: any[]): void {
this.init(colors.blue(text)).info()

if (args.length) console.info(...args)
}

/**
* Log success msg
*/
succeed(text = '', ...args: any[]): void {
this.init(colors.green(text)).succeed()

if (args.length) console.log(...args)
}

/**
* Log warning msg
*/
warn(text = '', ...args: any[]): void {
this.init(colors.yellow(text)).warn()

if (args.length) console.warn(...args)
}

/**
* Log error msg
*/
error(text = '', ...args: any[]): void {
this.init(colors.red(text)).fail()

if (args.length) console.error(...args)
}
}

0 comments on commit a92c710

Please sign in to comment.