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: add loading and logger on/off #51

Merged
merged 3 commits into from
Mar 14, 2024
Merged
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 bin/lf.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ const cmdArgs = program.args
const cmdOpts = program.opts()
// 通用参数执行
const baseDir = await commonMode(cmdOpts, easyFinderView)
await easyFinderView(baseDir)
willUse(cmdArgs, baseDir)
process.exit(0)
2 changes: 1 addition & 1 deletion common/utils/cli-utils/commonMode.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export async function commonMode(cmdOpts, easyCallback) {
}
process.exit(0)
}
if (cmdOpts?.easy) {
if (cmdOpts.easy) {
await easyCallback(baseDir)
process.exit(0)
}
Expand Down
10 changes: 8 additions & 2 deletions common/utils/http/fetch_.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { Loading } from '#common/utils/loading/loading.js'

/**
* 基础请求-直接返回JSON格式的值
* @param url
* @param options
* @param options {RequestInit&{loadText?:string}}
* @returns {Promise<any>}
* @private
*/
export async function fetch_(url, options) {
return await fetch(url, options).then((res) => res.json())
const loader = new Loading(options.loadText ?? 'loading...')
loader.start()
const resp = await fetch(url, options).then((res) => res.json())
loader.stop()
return resp
}
36 changes: 36 additions & 0 deletions common/utils/loading/loading.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import chalk from 'chalk'

export class Loading {
text = 'loading...'
constructor(text) {
this.frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
this.currentFrame = 0
this.interval = null
this.text = text
}

start() {
if (this.interval) clearInterval(this.interval)
this.interval = setInterval(() => {
process.stdout.write(
chalk.yellow(`\r${this.frames[this.currentFrame]} ${this.text}`)
)

this.currentFrame++
if (this.currentFrame === this.frames.length) this.currentFrame = 0
}, 80)
return this
}

stop() {
clearInterval(this.interval)
process.stdout.write('\r') // 清除动画最后一帧
return this
}

// 在发生异常时调用此方法清除动画
handleException() {
this.stop()
return this
}
}
22 changes: 22 additions & 0 deletions common/utils/logger/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,38 @@ import chalk from 'chalk'
import { getStore } from '#common/utils/store/controller/store.js'

class LOGGER {
isOn = true
constructor(_env) {
// console.log(
// chalk.bgGray(`[logger init] The current env is ${env ?? 'not plugin'}.`)
// )
}

/**
* 开启
*/
on() {
this.isOn = true
}

/**
* 关闭
*/
off() {
this.isOn = false
}

get forbidden() {
return !this.isOn
}

/**
* 普通消息
* @param message{*}
* @param args{*[]}
*/
info(message, ...args) {
if (this.forbidden) return
console.log(chalk.blue(message, ...args))
}

Expand All @@ -23,6 +43,7 @@ class LOGGER {
* @param args{*[]}
*/
warn(message, ...args) {
if (this.forbidden) return
console.log(chalk.yellow(message, ...args))
}

Expand All @@ -32,6 +53,7 @@ class LOGGER {
* @param args{*[]}
*/
error(message, ...args) {
if (this.forbidden) return
console.log(chalk.red(message, ...args))
}
}
Expand Down
9 changes: 5 additions & 4 deletions common/view/finder.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ async function studyMode(baseDir = process.cwd()) {
await createQuestionByTitleSlug(singleChoice, baseDir)
}
if (createMode === 'all') {
await getQuestionListCodeBySlug(
planSlug,
path.resolve(baseDir, planSlug.toString())
)
const dir = path.resolve(baseDir, planSlug.toString())
logger.off()
await getQuestionListCodeBySlug(planSlug, dir)
logger.on()
logger.info(`题目全部拉取完成: file://${dir}`)
}
}

Expand Down
Loading