From 57d68cd90f3e220bb859979b04a9c491f2f0204e Mon Sep 17 00:00:00 2001 From: SmallTeddy Date: Sat, 16 Mar 2024 16:40:11 +0800 Subject: [PATCH 1/2] feat: add lf select mode --- .../question-handler/getQuestionListCodeBy.js | 41 +++++++++++++++++++ .../getQuestionListCodeBySlug.js | 24 ----------- common/utils/store/controller/allQuestion.js | 2 +- common/view/finder.view.js | 40 ++++++++++++++++-- 4 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 common/utils/question-handler/getQuestionListCodeBy.js delete mode 100644 common/utils/question-handler/getQuestionListCodeBySlug.js diff --git a/common/utils/question-handler/getQuestionListCodeBy.js b/common/utils/question-handler/getQuestionListCodeBy.js new file mode 100644 index 0000000..4ffafef --- /dev/null +++ b/common/utils/question-handler/getQuestionListCodeBy.js @@ -0,0 +1,41 @@ +import { createQuestionByTitleSlug, createQuestionById } from '#common/utils/cli-utils/createQuestion.js'; +import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuestionList.js'; + +// 根据 slug 获取创建promise列表 +async function createSlugPromiseList(slugList, baseDir = process.cwd()) { + return slugList.map((titleSlug) => { + return createQuestionByTitleSlug(titleSlug, baseDir); + }); +} + +// 根据 id 获取创建promise列表 +async function createIdPromiseList(questionList, baseDir = process.cwd()) { + return questionList.map((question) => { + return createQuestionById(question.questionId, baseDir); + }); +} + +/** + * 創建題目列表通過plan slug + * @param slug + * @param baseDir + */ +export async function getQuestionListCodeBySlug(slug, baseDir) { + const { planSubGroups } = await getPlanQuestionList(slug); + const questionTitleList = planSubGroups.reduce((acc, cur) => { + acc.push(...cur.questions.map((res) => res.titleSlug)); + return acc; + }, []); + const promiseList = await createSlugPromiseList(questionTitleList, baseDir); + return await Promise.allSettled(promiseList); +} + +/** + * 創建題目列表通過tag + * @param tagQuestionList + * @param baseDir + */ +export async function getQuestionListCodeByTag(tagQuestionList, baseDir) { + const promiseList = await createIdPromiseList(tagQuestionList, baseDir); + return await Promise.allSettled(promiseList); +} diff --git a/common/utils/question-handler/getQuestionListCodeBySlug.js b/common/utils/question-handler/getQuestionListCodeBySlug.js deleted file mode 100644 index 28b6236..0000000 --- a/common/utils/question-handler/getQuestionListCodeBySlug.js +++ /dev/null @@ -1,24 +0,0 @@ -import { createQuestionByTitleSlug } from '#common/utils/cli-utils/createQuestion.js'; -import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuestionList.js'; - -// 获取创建promise列表 -async function createCreatePromiseList(slugList, baseDir = process.cwd()) { - return slugList.map((titleSlug) => { - return createQuestionByTitleSlug(titleSlug, baseDir); - }); -} - -/** - * 創建題目列表通過plan slug - * @param slug - * @param baseDir - */ -export async function getQuestionListCodeBySlug(slug, baseDir) { - const { planSubGroups } = await getPlanQuestionList(slug); - const questionTitleList = planSubGroups.reduce((acc, cur) => { - acc.push(...cur.questions.map((res) => res.titleSlug)); - return acc; - }, []); - const promiseList = await createCreatePromiseList(questionTitleList, baseDir); - return await Promise.allSettled(promiseList); -} diff --git a/common/utils/store/controller/allQuestion.js b/common/utils/store/controller/allQuestion.js index 254444a..f4b935c 100644 --- a/common/utils/store/controller/allQuestion.js +++ b/common/utils/store/controller/allQuestion.js @@ -7,7 +7,7 @@ const oSign = '$object$'; */ function parseQuestion(obj) { if (!obj) return null; - Object.entries(obj).reduce((pre, [k, v]) => { + return Object.entries(obj).reduce((pre, [k, v]) => { pre[k] = typeof v == 'string' && v.startsWith(oSign) ? JSON.parse(v.replace(oSign, '')) : v; return pre; }, {}); diff --git a/common/view/finder.view.js b/common/view/finder.view.js index ab0c56e..8045b68 100644 --- a/common/view/finder.view.js +++ b/common/view/finder.view.js @@ -6,8 +6,9 @@ import { getQuestionByKeyword } from '#common/utils/question-getter/getQuestionB 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'; -import { getQuestionListCodeBySlug } from '#common/utils/question-handler/getQuestionListCodeBySlug.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';; function handleQuestionList(list) { const questionList = []; @@ -124,8 +125,41 @@ async function selectMode(baseDir = process.cwd()) { pageSize: 30 }; const chooseTag = await select(tagQuestion); - logger.info(baseDir); - console.log(chooseTag); + const allQuestion = await getAllQuestion(); + const tagQuestionList = await allQuestion.filter(question => question.topicTags.some(topic => topic.slug === chooseTag)); + + const createMode = await select({ + message: '拉题模式', + choices: [ + { name: '单个选择(不穩定)', value: 'single' }, + { name: '全部拉取(不穩定)', value: 'all' } + ] + }); + + if (createMode === 'single') { + const singleMode = { + type: 'list', + name: 'chooseTagQuestion', + message: '请选择题目', + choices: tagQuestionList.map((res) => ({ + name: res.translatedTitle, + value: res.questionId + })), + pageSize: 30 + } + + const singleChoice = await select(singleMode); + await createQuestionById(singleChoice, baseDir); + } + if (createMode === 'all') { + const dir = path.resolve(baseDir, chooseTag.toString()); + logger.off(); + await getQuestionListCodeByTag(tagQuestionList, dir); + logger.on(); + logger.info(`题目全部拉取完成: file://${dir}`); + } + + } export async function easyFinderView(baseDir = process.cwd()) { From 52d76c487ac62194f24e803c5f10a226bfcb33e3 Mon Sep 17 00:00:00 2001 From: SmallTeddy Date: Sat, 16 Mar 2024 16:40:21 +0800 Subject: [PATCH 2/2] feat: add lf select mode --- common/utils/question-handler/getQuestionListCodeBy.js | 2 +- common/view/finder.view.js | 8 +++----- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/common/utils/question-handler/getQuestionListCodeBy.js b/common/utils/question-handler/getQuestionListCodeBy.js index 4ffafef..f50787f 100644 --- a/common/utils/question-handler/getQuestionListCodeBy.js +++ b/common/utils/question-handler/getQuestionListCodeBy.js @@ -1,4 +1,4 @@ -import { createQuestionByTitleSlug, createQuestionById } from '#common/utils/cli-utils/createQuestion.js'; +import { createQuestionById, createQuestionByTitleSlug } from '#common/utils/cli-utils/createQuestion.js'; import { getPlanQuestionList } from '#common/utils/question-getter/getPlanQuestionList.js'; // 根据 slug 获取创建promise列表 diff --git a/common/view/finder.view.js b/common/view/finder.view.js index 8045b68..e99eac0 100644 --- a/common/view/finder.view.js +++ b/common/view/finder.view.js @@ -8,7 +8,7 @@ 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 } from '#common/utils/store/controller/allQuestion.js'; function handleQuestionList(list) { const questionList = []; @@ -126,7 +126,7 @@ 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)); + const tagQuestionList = await allQuestion.filter((question) => question.topicTags.some((topic) => topic.slug === chooseTag)); const createMode = await select({ message: '拉题模式', @@ -146,7 +146,7 @@ async function selectMode(baseDir = process.cwd()) { value: res.questionId })), pageSize: 30 - } + }; const singleChoice = await select(singleMode); await createQuestionById(singleChoice, baseDir); @@ -158,8 +158,6 @@ async function selectMode(baseDir = process.cwd()) { logger.on(); logger.info(`题目全部拉取完成: file://${dir}`); } - - } export async function easyFinderView(baseDir = process.cwd()) {