Skip to content

Commit bbff9b1

Browse files
authored
feat: add loading and logger on/off (#51)
* fix: fix lf function * feat: loading function and usage * feat: logger add on/off to forbidden log
1 parent a6fdd23 commit bbff9b1

File tree

6 files changed

+73
-7
lines changed

6 files changed

+73
-7
lines changed

bin/lf.js

+1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,6 @@ const cmdArgs = program.args
2727
const cmdOpts = program.opts()
2828
// 通用参数执行
2929
const baseDir = await commonMode(cmdOpts, easyFinderView)
30+
await easyFinderView(baseDir)
3031
willUse(cmdArgs, baseDir)
3132
process.exit(0)

common/utils/cli-utils/commonMode.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export async function commonMode(cmdOpts, easyCallback) {
5252
}
5353
process.exit(0)
5454
}
55-
if (cmdOpts?.easy) {
55+
if (cmdOpts.easy) {
5656
await easyCallback(baseDir)
5757
process.exit(0)
5858
}

common/utils/http/fetch_.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { Loading } from '#common/utils/loading/loading.js'
2+
13
/**
24
* 基础请求-直接返回JSON格式的值
35
* @param url
4-
* @param options
6+
* @param options {RequestInit&{loadText?:string}}
57
* @returns {Promise<any>}
68
* @private
79
*/
810
export async function fetch_(url, options) {
9-
return await fetch(url, options).then((res) => res.json())
11+
const loader = new Loading(options.loadText ?? 'loading...')
12+
loader.start()
13+
const resp = await fetch(url, options).then((res) => res.json())
14+
loader.stop()
15+
return resp
1016
}

common/utils/loading/loading.js

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import chalk from 'chalk'
2+
3+
export class Loading {
4+
text = 'loading...'
5+
constructor(text) {
6+
this.frames = ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏']
7+
this.currentFrame = 0
8+
this.interval = null
9+
this.text = text
10+
}
11+
12+
start() {
13+
if (this.interval) clearInterval(this.interval)
14+
this.interval = setInterval(() => {
15+
process.stdout.write(
16+
chalk.yellow(`\r${this.frames[this.currentFrame]} ${this.text}`)
17+
)
18+
19+
this.currentFrame++
20+
if (this.currentFrame === this.frames.length) this.currentFrame = 0
21+
}, 80)
22+
return this
23+
}
24+
25+
stop() {
26+
clearInterval(this.interval)
27+
process.stdout.write('\r') // 清除动画最后一帧
28+
return this
29+
}
30+
31+
// 在发生异常时调用此方法清除动画
32+
handleException() {
33+
this.stop()
34+
return this
35+
}
36+
}

common/utils/logger/logger.js

+22
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,38 @@ import chalk from 'chalk'
22
import { getStore } from '#common/utils/store/controller/store.js'
33

44
class LOGGER {
5+
isOn = true
56
constructor(_env) {
67
// console.log(
78
// chalk.bgGray(`[logger init] The current env is ${env ?? 'not plugin'}.`)
89
// )
910
}
1011

12+
/**
13+
* 开启
14+
*/
15+
on() {
16+
this.isOn = true
17+
}
18+
19+
/**
20+
* 关闭
21+
*/
22+
off() {
23+
this.isOn = false
24+
}
25+
26+
get forbidden() {
27+
return !this.isOn
28+
}
29+
1130
/**
1231
* 普通消息
1332
* @param message{*}
1433
* @param args{*[]}
1534
*/
1635
info(message, ...args) {
36+
if (this.forbidden) return
1737
console.log(chalk.blue(message, ...args))
1838
}
1939

@@ -23,6 +43,7 @@ class LOGGER {
2343
* @param args{*[]}
2444
*/
2545
warn(message, ...args) {
46+
if (this.forbidden) return
2647
console.log(chalk.yellow(message, ...args))
2748
}
2849

@@ -32,6 +53,7 @@ class LOGGER {
3253
* @param args{*[]}
3354
*/
3455
error(message, ...args) {
56+
if (this.forbidden) return
3557
console.log(chalk.red(message, ...args))
3658
}
3759
}

common/view/finder.view.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ async function studyMode(baseDir = process.cwd()) {
7272
await createQuestionByTitleSlug(singleChoice, baseDir)
7373
}
7474
if (createMode === 'all') {
75-
await getQuestionListCodeBySlug(
76-
planSlug,
77-
path.resolve(baseDir, planSlug.toString())
78-
)
75+
const dir = path.resolve(baseDir, planSlug.toString())
76+
logger.off()
77+
await getQuestionListCodeBySlug(planSlug, dir)
78+
logger.on()
79+
logger.info(`题目全部拉取完成: file://${dir}`)
7980
}
8081
}
8182

0 commit comments

Comments
 (0)