-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlk.js
executable file
·89 lines (86 loc) · 3.3 KB
/
lk.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#! /usr/bin/env node
import {program} from "commander";
import {artFontLogo} from "#resources/text/art-font-logo.js";
import {lkExamples} from "#resources/text/examples.js";
import {love} from "#resources/text/love.js";
import {aim} from "#resources/text/aim.js";
import {referMode} from "#common/utils/create-check/refer-mode.js";
import {getArgs} from "#common/utils/create-check/get-args.js";
import fs from "fs";
import path from "path";
import {checkQuestion} from "#common/utils/question-handler/checkQuestion.js";
import {getQuestionByMode} from "#common/utils/store/store-realm.js";
import {getQuestionById} from "#common/utils/question-getter/getQuestionById.js";
import {getQuestionFileName} from "#common/utils/question-handler/getQuestionFileName.js";
import {getQuestionChineseName} from "#common/utils/question-handler/getQuestionChineseName.js";
import {easyCheckView} from "#common/view/check.view.js";
import {description} from "#resources/text/description.js";
import {easyUpdateView} from "#common/view/update.view.js";
const version = process.env.VERSION ?? '0.0.0';
program
.version(version)
.description(`${description}\n${artFontLogo}\n${aim}`)
.addHelpText('after', lkExamples + love)
.arguments("[identity]")
.option('-t, --today', 'Check the question today.')
.option('-i, --identity <identity>', 'Check the specified question by identity.')
.option('-r, --random', 'Check the last random question.')
.option('-e, --easy', 'Use easy mode.')
.option('-d, --directory <directory>', 'Set the question directory.')
.option('-u, --update','Check the version to determine whether to update to the latest one.')
.parse(process.argv)
const cmdArgs = program.args;
const cmdOpts = program.opts();
/**
* 执行逻辑:
* 目录检测 - 设置基础目录
* 模式检测 - 检测是不是easy mode
* [参数检测 - 执行对应参数]
*/
// 根据dir 参数来设置基本目录
const baseDir = cmdOpts.directory ? path.join(process.cwd(), cmdOpts.directory) : process.cwd();
if (cmdOpts.easy) {
await easyCheckView();
process.exit(0);
}
// 检测更新
if(cmdOpts.update){
await easyUpdateView();
process.exit(0);
}
// 检测函数
const check = async (mode, question) => {
const filePath = path.join(baseDir, getQuestionFileName(question), 'index.js');
if (!fs.existsSync(filePath)) {
console.log(`文件[${filePath}]不存在,请确保已经创建!`)
} else {
console.log(`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检测结果:`)
await checkQuestion(filePath);
}
return true;
}
// 模式对应的action
const callModeAction = {
'today': async () => {
const question = await getQuestionByMode("today");
await check('today', question)
process.exit(0);
},
'random': async () => {
const question = await getQuestionByMode("random");
await check('random', question)
process.exit(0);
},
'identity': async (id) => {
const question = !id ?
await getQuestionByMode(mode) :
await getQuestionById(id);
await check('identity', question)
process.exit(0);
},
}
// 获取模式和参数
const mode = referMode(cmdArgs, cmdOpts);
const args = getArgs(mode, cmdArgs, cmdOpts);
// 执行指令分发
callModeAction[mode](args);