diff --git a/common/utils/question-handler/getQuestionListCodeBy.js b/common/utils/question-handler/getQuestionListCodeBy.js new file mode 100644 index 0000000..f50787f --- /dev/null +++ b/common/utils/question-handler/getQuestionListCodeBy.js @@ -0,0 +1,41 @@ +import { createQuestionById, createQuestionByTitleSlug } 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..e99eac0 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,39 @@ 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()) {