-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlk.js
executable file
·111 lines (109 loc) · 3.88 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#! /usr/bin/env node
import fs from 'node:fs'
import path from 'node:path'
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 { checkQuestion } from '#common/utils/question-handler/checkQuestion.js'
import { getQuestionByMode } from '#common/utils/store/controller/question.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'
import { getQuestionFileExtension } from '#common/utils/question-handler/questionLanguage.js'
import { DefaultVer } from '#common/constants/question.const.js'
const version = process.env.VERSION ?? DefaultVer
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('-l, --language [language]', 'Set/Get the code language of question.')
.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
* [参数检测 - 执行对应参数]
*/
/**
* 语言设置
* -带参设置语言
* -无参获取语言
*/
if (cmdOpts.language) {
if (cmdOpts.language !== true) {
await easyLanguageView(cmdOpts.language)
}
else {
const lang = await getQuestionLanguage()
console.log(`当前CLI语言环境为:${lang}`)
}
process.exit(0)
}
// 根据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)
}
// 检测函数
async function check(mode, question) {
if (!question) {
console.log('题目信息不存在,请使用lc指令进行创建~')
return false
}
const filePath = path.join(baseDir, getQuestionFileName(question), `question${getQuestionFileExtension(question?.lang)}`)
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)