-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathlc.js
executable file
·73 lines (71 loc) · 2.98 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
68
69
70
71
72
73
#! /usr/bin/env node
import { DefaultVer } from '#common/constants/question.const.js';
import { commonMode } from '#common/utils/cli-utils/commonMode.js';
import { create } from '#common/utils/cli-utils/create.js';
import { createQuestionById } from '#common/utils/cli-utils/createQuestion.js';
import { getArgs } from '#common/utils/cli-utils/getArgs.js';
import { referMode } from '#common/utils/cli-utils/referMode.js';
import { logger } from '#common/utils/logger/logger.js';
import { getAllQuestionList } from '#common/utils/question-getter/getAllQuestionList.js';
import { getQuestionRandom } from '#common/utils/question-getter/getQuestionRandom.js';
import { getQuestionToday } from '#common/utils/question-getter/getQuestionToday.js';
import { setAllQuestion } from '#common/utils/store/controller/allQuestion.js';
import { easyCreateView } from '#common/view/create.view.js';
import { aim } from '#resources/text/aim.js';
import { artFontLogo } from '#resources/text/art-font-logo.js';
import { description } from '#resources/text/description.js';
import { lcExamples } from '#resources/text/examples.js';
import { love } from '#resources/text/love.js';
import { program } from 'commander';
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('-a, --all', 'Get all questions.')
.option('-v, --ver', 'Check the version info and some extra info about leetcode-practice.')
.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)
},
all: async () => {
const allQuestionData = await getAllQuestionList()
await setAllQuestion(allQuestionData)
logger.info('拉取全部题目成功!')
process.exit(0)
}
}
// 获取模式和参数
const mode = referMode(cmdArgs, cmdOpts)
const args = getArgs(mode, cmdArgs, cmdOpts)
// 执行指令分发
await callModeAction[mode](args)