-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlc.js
executable file
·67 lines (63 loc) · 2.4 KB
/
lc.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
#! /usr/bin/env node
import { program } from 'commander'
import { artFontLogo } from '#resources/text/art-font-logo.js'
import { lcExamples } from '#resources/text/examples.js'
import { love } from '#resources/text/love.js'
import { aim } from '#resources/text/aim.js'
import { referMode } from '#common/utils/cli-utils/referMode.js'
import { getArgs } from '#common/utils/cli-utils/getArgs.js'
import { getQuestionToday } from '#common/utils/question-getter/getQuestionToday.js'
import { getQuestionRandom } from '#common/utils/question-getter/getQuestionRandom.js'
import { easyCreateView } from '#common/view/create.view.js'
import { description } from '#resources/text/description.js'
import { DefaultVer } from '#common/constants/question.const.js'
import { createQuestionById } from '#common/utils/cli-utils/createQuestion.js'
import { create } from '#common/utils/cli-utils/create.js'
import { commonMode } from '#common/utils/cli-utils/commonMode.js'
const version = process.env.VERSION ?? DefaultVer
program
.version(version)
.description(`${description}\n${artFontLogo}\n${aim}`)
.addHelpText('after', lcExamples + love)
.arguments('[identity]')
.option('-t, --today', 'Get a question today.')
.option('-i, --identity <identity>', 'Specify a question by identity.')
.option('-r, --random', 'Get a question randomly.')
.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()
// 通用参数执行
const baseDir = await commonMode(cmdOpts, easyCreateView)
// 模式对应的action
export const callModeAction = {
today: () => {
getQuestionToday().then((question) => {
create('today', question, baseDir).then(() => {
process.exit(0)
})
})
},
random: () => {
getQuestionRandom().then((question) => {
create('random', question, baseDir).then(() => {
process.exit(0)
})
})
},
identity: async (id) => {
await createQuestionById(id, baseDir)
process.exit(0)
}
}
// 获取模式和参数
const mode = referMode(cmdArgs, cmdOpts)
const args = getArgs(mode, cmdArgs, cmdOpts)
// 执行指令分发
await callModeAction[mode](args)