From fbe4fbc98158e43a9e15ec557859119bd787a2c2 Mon Sep 17 00:00:00 2001 From: EternalHeart Date: Sun, 17 Mar 2024 11:48:03 +0800 Subject: [PATCH 1/2] chore: check question store before pull and init if not --- common/view/finder.view.js | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/common/view/finder.view.js b/common/view/finder.view.js index e99eac0..1bc0a65 100644 --- a/common/view/finder.view.js +++ b/common/view/finder.view.js @@ -8,7 +8,8 @@ import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuesti import { logger } from '#common/utils/logger/logger.js'; import { getQuestionListCodeBySlug, getQuestionListCodeByTag } from '#common/utils/question-handler/getQuestionListCodeBy.js'; import { getQuestionTagType } from '#common/utils/question-getter/getQuestionTagType.js'; -import { getAllQuestion } from '#common/utils/store/controller/allQuestion.js'; +import { getAllQuestion, setAllQuestion } from '#common/utils/store/controller/allQuestion.js'; +import { getAllQuestionList } from '#common/utils/question-getter/getAllQuestionList.js'; function handleQuestionList(list) { const questionList = []; @@ -126,8 +127,26 @@ async function selectMode(baseDir = process.cwd()) { }; const chooseTag = await select(tagQuestion); const allQuestion = await getAllQuestion(); - const tagQuestionList = await allQuestion.filter((question) => question.topicTags.some((topic) => topic.slug === chooseTag)); - + // 未发现题目 所以先自动拉取题目 + if (!allQuestion?.length) { + logger.info('本地数据库未初始化,自动执行初始化流程,请稍等~'); + try { + const allQuestionData = await getAllQuestionList(); + await setAllQuestion(allQuestionData); + const newData = await getAllQuestion(); + allQuestion.push(...newData); + } catch (e) { + logger.error('初始化失败!终止.'); + process.exit(0); + } finally { + logger.info('本地数据库初始化完成.'); + } + } + const tagQuestionList = allQuestion.filter((question) => question.topicTags?.some((topic) => topic.slug === chooseTag)); + if (!tagQuestionList?.length) { + logger.info('您选择的类型暂无可拉取题目~'); + process.exit(0); + } const createMode = await select({ message: '拉题模式', choices: [ @@ -135,7 +154,6 @@ async function selectMode(baseDir = process.cwd()) { { name: '全部拉取(不穩定)', value: 'all' } ] }); - if (createMode === 'single') { const singleMode = { type: 'list', From 9ac9c2b41d22270b30b7489d388082ac64713dd4 Mon Sep 17 00:00:00 2001 From: EternalHeart Date: Sun, 24 Mar 2024 10:53:00 +0800 Subject: [PATCH 2/2] feat(common): add -v command to check all info about leetcode-practice --- bin/lc.js | 1 + bin/lf.js | 1 + bin/lk.js | 1 + common/constants/question.const.js | 5 ++++- common/utils/cli-utils/commonMode.js | 6 ++++++ common/utils/etc/checkVisionInfo.js | 16 ++++++++++++++++ common/utils/logger/logger.js | 6 +++--- common/utils/update/update.js | 4 ++-- resources/text/description.js | 4 +++- resources/text/examples.js | 11 ++++++----- 10 files changed, 43 insertions(+), 12 deletions(-) create mode 100644 common/utils/etc/checkVisionInfo.js diff --git a/bin/lc.js b/bin/lc.js index d36d68d..2d44c3a 100755 --- a/bin/lc.js +++ b/bin/lc.js @@ -31,6 +31,7 @@ program .option('-d, --directory ', 'Set the question directory.') .option('-l, --language [language]', 'Set/Get the code language of question.') .option('-a, --all', 'Get all questions.') + .option('-v, --ver', 'Check the version info and some extra info about leetcode-practice.') .option('-u, --update', 'Check the version to determine whether to update to the latest one.') .parse(process.argv); diff --git a/bin/lf.js b/bin/lf.js index 4531419..051609a 100755 --- a/bin/lf.js +++ b/bin/lf.js @@ -18,6 +18,7 @@ program .option('-e, --easy', 'Use easy mode.') .option('-d, --directory ', 'Set the question directory.') .option('-l, --language [language]', 'Set/Get the code language of question.') + .option('-v, --ver', 'Check the version info and some extra info about leetcode-practice.') .option('-u, --update', 'Check the version to determine whether to update to the latest one.') .parse(process.argv); diff --git a/bin/lk.js b/bin/lk.js index e9da5c3..eae48b5 100755 --- a/bin/lk.js +++ b/bin/lk.js @@ -34,6 +34,7 @@ program .option('-e, --easy', 'Use easy mode.') .option('-d, --directory ', 'Set the question directory.') .option('-l, --language [language]', 'Set/Get the code language of question.') + .option('-v, --ver', 'Check the version info and some extra info about leetcode-practice.') .option('-u, --update', 'Check the version to determine whether to update to the latest one.') .parse(process.argv); diff --git a/common/constants/question.const.js b/common/constants/question.const.js index bcaee59..fb6bc2c 100644 --- a/common/constants/question.const.js +++ b/common/constants/question.const.js @@ -10,7 +10,10 @@ export const GITHUB_HOST = 'EternalHeartTeam'; // endregion // region 域名列表 // npm 主域名 -export const NPM_URL = 'https://registry.npmjs.org/'; +export const NPM_URL = 'https://npmjs.org/'; +// npm 仓库域名 +export const NPM_REGISTRY_URL = 'https://registry.npmjs.org/'; + // github raw 主域名 export const GITHUB_RAW = 'https://raw.githubusercontent.com/'; // github diff --git a/common/utils/cli-utils/commonMode.js b/common/utils/cli-utils/commonMode.js index 9e2de68..53144df 100644 --- a/common/utils/cli-utils/commonMode.js +++ b/common/utils/cli-utils/commonMode.js @@ -6,6 +6,7 @@ import { easyLanguageView } from '#common/view/language.view.js'; import { logger } from '#common/utils/logger/logger.js'; import { rootPath } from '#common/utils/file/getRootPath.js'; import { currentEnv } from '#common/utils/etc/checkEnv.js'; +import { checkVisionInfo } from '#common/utils/etc/checkVisionInfo.js'; /** * 执行逻辑: @@ -45,10 +46,15 @@ export async function commonMode(cmdOpts, easyCallback) { } process.exit(0); } + // 简单模式 if (cmdOpts.easy) { await easyCallback(baseDir); process.exit(0); } + if (cmdOpts.ver) { + await checkVisionInfo(); + process.exit(0); + } // 检测更新 if (cmdOpts.update) { await easyUpdateView(); diff --git a/common/utils/etc/checkVisionInfo.js b/common/utils/etc/checkVisionInfo.js new file mode 100644 index 0000000..86a3b8d --- /dev/null +++ b/common/utils/etc/checkVisionInfo.js @@ -0,0 +1,16 @@ +import { DefaultVer, GITHUB_HOST, GITHUB_URL, NPM_URL, PackageName } from '#common/constants/question.const.js'; +import { rootPath } from '#common/utils/file/getRootPath.js'; +import { logger } from '#common/utils/logger/logger.js'; +import { getQuestionLanguage } from '#common/utils/question-handler/questionLanguage.js'; +import { url_join } from '#common/utils/http/urlJoin.js'; +import { aim } from '#resources/text/aim.js'; + +/** + * 检查版本信息 加一些额外输出 + */ +export async function checkVisionInfo() { + const version = process.env.VERSION ?? DefaultVer; + const location = rootPath; + const lang = await getQuestionLanguage(); + logger.info(`version: ${version}\nlanguage: ${lang}\nlocation: file://${location}\ngithub: ${url_join(GITHUB_URL, GITHUB_HOST, PackageName)}\nnpm: ${url_join(NPM_URL, 'package', PackageName)}\n\n${aim}`); +} diff --git a/common/utils/logger/logger.js b/common/utils/logger/logger.js index a1c3484..b024c4c 100644 --- a/common/utils/logger/logger.js +++ b/common/utils/logger/logger.js @@ -34,7 +34,7 @@ class LOGGER { */ info(message, ...args) { if (this.forbidden) return; - console.log(chalk.blue(message, ...args)); + console.log(chalk.whiteBright(message, ...args)); } /** @@ -44,7 +44,7 @@ class LOGGER { */ warn(message, ...args) { if (this.forbidden) return; - console.log(chalk.yellow(message, ...args)); + console.log(chalk.yellowBright(message, ...args)); } /** @@ -54,7 +54,7 @@ class LOGGER { */ error(message, ...args) { if (this.forbidden) return; - console.log(chalk.red(message, ...args)); + console.log(chalk.redBright(message, ...args)); } } const { env = null } = (await getStore('config')) ?? {}; diff --git a/common/utils/update/update.js b/common/utils/update/update.js index ef6b16e..8f8d1de 100644 --- a/common/utils/update/update.js +++ b/common/utils/update/update.js @@ -1,12 +1,12 @@ import fs from 'node:fs'; import path from 'node:path'; import { rootPath } from '#common/utils/file/getRootPath.js'; -import { GITEE_URL, GITHUB_HOST, GITHUB_RAW, NPM_URL, PackageName } from '#common/constants/question.const.js'; +import { GITEE_URL, GITHUB_HOST, GITHUB_RAW, NPM_REGISTRY_URL, PackageName } from '#common/constants/question.const.js'; import { url_join } from '#common/utils/http/urlJoin.js'; import { fetch_ } from '#common/utils/http/fetch_.js'; // npm 中的 包地址 -const npmUrl = url_join(NPM_URL, PackageName); +const npmUrl = url_join(NPM_REGISTRY_URL, PackageName); const githubUrl = url_join(GITHUB_RAW, GITHUB_HOST, PackageName, 'master/package.json'); const giteeUrl = url_join(GITEE_URL, GITHUB_HOST, PackageName, 'raw', 'master/package.json'); diff --git a/resources/text/description.js b/resources/text/description.js index 86094f2..c83f4a4 100644 --- a/resources/text/description.js +++ b/resources/text/description.js @@ -1,5 +1,7 @@ +import { GITHUB_HOST, PackageName } from '#common/constants/question.const.js'; + export const description = ` A powerful practice platform for leetcode. CLI / Template Project / Plugin, you can create question by any way you like. -See https://github.com/wh131462/leetcode-practice +See https://github.com/${GITHUB_HOST}/${PackageName} `; diff --git a/resources/text/examples.js b/resources/text/examples.js index 30759d8..2ba8f50 100644 --- a/resources/text/examples.js +++ b/resources/text/examples.js @@ -13,7 +13,8 @@ Examples: $ lc -u // Check the version to determine whether to update to the latest one. $ lc -l // Get the code language of question. $ lc -l java // Set the code language of question. - $ lc -v // Check the CLI version. + $ lc -a // Fetch all questions from server and store them locally, prepare database for lf command. + $ lc -v // Check the version info and some extra info about leetcode-practice. $ lc -h // Check the help information. `; @@ -33,7 +34,7 @@ Examples: $ lk -l // Get the code language of question. $ lk -l java // Set the code language of question. $ lk -u // Check the version to determine whether to update to the latest one. - $ lk -v // Check the CLI version. + $ lk -v // Check the version info and some extra info about leetcode-practice. $ lk -h // Display the help information. `; @@ -41,16 +42,16 @@ Examples: export const lfExamples = ` Examples: # Command with no parameters - + nothing... # Exclusive commands - + nothing... # Other instructions $ lf -e // Easy mode to check a question. $ lf -d src // Use the relative path to the source folder. $ lf -l // Get the code language of question. $ lf -l java // Set the code language of question. $ lf -u // Check the version to determine whether to update to the latest one. - $ lf -v // Check the CLI version. + $ lf -v // Check the version info and some extra info about leetcode-practice. $ lf -h // Display the help information. `;