Skip to content

Commit

Permalink
fix: change the config
Browse files Browse the repository at this point in the history
  • Loading branch information
wh131462 committed Mar 11, 2024
1 parent 659c47e commit 2b9bb11
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 76 deletions.
8 changes: 2 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@


## [1.0.6](https://github.com/wh131462/leetcode-practice/compare/cli-v1.0.5...cli-v1.0.6) (2024-03-11)


### Bug Fixes

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

- realm-compatible-problem ([#25](https://github.com/wh131462/leetcode-practice/issues/25)) ([d2d37d6](https://github.com/wh131462/leetcode-practice/commit/d2d37d68e6405507340b115d55bec15e070cf3ba))

### Features

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

## 1.0.5 (2024-03-11)

Expand Down
3 changes: 2 additions & 1 deletion bin/lc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
create,
createQuestionById
} from '#common/utils/create-check/createUtil.js'
import { logger } from '#common/utils/logger/logger.js'

const version = process.env.VERSION ?? DefaultVer
program
Expand Down Expand Up @@ -58,7 +59,7 @@ if (cmdOpts.language) {
await easyLanguageView(cmdOpts.language)
} else {
const lang = await getQuestionLanguage()
console.log(`当前CLI语言环境为:${lang}`)
logger.info(`当前CLI语言环境为:${lang}`)
}
process.exit(0)
}
Expand Down
2 changes: 1 addition & 1 deletion bin/lf.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if (cmdOpts.language) {
await easyLanguageView(cmdOpts.language)
} else {
const lang = await getQuestionLanguage()
console.log(`当前CLI语言环境为:${lang}`)
logger.info(`当前CLI语言环境为:${lang}`)
}
process.exit(0)
}
Expand Down
9 changes: 5 additions & 4 deletions bin/lk.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { description } from '#resources/text/description.js'
import { easyUpdateView } from '#common/view/update.view.js'
import { getQuestionFileExtension } from '#common/utils/question-handler/questionLanguage.js'
import { DefaultVer } from '#common/constants/question.const.js'
import { logger } from '#common/utils/logger/logger.js'

const version = process.env.VERSION ?? DefaultVer
program
Expand Down Expand Up @@ -63,7 +64,7 @@ if (cmdOpts.language) {
await easyLanguageView(cmdOpts.language)
} else {
const lang = await getQuestionLanguage()
console.log(`当前CLI语言环境为:${lang}`)
logger.info(`当前CLI语言环境为:${lang}`)
}
process.exit(0)
}
Expand All @@ -83,7 +84,7 @@ if (cmdOpts.update) {
// 检测函数
async function check(mode, question) {
if (!question) {
console.log('题目信息不存在,请使用lc指令进行创建~')
logger.info('题目信息不存在,请使用lc指令进行创建~')
return false
}
const filePath = path.join(
Expand All @@ -92,9 +93,9 @@ async function check(mode, question) {
`question${getQuestionFileExtension(question?.lang)}`
)
if (!fs.existsSync(filePath)) {
console.log(`文件[${filePath}]不存在,请确保已经创建!`)
logger.info(`文件[${filePath}]不存在,请确保已经创建!`)
} else {
console.log(
logger.info(
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检测结果:`
)
await checkQuestion(filePath)
Expand Down
7 changes: 4 additions & 3 deletions common/utils/create-check/createUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ import { getQuestionChineseName } from '#common/utils/question-handler/getQuesti
import { getQuestionById } from '#common/utils/question-getter/getQuestionById.js'
import { getQuestionIdBySlug } from '#common/utils/question-handler/getQuestionIdBySlug.js'
import { getLineNumberByContent } from '#common/utils/file/getLineNumberByContent.js'
import { logger } from '#common/utils/logger/logger.js'

export function create(mode, question, baseDir) {
console.log(`MODE: ${mode}`)
logger.info(`MODE: ${mode}`)
return new Promise((resolve) => {
setQuestion(mode, question)
const questionDir = path.join(baseDir, getQuestionFileName(question))
createQuestion(question, questionDir).then(async (path) => {
if (!path) path = await createQuestionCopy(question, questionDir)
const line = (await getLineNumberByContent(path, '@QUESTION_START')) + 1
console.log(
logger.info(
`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:file://${path}:${line}`
)
resolve(true)
Expand All @@ -34,6 +35,6 @@ export async function createQuestionByTitleSlug(
}
export async function createQuestionById(id, baseDir = process.cwd()) {
const question = await getQuestionById(id)
if (!question?.id) console.log(`指定编号: [ ${id} ] 题目不存在.`)
if (!question?.id) logger.warn(`指定编号: [ ${id} ] 题目不存在.`)
await create('identity', question, baseDir)
}
3 changes: 2 additions & 1 deletion common/utils/etc/open.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os from 'node:os'
import { exec } from 'node:child_process'
import { logger } from '#common/utils/logger/logger.js'

const platform = os.platform()

Expand All @@ -19,7 +20,7 @@ export function open(url) {
exec(`xdg-open "${url}"`)
break
default:
console.log(`Unsupported platform: ${platform}`)
logger.info(`Unsupported platform: ${platform}`)
break
}
}
24 changes: 24 additions & 0 deletions common/utils/logger/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getStore } from '#common/utils/store/controller/store.js'
import chalk from 'chalk'

class Logger {
constructor(env) {
console.log(
chalk.bgGray(`[logger init] The current env is ${env ?? 'not plugin'}.`)
)
}

info(message) {
console.log(chalk.blue(message))
}

warn(message) {
console.log(chalk.yellow(message))
}

error(message) {
console.log(chalk.red(message))
}
}
const { env = null } = (await getStore('config')) ?? {}
export const logger = new Logger(env)
2 changes: 1 addition & 1 deletion common/utils/question-getter/getQuestionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ import { graphql } from '#common/utils/http/graphql.js'
export async function getQuestionTypes() {
const res = await graphql(getQuestionTypesJson())
const tags = res.data?.questionTagTypeWithTags
// console.log(JSON.stringify(tags))
// logger.info(JSON.stringify(tags))
return tags
}
3 changes: 2 additions & 1 deletion common/utils/question-handler/getRandomId.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { readdirSync } from 'node:fs'
import { getQuestionListJson } from '#resources/headers/questionListJson.js'
import { graphql } from '#common/utils/http/graphql.js'
import { DefaultLimit } from '#common/constants/question.const.js'
import { logger } from '#common/utils/logger/logger.js'

/**
* 获取指定页数的ids
Expand Down Expand Up @@ -49,7 +50,7 @@ export async function getRandomId() {
length: Math.ceil(maxLength / DefaultLimit)
}).map((_, i) => i)
const one = await getOne(waitIndexList, allLocalIds)
if (one === null) console.log('恭喜!你已经刷完了所有的题目!')
if (one === null) logger.info('恭喜!你已经刷完了所有的题目!')
else return one
}
/**
Expand Down
13 changes: 7 additions & 6 deletions common/utils/update/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from '#common/constants/question.const.js'
import { url_join } from '#common/utils/http/urlJoin.js'
import { fetch_ } from '#common/utils/http/fetch_.js'
import { logger } from '#common/utils/logger/logger.js'

// npm 中的 包地址
const npmUrl = url_join(NPM_URL, PackageName)
Expand All @@ -33,10 +34,10 @@ const giteeUrl = url_join(
export async function getNpmVersion() {
try {
const res = await fetch_(npmUrl, { method: 'GET' })
console.log('获取NPM版本成功!======', res['dist-tags']?.latest)
logger.info('获取NPM版本成功!======', res['dist-tags']?.latest)
return res['dist-tags']?.latest
} catch (e) {
console.log('获取NPM版本失败!')
logger.info('获取NPM版本失败!')
throw new Error(e)
}
}
Expand All @@ -52,10 +53,10 @@ export async function getGithubVersion() {
fetch_(giteeUrl, { method: 'GET' })
])
const ver = github?.version ?? gitee?.version
console.log('获取Github版本成功!======', ver)
logger.info('获取Github版本成功!======', ver)
return ver
} catch (e) {
console.log('获取Github版本失败!', e)
logger.info('获取Github版本失败!', e)
throw new Error(e)
}
}
Expand All @@ -64,10 +65,10 @@ export function getLocalVersion() {
const { version } = JSON.parse(
fs.readFileSync(path.resolve(rootPath, 'package.json'), 'utf-8')
)
console.log('本地版本号获取成功!======', version)
logger.info('本地版本号获取成功!======', version)
return version
} catch (e) {
console.log('本地版本号获取失败!')
logger.info('本地版本号获取失败!')
return false
}
}
Expand Down
8 changes: 4 additions & 4 deletions common/utils/update/updateByEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export function updateCli() {
* @returns {Promise<void>}
*/
export async function updateProject() {
console.log('脚本升级中,请等待后续版本更新...')
logger.info('脚本升级中,请等待后续版本更新...')
return
// todo 更新项目
// 0. 询问是否存在自己修改过的内容
Expand Down Expand Up @@ -76,7 +76,7 @@ export async function updateProject() {

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

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

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

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

// 4. 完成更新
console.log('项目更新完成。')
logger.info('项目更新完成。')
} catch (error) {
console.error('更新过程中出现错误:', error)
}
Expand Down
7 changes: 4 additions & 3 deletions common/view/check.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getQuestionByMode } from '#common/utils/store/controller/question.js'
import { checkQuestion } from '#common/utils/question-handler/checkQuestion.js'
import { getCountBySameName } from '#common/utils/file/getCountBySameName.js'
import { getFileListBySameName } from '#common/utils/file/getFileListBySameName.js'
import { logger } from '#common/utils/logger/logger.js'

export async function easyCheckView() {
const modeQuestion = [
Expand Down Expand Up @@ -65,7 +66,7 @@ export async function easyCheckView() {
]
const { newDir } = await inquirer.prompt(newDirQuestion, null)
if (!newDir) {
console.log('[LK-LOG]用户终止操作~')
logger.info('[LK-LOG]用户终止操作~')
process.exit(0)
}
questionDir = path.join(
Expand All @@ -90,12 +91,12 @@ export async function easyCheckView() {
null
)
questionDir = path.join(questionParentDir, selectQuestion)
console.log(
logger.info(
`用户选择题目[ ${questionFileName}]的副本[ ${selectQuestion}]进行检测`
)
}
const filePath = path.join(questionDir, `question${question.lang}`)
await checkQuestion(filePath)
console.log(`题目[${questionFileName}]检查完成!\n文件地址为: ${filePath}`)
logger.info(`题目[${questionFileName}]检查完成!\n文件地址为: ${filePath}`)
process.exit(0)
}
7 changes: 4 additions & 3 deletions common/view/create.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getQuestionRandom } from '#common/utils/question-getter/getQuestionRand
import { createQuestion } from '#common/utils/question-handler/createQuestion.js'
import { createQuestionCopy } from '#common/utils/question-handler/createQuestionCopy.js'
import { setQuestion } from '#common/utils/store/controller/question.js'
import { logger } from '#common/utils/logger/logger.js'

export async function easyCreateView() {
const modeQuestion = [
Expand All @@ -30,7 +31,7 @@ export async function easyCreateView() {
switch (mode) {
case 'identity': {
const { identity } = await inquirer.prompt(identityQuestion, null)
console.log(identity)
logger.info(identity)
question = await getQuestionById(identity)
break
}
Expand Down Expand Up @@ -67,7 +68,7 @@ export async function easyCreateView() {
]
const { newDir } = await inquirer.prompt(newDirQuestion, null)
if (!newDir) {
console.log('[LC-LOG]用户终止操作~')
logger.info('[LC-LOG]用户终止操作~')
process.exit(0)
}
questionDir = path.join(
Expand All @@ -78,6 +79,6 @@ export async function easyCreateView() {
let filePath = await createQuestion(question, questionDir)
if (!filePath) filePath = await createQuestionCopy(question, questionDir)

console.log(`题目[${questionFileName}]创建完成!\n文件地址为: ${filePath}`)
logger.info(`题目[${questionFileName}]创建完成!\n文件地址为: ${filePath}`)
process.exit(0)
}
6 changes: 3 additions & 3 deletions common/view/finder.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { getQuestionByKeyword } from '#common/utils/question-getter/getQuestionByKeyword.js'
import { getStudyPlanList } from '#common/utils/question-getter/getStudyPlanList.js'
import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuestionList.js'
import { logger } from '#common/utils/logger/logger.js'

async function studyMode(baseDir = process.cwd()) {
const questionList = await getStudyPlanList()
Expand Down Expand Up @@ -51,7 +52,7 @@ async function studyMode(baseDir = process.cwd()) {

await createQuestionByTitleSlug(singleChoice, baseDir)
}
if (createMode === 'all') console.log('暂不支持')
if (createMode === 'all') logger.warn('暂不支持')
// await getHot100QuestionListCode()
}

Expand All @@ -71,12 +72,11 @@ async function keywordMode(baseDir = process.cwd()) {
choices: list
}
const chooseQuestion = await select(listQuestion)
console.log(chooseQuestion)
await createQuestionById(chooseQuestion, baseDir)
}

async function selectMode(baseDir) {
console.log(baseDir)
logger.info(baseDir)
}

export async function easyFinderView(baseDir = process.cwd()) {
Expand Down
3 changes: 2 additions & 1 deletion common/view/language.view.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
setQuestionLanguage
} from '#common/utils/question-handler/questionLanguage.js'
import { DefaultLang } from '#common/constants/question.const.js'
import { logger } from '#common/utils/logger/logger.js'

export async function easyLanguageView(defaultLang = DefaultLang) {
const list = LANGUAGES.map((o) => o.name)
Expand All @@ -18,7 +19,7 @@ export async function easyLanguageView(defaultLang = DefaultLang) {
}
]
const { newSet } = await inquirer.prompt(setQuestion, null)
console.log('设置语言环境为:', newSet)
logger.info('设置语言环境为:', newSet)
await setQuestionLanguage(newSet)
process.exit(0)
}
Loading

0 comments on commit 2b9bb11

Please sign in to comment.