|
1 |
| -#! /usr/bin/env node |
2 |
| -import {program} from "commander"; |
3 |
| -import {artFontLogo} from "#resources/text/art-font-logo.js"; |
4 |
| -import {lcExamples} from "#resources/text/examples.js"; |
5 |
| -import {love} from "#resources/text/love.js"; |
6 |
| -import {aim} from "#resources/text/aim.js"; |
7 |
| -import {referMode} from "#common/utils/create-check/refer-mode.js"; |
8 |
| -import {getArgs} from "#common/utils/create-check/get-args.js"; |
9 |
| -import {getQuestionToday} from "#common/utils/question-getter/getQuestionToday.js"; |
10 |
| -import {createQuestion} from "#common/utils/question-handler/createQuestion.js"; |
11 |
| -import path from "path"; |
12 |
| -import {getQuestionFileName} from "#common/utils/question-handler/getQuestionFileName.js"; |
13 |
| -import {createQuestionCopy} from "#common/utils/question-handler/createQuestionCopy.js"; |
14 |
| -import {getQuestionRandom} from "#common/utils/question-getter/getQuestionRandom.js"; |
15 |
| -import {getQuestionById} from "#common/utils/question-getter/getQuestionById.js"; |
16 |
| -import {setQuestion} from "#common/utils/store/store-realm.js"; |
17 |
| -import {getQuestionChineseName} from "#common/utils/question-handler/getQuestionChineseName.js"; |
18 |
| -import {easyCreateView} from "#common/view/create.view.js"; |
19 |
| -import {description} from "#resources/text/description.js"; |
20 |
| -import {easyUpdateView} from "#common/view/update.view.js"; |
21 |
| - |
22 |
| -const version = process.env.VERSION??'0.0.0'; |
23 |
| -program |
24 |
| - .version(version) |
25 |
| - .description(`${description}\n${artFontLogo}\n${aim}`) |
26 |
| - .addHelpText('after', lcExamples+love) |
27 |
| - .arguments("[identity]") |
28 |
| - .option('-t, --today', 'Get a question today.') |
29 |
| - .option('-i, --identity <identity>', 'Specify a question by identity.') |
30 |
| - .option('-r, --random', 'Get a question randomly.') |
31 |
| - .option('-e, --easy', 'Use easy mode.') |
32 |
| - .option('-d, --directory <directory>', 'Set the question directory.') |
33 |
| - .option('-u, --update','Check the version to determine whether to update to the latest one.') |
34 |
| - .parse(process.argv) |
35 |
| - |
36 |
| -const cmdArgs = program.args; |
37 |
| -const cmdOpts = program.opts(); |
38 |
| -/** |
39 |
| - * 执行逻辑: |
40 |
| - * 目录检测 - 设置基础目录 |
41 |
| - * 模式检测 - 检测是不是easy mode |
42 |
| - * [参数检测 - 执行对应参数] |
43 |
| - */ |
44 |
| -// 根据dir 参数来设置基本目录 |
45 |
| -const baseDir = cmdOpts.directory?path.join(process.cwd(),cmdOpts.directory):process.cwd(); |
46 |
| -if(cmdOpts.easy){ |
47 |
| - await easyCreateView(); |
48 |
| - process.exit(0);; |
49 |
| -} |
50 |
| -// 检测更新 |
51 |
| -if(cmdOpts.update){ |
52 |
| - await easyUpdateView(); |
53 |
| - process.exit(0);; |
54 |
| -} |
55 |
| -// 创建 |
56 |
| -const create = (mode,question)=>{ |
57 |
| - console.log(`MODE: ${mode}`); |
58 |
| - return new Promise(resolve=>{ |
59 |
| - setQuestion(mode,question); |
60 |
| - const questionDir = path.join(baseDir,getQuestionFileName(question)) |
61 |
| - createQuestion(question,questionDir).then(async (path)=>{ |
62 |
| - if(!path)path = await createQuestionCopy(question,questionDir); |
63 |
| - console.log(`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:${path}`) |
64 |
| - resolve(true) |
65 |
| - }) |
66 |
| - }) |
67 |
| -} |
68 |
| -// 模式对应的action |
69 |
| -const callModeAction = { |
70 |
| - 'today': () => { |
71 |
| - getQuestionToday().then(question=>{ |
72 |
| - create("today",question).then(()=>{ |
73 |
| - process.exit(0) |
74 |
| - }); |
75 |
| - }) |
76 |
| - }, |
77 |
| - 'random': () => { |
78 |
| - getQuestionRandom().then(question=>{ |
79 |
| - create("random",question).then(()=>{ |
80 |
| - process.exit(0) |
81 |
| - }); |
82 |
| - }) |
83 |
| - }, |
84 |
| - 'identity': (id) => { |
85 |
| - getQuestionById(id).then(question=>{ |
86 |
| - create("identity",question).then(()=>{ |
87 |
| - process.exit(0) |
88 |
| - }); |
89 |
| - }) |
90 |
| - }, |
91 |
| -} |
92 |
| -// 获取模式和参数 |
93 |
| -const mode = referMode(cmdArgs, cmdOpts); |
94 |
| -const args = getArgs(mode,cmdArgs,cmdOpts); |
95 |
| -// 执行指令分发 |
96 |
| -await callModeAction[mode](args); |
| 1 | +#! /usr/bin/env node |
| 2 | +import {program} from "commander"; |
| 3 | +import {artFontLogo} from "#resources/text/art-font-logo.js"; |
| 4 | +import {lcExamples} from "#resources/text/examples.js"; |
| 5 | +import {love} from "#resources/text/love.js"; |
| 6 | +import {aim} from "#resources/text/aim.js"; |
| 7 | +import {referMode} from "#common/utils/create-check/refer-mode.js"; |
| 8 | +import {getArgs} from "#common/utils/create-check/get-args.js"; |
| 9 | +import {getQuestionToday} from "#common/utils/question-getter/getQuestionToday.js"; |
| 10 | +import {createQuestion} from "#common/utils/question-handler/createQuestion.js"; |
| 11 | +import path from "path"; |
| 12 | +import {getQuestionFileName} from "#common/utils/question-handler/getQuestionFileName.js"; |
| 13 | +import {createQuestionCopy} from "#common/utils/question-handler/createQuestionCopy.js"; |
| 14 | +import {getQuestionRandom} from "#common/utils/question-getter/getQuestionRandom.js"; |
| 15 | +import {getQuestionById} from "#common/utils/question-getter/getQuestionById.js"; |
| 16 | +import {setQuestion} from "#common/utils/store/store-realm.js"; |
| 17 | +import {getQuestionChineseName} from "#common/utils/question-handler/getQuestionChineseName.js"; |
| 18 | +import {easyCreateView} from "#common/view/create.view.js"; |
| 19 | +import {description} from "#resources/text/description.js"; |
| 20 | +import {easyUpdateView} from "#common/view/update.view.js"; |
| 21 | + |
| 22 | +const version = process.env.VERSION??'0.0.0'; |
| 23 | +program |
| 24 | + .version(version) |
| 25 | + .description(`${description}\n${artFontLogo}\n${aim}`) |
| 26 | + .addHelpText('after', lcExamples+love) |
| 27 | + .arguments("[identity]") |
| 28 | + .option('-t, --today', 'Get a question today.') |
| 29 | + .option('-i, --identity <identity>', 'Specify a question by identity.') |
| 30 | + .option('-r, --random', 'Get a question randomly.') |
| 31 | + .option('-e, --easy', 'Use easy mode.') |
| 32 | + .option('-d, --directory <directory>', 'Set the question directory.') |
| 33 | + .option('-u, --update','Check the version to determine whether to update to the latest one.') |
| 34 | + .parse(process.argv) |
| 35 | + |
| 36 | +const cmdArgs = program.args; |
| 37 | +const cmdOpts = program.opts(); |
| 38 | +/** |
| 39 | + * 执行逻辑: |
| 40 | + * 目录检测 - 设置基础目录 |
| 41 | + * 模式检测 - 检测是不是easy mode |
| 42 | + * [参数检测 - 执行对应参数] |
| 43 | + */ |
| 44 | +// 根据dir 参数来设置基本目录 |
| 45 | +const baseDir = cmdOpts.directory?path.join(process.cwd(),cmdOpts.directory):process.cwd(); |
| 46 | +if(cmdOpts.easy){ |
| 47 | + await easyCreateView(); |
| 48 | + process.exit(0);; |
| 49 | +} |
| 50 | +// 检测更新 |
| 51 | +if(cmdOpts.update){ |
| 52 | + await easyUpdateView(); |
| 53 | + process.exit(0);; |
| 54 | +} |
| 55 | +// 创建 |
| 56 | +const create = (mode,question)=>{ |
| 57 | + console.log(`MODE: ${mode}`); |
| 58 | + return new Promise(resolve=>{ |
| 59 | + setQuestion(mode,question); |
| 60 | + const questionDir = path.join(baseDir,getQuestionFileName(question)) |
| 61 | + createQuestion(question,questionDir).then(async (path)=>{ |
| 62 | + if(!path)path = await createQuestionCopy(question,questionDir); |
| 63 | + console.log(`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:${path}`) |
| 64 | + resolve(true) |
| 65 | + }) |
| 66 | + }) |
| 67 | +} |
| 68 | +// 模式对应的action |
| 69 | +const callModeAction = { |
| 70 | + 'today': () => { |
| 71 | + getQuestionToday().then(question=>{ |
| 72 | + create("today",question).then(()=>{ |
| 73 | + process.exit(0) |
| 74 | + }); |
| 75 | + }) |
| 76 | + }, |
| 77 | + 'random': () => { |
| 78 | + getQuestionRandom().then(question=>{ |
| 79 | + create("random",question).then(()=>{ |
| 80 | + process.exit(0) |
| 81 | + }); |
| 82 | + }) |
| 83 | + }, |
| 84 | + 'identity': (id) => { |
| 85 | + getQuestionById(id).then(question=>{ |
| 86 | + if(!question?.id) { |
| 87 | + console.log(`指定编号: [ ${id} ] 题目不存在.`) |
| 88 | + process.exit(0) |
| 89 | + } |
| 90 | + create("identity",question).then(()=>{ |
| 91 | + process.exit(0) |
| 92 | + }); |
| 93 | + }) |
| 94 | + }, |
| 95 | +} |
| 96 | +// 获取模式和参数 |
| 97 | +const mode = referMode(cmdArgs, cmdOpts); |
| 98 | +const args = getArgs(mode,cmdArgs,cmdOpts); |
| 99 | +// 执行指令分发 |
| 100 | +await callModeAction[mode](args); |
0 commit comments