Skip to content

Commit 2b9bb11

Browse files
committed
fix: change the config
1 parent 659c47e commit 2b9bb11

18 files changed

+86
-76
lines changed

CHANGELOG.md

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
2-
31
## [1.0.6](https://github.com/wh131462/leetcode-practice/compare/cli-v1.0.5...cli-v1.0.6) (2024-03-11)
42

5-
63
### Bug Fixes
74

8-
* realm-compatible-problem ([#25](https://github.com/wh131462/leetcode-practice/issues/25)) ([d2d37d6](https://github.com/wh131462/leetcode-practice/commit/d2d37d68e6405507340b115d55bec15e070cf3ba))
9-
5+
- realm-compatible-problem ([#25](https://github.com/wh131462/leetcode-practice/issues/25)) ([d2d37d6](https://github.com/wh131462/leetcode-practice/commit/d2d37d68e6405507340b115d55bec15e070cf3ba))
106

117
### Features
128

13-
* change lf feature and update docs ([#27](https://github.com/wh131462/leetcode-practice/issues/27)) ([3b7e129](https://github.com/wh131462/leetcode-practice/commit/3b7e129b722391b50d32e48e782f244a12faad05))
9+
- change lf feature and update docs ([#27](https://github.com/wh131462/leetcode-practice/issues/27)) ([3b7e129](https://github.com/wh131462/leetcode-practice/commit/3b7e129b722391b50d32e48e782f244a12faad05))
1410

1511
## 1.0.5 (2024-03-11)
1612

bin/lc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
create,
2222
createQuestionById
2323
} from '#common/utils/create-check/createUtil.js'
24+
import { logger } from '#common/utils/logger/logger.js'
2425

2526
const version = process.env.VERSION ?? DefaultVer
2627
program
@@ -58,7 +59,7 @@ if (cmdOpts.language) {
5859
await easyLanguageView(cmdOpts.language)
5960
} else {
6061
const lang = await getQuestionLanguage()
61-
console.log(`当前CLI语言环境为:${lang}`)
62+
logger.info(`当前CLI语言环境为:${lang}`)
6263
}
6364
process.exit(0)
6465
}

bin/lf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if (cmdOpts.language) {
3838
await easyLanguageView(cmdOpts.language)
3939
} else {
4040
const lang = await getQuestionLanguage()
41-
console.log(`当前CLI语言环境为:${lang}`)
41+
logger.info(`当前CLI语言环境为:${lang}`)
4242
}
4343
process.exit(0)
4444
}

bin/lk.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { description } from '#resources/text/description.js'
1818
import { easyUpdateView } from '#common/view/update.view.js'
1919
import { getQuestionFileExtension } from '#common/utils/question-handler/questionLanguage.js'
2020
import { DefaultVer } from '#common/constants/question.const.js'
21+
import { logger } from '#common/utils/logger/logger.js'
2122

2223
const version = process.env.VERSION ?? DefaultVer
2324
program
@@ -63,7 +64,7 @@ if (cmdOpts.language) {
6364
await easyLanguageView(cmdOpts.language)
6465
} else {
6566
const lang = await getQuestionLanguage()
66-
console.log(`当前CLI语言环境为:${lang}`)
67+
logger.info(`当前CLI语言环境为:${lang}`)
6768
}
6869
process.exit(0)
6970
}
@@ -83,7 +84,7 @@ if (cmdOpts.update) {
8384
// 检测函数
8485
async function check(mode, question) {
8586
if (!question) {
86-
console.log('题目信息不存在,请使用lc指令进行创建~')
87+
logger.info('题目信息不存在,请使用lc指令进行创建~')
8788
return false
8889
}
8990
const filePath = path.join(
@@ -92,9 +93,9 @@ async function check(mode, question) {
9293
`question${getQuestionFileExtension(question?.lang)}`
9394
)
9495
if (!fs.existsSync(filePath)) {
95-
console.log(`文件[${filePath}]不存在,请确保已经创建!`)
96+
logger.info(`文件[${filePath}]不存在,请确保已经创建!`)
9697
} else {
97-
console.log(
98+
logger.info(
9899
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检测结果:`
99100
)
100101
await checkQuestion(filePath)

common/utils/create-check/createUtil.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ import { getQuestionChineseName } from '#common/utils/question-handler/getQuesti
77
import { getQuestionById } from '#common/utils/question-getter/getQuestionById.js'
88
import { getQuestionIdBySlug } from '#common/utils/question-handler/getQuestionIdBySlug.js'
99
import { getLineNumberByContent } from '#common/utils/file/getLineNumberByContent.js'
10+
import { logger } from '#common/utils/logger/logger.js'
1011

1112
export function create(mode, question, baseDir) {
12-
console.log(`MODE: ${mode}`)
13+
logger.info(`MODE: ${mode}`)
1314
return new Promise((resolve) => {
1415
setQuestion(mode, question)
1516
const questionDir = path.join(baseDir, getQuestionFileName(question))
1617
createQuestion(question, questionDir).then(async (path) => {
1718
if (!path) path = await createQuestionCopy(question, questionDir)
1819
const line = (await getLineNumberByContent(path, '@QUESTION_START')) + 1
19-
console.log(
20+
logger.info(
2021
`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:file://${path}:${line}`
2122
)
2223
resolve(true)
@@ -34,6 +35,6 @@ export async function createQuestionByTitleSlug(
3435
}
3536
export async function createQuestionById(id, baseDir = process.cwd()) {
3637
const question = await getQuestionById(id)
37-
if (!question?.id) console.log(`指定编号: [ ${id} ] 题目不存在.`)
38+
if (!question?.id) logger.warn(`指定编号: [ ${id} ] 题目不存在.`)
3839
await create('identity', question, baseDir)
3940
}

common/utils/etc/open.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os from 'node:os'
22
import { exec } from 'node:child_process'
3+
import { logger } from '#common/utils/logger/logger.js'
34

45
const platform = os.platform()
56

@@ -19,7 +20,7 @@ export function open(url) {
1920
exec(`xdg-open "${url}"`)
2021
break
2122
default:
22-
console.log(`Unsupported platform: ${platform}`)
23+
logger.info(`Unsupported platform: ${platform}`)
2324
break
2425
}
2526
}

common/utils/logger/logger.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { getStore } from '#common/utils/store/controller/store.js'
2+
import chalk from 'chalk'
3+
4+
class Logger {
5+
constructor(env) {
6+
console.log(
7+
chalk.bgGray(`[logger init] The current env is ${env ?? 'not plugin'}.`)
8+
)
9+
}
10+
11+
info(message) {
12+
console.log(chalk.blue(message))
13+
}
14+
15+
warn(message) {
16+
console.log(chalk.yellow(message))
17+
}
18+
19+
error(message) {
20+
console.log(chalk.red(message))
21+
}
22+
}
23+
const { env = null } = (await getStore('config')) ?? {}
24+
export const logger = new Logger(env)

common/utils/question-getter/getQuestionTypes.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ import { graphql } from '#common/utils/http/graphql.js'
2525
export async function getQuestionTypes() {
2626
const res = await graphql(getQuestionTypesJson())
2727
const tags = res.data?.questionTagTypeWithTags
28-
// console.log(JSON.stringify(tags))
28+
// logger.info(JSON.stringify(tags))
2929
return tags
3030
}

common/utils/question-handler/getRandomId.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { readdirSync } from 'node:fs'
22
import { getQuestionListJson } from '#resources/headers/questionListJson.js'
33
import { graphql } from '#common/utils/http/graphql.js'
44
import { DefaultLimit } from '#common/constants/question.const.js'
5+
import { logger } from '#common/utils/logger/logger.js'
56

67
/**
78
* 获取指定页数的ids
@@ -49,7 +50,7 @@ export async function getRandomId() {
4950
length: Math.ceil(maxLength / DefaultLimit)
5051
}).map((_, i) => i)
5152
const one = await getOne(waitIndexList, allLocalIds)
52-
if (one === null) console.log('恭喜!你已经刷完了所有的题目!')
53+
if (one === null) logger.info('恭喜!你已经刷完了所有的题目!')
5354
else return one
5455
}
5556
/**

common/utils/update/update.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from '#common/constants/question.const.js'
1111
import { url_join } from '#common/utils/http/urlJoin.js'
1212
import { fetch_ } from '#common/utils/http/fetch_.js'
13+
import { logger } from '#common/utils/logger/logger.js'
1314

1415
// npm 中的 包地址
1516
const npmUrl = url_join(NPM_URL, PackageName)
@@ -33,10 +34,10 @@ const giteeUrl = url_join(
3334
export async function getNpmVersion() {
3435
try {
3536
const res = await fetch_(npmUrl, { method: 'GET' })
36-
console.log('获取NPM版本成功!======', res['dist-tags']?.latest)
37+
logger.info('获取NPM版本成功!======', res['dist-tags']?.latest)
3738
return res['dist-tags']?.latest
3839
} catch (e) {
39-
console.log('获取NPM版本失败!')
40+
logger.info('获取NPM版本失败!')
4041
throw new Error(e)
4142
}
4243
}
@@ -52,10 +53,10 @@ export async function getGithubVersion() {
5253
fetch_(giteeUrl, { method: 'GET' })
5354
])
5455
const ver = github?.version ?? gitee?.version
55-
console.log('获取Github版本成功!======', ver)
56+
logger.info('获取Github版本成功!======', ver)
5657
return ver
5758
} catch (e) {
58-
console.log('获取Github版本失败!', e)
59+
logger.info('获取Github版本失败!', e)
5960
throw new Error(e)
6061
}
6162
}
@@ -64,10 +65,10 @@ export function getLocalVersion() {
6465
const { version } = JSON.parse(
6566
fs.readFileSync(path.resolve(rootPath, 'package.json'), 'utf-8')
6667
)
67-
console.log('本地版本号获取成功!======', version)
68+
logger.info('本地版本号获取成功!======', version)
6869
return version
6970
} catch (e) {
70-
console.log('本地版本号获取失败!')
71+
logger.info('本地版本号获取失败!')
7172
return false
7273
}
7374
}

common/utils/update/updateByEnv.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function updateCli() {
3737
* @returns {Promise<void>}
3838
*/
3939
export async function updateProject() {
40-
console.log('脚本升级中,请等待后续版本更新...')
40+
logger.info('脚本升级中,请等待后续版本更新...')
4141
return
4242
// todo 更新项目
4343
// 0. 询问是否存在自己修改过的内容
@@ -76,7 +76,7 @@ export async function updateProject() {
7676

7777
// 0.1 如果有过更改,终止更新脚本,并给出提示建议
7878
if (hasChanges) {
79-
console.log('您有未提交的更改,请先处理后再更新。')
79+
logger.info('您有未提交的更改,请先处理后再更新。')
8080
return
8181
}
8282

@@ -85,7 +85,7 @@ export async function updateProject() {
8585

8686
// 1.1 如果有内容,提示提交,终止更新
8787
if (gitStatus) {
88-
console.log('您有未提交的更改,请先提交后再更新。')
88+
logger.info('您有未提交的更改,请先提交后再更新。')
8989
return
9090
}
9191

@@ -113,7 +113,7 @@ export async function updateProject() {
113113
await execCommand(`git commit -m "update:${ltsVersion}"`) // 这里替换为实际的最新版本号
114114

115115
// 4. 完成更新
116-
console.log('项目更新完成。')
116+
logger.info('项目更新完成。')
117117
} catch (error) {
118118
console.error('更新过程中出现错误:', error)
119119
}

common/view/check.view.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { getQuestionByMode } from '#common/utils/store/controller/question.js'
66
import { checkQuestion } from '#common/utils/question-handler/checkQuestion.js'
77
import { getCountBySameName } from '#common/utils/file/getCountBySameName.js'
88
import { getFileListBySameName } from '#common/utils/file/getFileListBySameName.js'
9+
import { logger } from '#common/utils/logger/logger.js'
910

1011
export async function easyCheckView() {
1112
const modeQuestion = [
@@ -65,7 +66,7 @@ export async function easyCheckView() {
6566
]
6667
const { newDir } = await inquirer.prompt(newDirQuestion, null)
6768
if (!newDir) {
68-
console.log('[LK-LOG]用户终止操作~')
69+
logger.info('[LK-LOG]用户终止操作~')
6970
process.exit(0)
7071
}
7172
questionDir = path.join(
@@ -90,12 +91,12 @@ export async function easyCheckView() {
9091
null
9192
)
9293
questionDir = path.join(questionParentDir, selectQuestion)
93-
console.log(
94+
logger.info(
9495
`用户选择题目[ ${questionFileName}]的副本[ ${selectQuestion}]进行检测`
9596
)
9697
}
9798
const filePath = path.join(questionDir, `question${question.lang}`)
9899
await checkQuestion(filePath)
99-
console.log(`题目[${questionFileName}]检查完成!\n文件地址为: ${filePath}`)
100+
logger.info(`题目[${questionFileName}]检查完成!\n文件地址为: ${filePath}`)
100101
process.exit(0)
101102
}

common/view/create.view.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { getQuestionRandom } from '#common/utils/question-getter/getQuestionRand
77
import { createQuestion } from '#common/utils/question-handler/createQuestion.js'
88
import { createQuestionCopy } from '#common/utils/question-handler/createQuestionCopy.js'
99
import { setQuestion } from '#common/utils/store/controller/question.js'
10+
import { logger } from '#common/utils/logger/logger.js'
1011

1112
export async function easyCreateView() {
1213
const modeQuestion = [
@@ -30,7 +31,7 @@ export async function easyCreateView() {
3031
switch (mode) {
3132
case 'identity': {
3233
const { identity } = await inquirer.prompt(identityQuestion, null)
33-
console.log(identity)
34+
logger.info(identity)
3435
question = await getQuestionById(identity)
3536
break
3637
}
@@ -67,7 +68,7 @@ export async function easyCreateView() {
6768
]
6869
const { newDir } = await inquirer.prompt(newDirQuestion, null)
6970
if (!newDir) {
70-
console.log('[LC-LOG]用户终止操作~')
71+
logger.info('[LC-LOG]用户终止操作~')
7172
process.exit(0)
7273
}
7374
questionDir = path.join(
@@ -78,6 +79,6 @@ export async function easyCreateView() {
7879
let filePath = await createQuestion(question, questionDir)
7980
if (!filePath) filePath = await createQuestionCopy(question, questionDir)
8081

81-
console.log(`题目[${questionFileName}]创建完成!\n文件地址为: ${filePath}`)
82+
logger.info(`题目[${questionFileName}]创建完成!\n文件地址为: ${filePath}`)
8283
process.exit(0)
8384
}

common/view/finder.view.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
import { getQuestionByKeyword } from '#common/utils/question-getter/getQuestionByKeyword.js'
1010
import { getStudyPlanList } from '#common/utils/question-getter/getStudyPlanList.js'
1111
import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuestionList.js'
12+
import { logger } from '#common/utils/logger/logger.js'
1213

1314
async function studyMode(baseDir = process.cwd()) {
1415
const questionList = await getStudyPlanList()
@@ -51,7 +52,7 @@ async function studyMode(baseDir = process.cwd()) {
5152

5253
await createQuestionByTitleSlug(singleChoice, baseDir)
5354
}
54-
if (createMode === 'all') console.log('暂不支持')
55+
if (createMode === 'all') logger.warn('暂不支持')
5556
// await getHot100QuestionListCode()
5657
}
5758

@@ -71,12 +72,11 @@ async function keywordMode(baseDir = process.cwd()) {
7172
choices: list
7273
}
7374
const chooseQuestion = await select(listQuestion)
74-
console.log(chooseQuestion)
7575
await createQuestionById(chooseQuestion, baseDir)
7676
}
7777

7878
async function selectMode(baseDir) {
79-
console.log(baseDir)
79+
logger.info(baseDir)
8080
}
8181

8282
export async function easyFinderView(baseDir = process.cwd()) {

common/view/language.view.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
setQuestionLanguage
55
} from '#common/utils/question-handler/questionLanguage.js'
66
import { DefaultLang } from '#common/constants/question.const.js'
7+
import { logger } from '#common/utils/logger/logger.js'
78

89
export async function easyLanguageView(defaultLang = DefaultLang) {
910
const list = LANGUAGES.map((o) => o.name)
@@ -18,7 +19,7 @@ export async function easyLanguageView(defaultLang = DefaultLang) {
1819
}
1920
]
2021
const { newSet } = await inquirer.prompt(setQuestion, null)
21-
console.log('设置语言环境为:', newSet)
22+
logger.info('设置语言环境为:', newSet)
2223
await setQuestionLanguage(newSet)
2324
process.exit(0)
2425
}

0 commit comments

Comments
 (0)