diff --git a/CHANGELOG.md b/CHANGELOG.md index 0ba83c9..69e6784 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/bin/lc.js b/bin/lc.js index 0f8567e..bc79697 100755 --- a/bin/lc.js +++ b/bin/lc.js @@ -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 @@ -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) } diff --git a/bin/lf.js b/bin/lf.js index 4828527..2c7a58e 100755 --- a/bin/lf.js +++ b/bin/lf.js @@ -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) } diff --git a/bin/lk.js b/bin/lk.js index 8df5aa6..931aac6 100755 --- a/bin/lk.js +++ b/bin/lk.js @@ -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 @@ -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) } @@ -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( @@ -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) diff --git a/common/utils/create-check/createUtil.js b/common/utils/create-check/createUtil.js index 3322dd6..bfb570e 100644 --- a/common/utils/create-check/createUtil.js +++ b/common/utils/create-check/createUtil.js @@ -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) @@ -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) } diff --git a/common/utils/etc/open.js b/common/utils/etc/open.js index 8c0e991..1289461 100644 --- a/common/utils/etc/open.js +++ b/common/utils/etc/open.js @@ -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() @@ -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 } } diff --git a/common/utils/logger/logger.js b/common/utils/logger/logger.js new file mode 100644 index 0000000..1031b29 --- /dev/null +++ b/common/utils/logger/logger.js @@ -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) diff --git a/common/utils/question-getter/getQuestionTypes.js b/common/utils/question-getter/getQuestionTypes.js index 8db5e3a..74e809d 100644 --- a/common/utils/question-getter/getQuestionTypes.js +++ b/common/utils/question-getter/getQuestionTypes.js @@ -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 } diff --git a/common/utils/question-handler/getRandomId.js b/common/utils/question-handler/getRandomId.js index 0c6b3f1..72a615b 100644 --- a/common/utils/question-handler/getRandomId.js +++ b/common/utils/question-handler/getRandomId.js @@ -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 @@ -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 } /** diff --git a/common/utils/update/update.js b/common/utils/update/update.js index bef2cb4..a91657d 100644 --- a/common/utils/update/update.js +++ b/common/utils/update/update.js @@ -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) @@ -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) } } @@ -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) } } @@ -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 } } diff --git a/common/utils/update/updateByEnv.js b/common/utils/update/updateByEnv.js index b2409e4..741abdf 100644 --- a/common/utils/update/updateByEnv.js +++ b/common/utils/update/updateByEnv.js @@ -37,7 +37,7 @@ export function updateCli() { * @returns {Promise} */ export async function updateProject() { - console.log('脚本升级中,请等待后续版本更新...') + logger.info('脚本升级中,请等待后续版本更新...') return // todo 更新项目 // 0. 询问是否存在自己修改过的内容 @@ -76,7 +76,7 @@ export async function updateProject() { // 0.1 如果有过更改,终止更新脚本,并给出提示建议 if (hasChanges) { - console.log('您有未提交的更改,请先处理后再更新。') + logger.info('您有未提交的更改,请先处理后再更新。') return } @@ -85,7 +85,7 @@ export async function updateProject() { // 1.1 如果有内容,提示提交,终止更新 if (gitStatus) { - console.log('您有未提交的更改,请先提交后再更新。') + logger.info('您有未提交的更改,请先提交后再更新。') return } @@ -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) } diff --git a/common/view/check.view.js b/common/view/check.view.js index 8172651..4894a37 100644 --- a/common/view/check.view.js +++ b/common/view/check.view.js @@ -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 = [ @@ -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( @@ -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) } diff --git a/common/view/create.view.js b/common/view/create.view.js index ecdb06c..332c9e7 100644 --- a/common/view/create.view.js +++ b/common/view/create.view.js @@ -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 = [ @@ -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 } @@ -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( @@ -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) } diff --git a/common/view/finder.view.js b/common/view/finder.view.js index c10ef78..4452e11 100644 --- a/common/view/finder.view.js +++ b/common/view/finder.view.js @@ -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() @@ -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() } @@ -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()) { diff --git a/common/view/language.view.js b/common/view/language.view.js index 2828f1f..75d461c 100644 --- a/common/view/language.view.js +++ b/common/view/language.view.js @@ -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) @@ -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) } diff --git a/common/view/update.view.js b/common/view/update.view.js index 65cde1e..c60431c 100644 --- a/common/view/update.view.js +++ b/common/view/update.view.js @@ -2,6 +2,7 @@ import inquirer from 'inquirer' import { checkUpdate } from '#common/utils/update/update.js' import { currentEnv } from '#common/utils/etc/checkEnv.js' import { updateByEnv } from '#common/utils/update/updateByEnv.js' +import { logger } from '#common/utils/logger/logger.js' export async function easyUpdateView() { // 1. 询问当前的环境是啥 (自动检测一次) @@ -23,7 +24,7 @@ export async function easyUpdateView() { isCliUpdate, isGithubUpdate } = await checkUpdate() - console.log( + logger.info( `当前版本:[ ${localVersion} ] npm包最新版本:[ ${npmVersion} ] github版本:[ ${githubVersion} ]` ) let isUpdate = false @@ -38,7 +39,7 @@ export async function easyUpdateView() { version = npmVersion break default: - console.log('未知环境:', choseEnv) + logger.warn('未知环境:', choseEnv) process.exit(0) break } @@ -52,18 +53,18 @@ export async function easyUpdateView() { const { willUpdate } = await inquirer.prompt(checkQuestion, null) if (willUpdate) { // 4.1 选择更新 - console.log('开始更新...') + logger.info('开始更新...') await updateByEnv(env) - console.log('更新完成~祝你使用愉快~') + logger.info('更新完成~祝你使用愉快~') } else { // 4.2 取消更新 - console.log( + logger.info( '你选择跳过此次更新,如果想要进行更新,随时可以使用参数 -u 进行更新检测!祝你使用愉快~' ) } process.exit(0) } else { - console.log('当前已是最新版本!祝你使用愉快~') + logger.info('当前已是最新版本!祝你使用愉快~') process.exit(0) } } diff --git a/esbuild.config.js b/esbuild.config.js index 6074108..1221493 100644 --- a/esbuild.config.js +++ b/esbuild.config.js @@ -2,6 +2,7 @@ import path from 'node:path' import fs, { mkdirSync } from 'node:fs' import esbuild from 'esbuild' import { rootPath } from '#common/utils/file/getRootPath.js' +import { logger } from '#common/utils/logger/logger.js' // 读取 package.json 文件内容 const packageJson = JSON.parse( @@ -43,38 +44,18 @@ function clean() { }) } -/** - * 创建入口文件 暴露三个接口 - */ -function createIndex() { - const dirs = buildBinConfig - const expTem = `export * from ` - - const content = Object.values(dirs).reduce((p, file) => { - return `${p + expTem}'${file}'\n` - }, '') - const filePath = 'pl-cli/index.js' - fs.writeFileSync(filePath, content) -} - /** * 复制文档 */ function copyDocs() { - // 复制文件 - const docs = fs.readdirSync(path.resolve(rootPath, 'docs')) // 创建docs - mkdirSync(path.resolve(rootPath, 'pl-cli/docs'), { recursive: true }) + const docs = ['README.md', 'README_CN.md', 'README_JP.md'] docs.forEach((doc) => { fs.copyFileSync( - path.resolve(rootPath, 'docs', doc), - path.resolve(rootPath, 'pl-cli/docs/', doc) + path.resolve(rootPath, doc), + path.resolve(rootPath, `pl-cli/${doc}`) ) }) - fs.copyFileSync( - path.resolve(rootPath, 'README.md'), - path.resolve(rootPath, 'pl-cli/README.md') - ) fs.copyFileSync( path.resolve(rootPath, 'LICENSE'), path.resolve(rootPath, 'pl-cli/LICENSE') @@ -91,7 +72,6 @@ function rewritePackageFile() { publishExcludeFields?.forEach((key) => { delete newPackageJson[key] }) - Object.assign(newPackageJson, { main: 'index.js' }) fs.writeFileSync( path.resolve(rootPath, 'pl-cli/package.json'), JSON.stringify(newPackageJson) @@ -104,7 +84,6 @@ function rewritePackageFile() { function afterBuild() { copyDocs() rewritePackageFile() - createIndex() } /** @@ -117,10 +96,10 @@ async function main() { .then(() => { // 构建完成后执行的操作 afterBuild() - console.log('[LP]脚本打包完成,查看目录[pl-cli].') + logger.info('[LP]脚本打包完成,请查看目录[ pl-cli ].') }) .catch((e) => { - console.error('[LP]脚本打包失败', e) + logger.error('[LP]脚本打包失败', e) process.exit(1) }) } diff --git a/scripts/update.js b/scripts/update.js index ba74ec6..5a0ae9a 100644 --- a/scripts/update.js +++ b/scripts/update.js @@ -2,9 +2,10 @@ import inquirer from 'inquirer' import { checkUpdate } from '#common/utils/update/update.js' import { updateProject } from '#common/utils/update/updateByEnv.js' +import { logger } from '#common/utils/logger/logger.js' const { isGithubUpdate, githubVersion, localVersion } = await checkUpdate() -console.log(`当前版本: [${localVersion}] github版本: [${githubVersion}] `) +logger.info(`当前版本: [${localVersion}] github版本: [${githubVersion}] `) if (isGithubUpdate) { const checkQuestion = { type: 'confirm', @@ -14,17 +15,17 @@ if (isGithubUpdate) { const { willUpdate } = await inquirer.prompt(checkQuestion, null) if (willUpdate) { // 4.1 选择更新 - console.log('开始更新...') + logger.info('开始更新...') await updateProject() - console.log('更新完成~祝你使用愉快~') + logger.info('更新完成~祝你使用愉快~') } else { // 4.2 取消更新 - console.log( + logger.info( '你选择跳过此次更新,如果想要进行更新,随时可以使用参数 -u 进行更新检测!祝你使用愉快~' ) } process.exit(0) } else { - console.log('当前版本为最新版本~无需更新!') + logger.info('当前版本为最新版本~无需更新!') } process.exit(0)