Skip to content

Commit bec7081

Browse files
committed
feat: lk check question by file
1 parent 6dfe1e7 commit bec7081

18 files changed

+230
-172
lines changed

.husky/pre-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
# 如果要执行 rebase 需要跳过当前所有hooks
55
# todo-highlight HUSKY=0 git rebase ...
66
echo "Running pre-commit script..."
7-
npm run lint && npm run format
7+
npm run lint && npm run format && git add .

bin/lc.js

+7-46
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,21 @@
11
#! /usr/bin/env node
2-
import path from 'node:path'
32
import { program } from 'commander'
43
import { artFontLogo } from '#resources/text/art-font-logo.js'
54
import { lcExamples } from '#resources/text/examples.js'
65
import { love } from '#resources/text/love.js'
76
import { aim } from '#resources/text/aim.js'
8-
import { referMode } from '#common/utils/create-check/refer-mode.js'
9-
import { getArgs } from '#common/utils/create-check/get-args.js'
7+
import { referMode } from '#common/utils/cli-utils/referMode.js'
8+
import { getArgs } from '#common/utils/cli-utils/getArgs.js'
109
import { getQuestionToday } from '#common/utils/question-getter/getQuestionToday.js'
1110

1211
import { getQuestionRandom } from '#common/utils/question-getter/getQuestionRandom.js'
1312

1413
import { easyCreateView } from '#common/view/create.view.js'
1514
import { description } from '#resources/text/description.js'
16-
import { easyUpdateView } from '#common/view/update.view.js'
17-
import { getQuestionLanguage } from '#common/utils/question-handler/questionLanguage.js'
18-
import { easyLanguageView } from '#common/view/language.view.js'
1915
import { DefaultVer } from '#common/constants/question.const.js'
20-
import {
21-
create,
22-
createQuestionById
23-
} from '#common/utils/create-check/createUtil.js'
24-
import { logger } from '#common/utils/logger/logger.js'
16+
import { createQuestionById } from '#common/utils/cli-utils/createQuestion.js'
17+
import { create } from '#common/utils/cli-utils/create.js'
18+
import { commonMode } from '#common/utils/cli-utils/commonMode.js'
2519

2620
const version = process.env.VERSION ?? DefaultVer
2721
program
@@ -43,41 +37,8 @@ program
4337

4438
const cmdArgs = program.args
4539
const cmdOpts = program.opts()
46-
/**
47-
* 执行逻辑:
48-
* 目录检测 - 设置基础目录
49-
* 模式检测 - 检测是不是easy mode
50-
* [参数检测 - 执行对应参数]
51-
*/
52-
/**
53-
* 语言设置
54-
* -带参设置语言
55-
* -无参获取语言
56-
*/
57-
if (cmdOpts.language) {
58-
if (cmdOpts.language !== true) {
59-
await easyLanguageView(cmdOpts.language)
60-
} else {
61-
const lang = await getQuestionLanguage()
62-
logger.info(`当前CLI语言环境为:${lang}`)
63-
}
64-
process.exit(0)
65-
}
66-
// 根据dir 参数来设置基本目录
67-
const baseDir = cmdOpts.directory
68-
? path.join(process.cwd(), cmdOpts.directory)
69-
: process.cwd()
70-
if (cmdOpts.easy) {
71-
await easyCreateView()
72-
process.exit(0)
73-
}
74-
// 检测更新
75-
if (cmdOpts.update) {
76-
await easyUpdateView()
77-
process.exit(0)
78-
}
79-
// 创建
80-
40+
// 通用参数执行
41+
const baseDir = await commonMode(cmdOpts, easyCreateView)
8142
// 模式对应的action
8243
export const callModeAction = {
8344
today: () => {

bin/lf.js

+6-31
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
#! /usr/bin/env node
2-
import path from 'node:path'
32
import { program } from 'commander'
43
import { description } from '#resources/text/description.js'
54
import { artFontLogo } from '#resources/text/art-font-logo.js'
65
import { aim } from '#resources/text/aim.js'
76
import { lfExamples } from '#resources/text/examples.js'
87
import { love } from '#resources/text/love.js'
9-
import { easyUpdateView } from '#common/view/update.view.js'
10-
import { easyLanguageView } from '#common/view/language.view.js'
11-
import { getQuestionLanguage } from '#common/utils/question-handler/questionLanguage.js'
128
import { DefaultVer } from '#common/constants/question.const.js'
139
import { easyFinderView } from '#common/view/finder.view.js'
10+
import { commonMode } from '#common/utils/cli-utils/commonMode.js'
11+
import { willUse } from '#common/utils/etc/willUse.js'
1412

1513
const version = process.env.VERSION ?? DefaultVer
1614
program
@@ -25,32 +23,9 @@ program
2523
)
2624
.parse(process.argv)
2725

28-
// const cmdArgs = program.args
26+
const cmdArgs = program.args
2927
const cmdOpts = program.opts()
30-
31-
/**
32-
* 语言设置
33-
* -带参设置语言
34-
* -无参获取语言
35-
*/
36-
if (cmdOpts.language) {
37-
if (cmdOpts.language !== true) {
38-
await easyLanguageView(cmdOpts.language)
39-
} else {
40-
const lang = await getQuestionLanguage()
41-
logger.info(`当前CLI语言环境为:${lang}`)
42-
}
43-
process.exit(0)
44-
}
45-
// 根据dir 参数来设置基本目录
46-
const baseDir = cmdOpts.directory
47-
? path.join(process.cwd(), cmdOpts.directory)
48-
: process.cwd()
49-
// 检测更新
50-
if (cmdOpts.update) {
51-
await easyUpdateView()
52-
process.exit(0)
53-
}
54-
// 进入视图操作
55-
await easyFinderView(baseDir)
28+
// 通用参数执行
29+
const baseDir = await commonMode(cmdOpts, easyFinderView)
30+
willUse(cmdArgs, baseDir)
5631
process.exit(0)

bin/lk.js

+81-47
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22
import fs from 'node:fs'
33
import path from 'node:path'
44
import { program } from 'commander'
5+
import select from '@inquirer/select'
56
import { artFontLogo } from '#resources/text/art-font-logo.js'
67
import { lkExamples } from '#resources/text/examples.js'
78
import { love } from '#resources/text/love.js'
89
import { aim } from '#resources/text/aim.js'
9-
import { referMode } from '#common/utils/create-check/refer-mode.js'
10-
import { getArgs } from '#common/utils/create-check/get-args.js'
11-
import { checkQuestion } from '#common/utils/question-handler/checkQuestion.js'
10+
import { referMode } from '#common/utils/cli-utils/referMode.js'
11+
import { getArgs } from '#common/utils/cli-utils/getArgs.js'
12+
import { checkQuestionByPath } from '#common/utils/question-handler/checkQuestionByPath.js'
1213
import { getQuestionByMode } from '#common/utils/store/controller/question.js'
13-
import { getQuestionById } from '#common/utils/question-getter/getQuestionById.js'
1414
import { getQuestionFileName } from '#common/utils/question-handler/getQuestionFileName.js'
1515
import { getQuestionChineseName } from '#common/utils/question-handler/getQuestionChineseName.js'
1616
import { easyCheckView } from '#common/view/check.view.js'
1717
import { description } from '#resources/text/description.js'
18-
import { easyUpdateView } from '#common/view/update.view.js'
1918
import { getQuestionFileExtension } from '#common/utils/question-handler/questionLanguage.js'
20-
import { DefaultVer } from '#common/constants/question.const.js'
19+
import { DefaultLang, DefaultVer } from '#common/constants/question.const.js'
2120
import { logger } from '#common/utils/logger/logger.js'
21+
import { commonMode } from '#common/utils/cli-utils/commonMode.js'
22+
import { getFilePathById } from '#common/utils/file/getFilePathById.js'
23+
import { getQuestionFileInDir } from '#common/utils/file/getQuestionInDir.js'
2224

2325
const version = process.env.VERSION ?? DefaultVer
2426
program
@@ -44,43 +46,10 @@ program
4446
const cmdArgs = program.args
4547
const cmdOpts = program.opts()
4648
// 获取模式和参数
47-
4849
const mode = referMode(cmdArgs, cmdOpts)
4950
const args = getArgs(mode, cmdArgs, cmdOpts)
50-
51-
/**
52-
* 执行逻辑:
53-
* 目录检测 - 设置基础目录
54-
* 模式检测 - 检测是不是easy mode
55-
* [参数检测 - 执行对应参数]
56-
*/
57-
/**
58-
* 语言设置
59-
* -带参设置语言
60-
* -无参获取语言
61-
*/
62-
if (cmdOpts.language) {
63-
if (cmdOpts.language !== true) {
64-
await easyLanguageView(cmdOpts.language)
65-
} else {
66-
const lang = await getQuestionLanguage()
67-
logger.info(`当前CLI语言环境为:${lang}`)
68-
}
69-
process.exit(0)
70-
}
71-
// 根据dir 参数来设置基本目录
72-
const baseDir = cmdOpts.directory
73-
? path.join(process.cwd(), cmdOpts.directory)
74-
: process.cwd()
75-
if (cmdOpts.easy) {
76-
await easyCheckView()
77-
process.exit(0)
78-
}
79-
// 检测更新
80-
if (cmdOpts.update) {
81-
await easyUpdateView()
82-
process.exit(0)
83-
}
51+
// 通用参数执行
52+
const baseDir = await commonMode(cmdOpts, easyCheckView)
8453
// 检测函数
8554
async function check(mode, question) {
8655
if (!question) {
@@ -98,7 +67,7 @@ async function check(mode, question) {
9867
logger.info(
9968
`MODE: ${mode}\n题目[${getQuestionChineseName(question)}]检测结果:`
10069
)
101-
await checkQuestion(filePath)
70+
await checkQuestionByPath(filePath)
10271
}
10372
return true
10473
}
@@ -115,13 +84,78 @@ const callModeAction = {
11584
process.exit(0)
11685
},
11786
identity: async (id) => {
118-
const question = !id
119-
? await getQuestionByMode(mode)
120-
: await getQuestionById(id)
121-
await check('identity', question)
87+
let question
88+
if (!id) {
89+
// 如果未指定id说明是要检测模式创建的题目
90+
question = await getQuestionByMode(mode)
91+
await check('identity', question)
92+
} else {
93+
question = await getFilePathById(id)
94+
const needToSelect = {
95+
type: 'list',
96+
name: 'need',
97+
message: '在当前目录下存在多个题目,请选择你要检测的题目:',
98+
choices: []
99+
}
100+
/**
101+
* 只检查一个题目
102+
* @param fileOrFiles
103+
* @returns {Promise<void>}
104+
*/
105+
const checkOne = async (fileOrFiles) => {
106+
const needToCheck = {
107+
type: 'list',
108+
name: 'check',
109+
message: '存在多个题目文件,请选择:',
110+
choices: [],
111+
default: null
112+
}
113+
let filePath
114+
switch (typeof fileOrFiles) {
115+
case 'undefined':
116+
logger.warn(
117+
`虽然在题目目录中,但当前目录下不存在[${id}]的题目文件!`
118+
)
119+
process.exit(0)
120+
break
121+
case 'string':
122+
filePath = fileOrFiles
123+
break
124+
case 'object':
125+
needToCheck.choices = fileOrFiles.map((o) => {
126+
return { name: o, value: o }
127+
})
128+
needToCheck.default = fileOrFiles?.find((o) =>
129+
o.endsWith(getQuestionFileExtension(DefaultLang))
130+
)
131+
filePath = await select(needToCheck)
132+
break
133+
}
134+
return await checkQuestionByPath(filePath)
135+
}
136+
137+
let files
138+
switch (typeof question) {
139+
case 'undefined':
140+
logger.warn(`当前目录下未找到题目id为[${id}]的题目!`)
141+
process.exit(0)
142+
break
143+
case 'string':
144+
files = getQuestionFileInDir(path.resolve(baseDir, question))
145+
break
146+
case 'object':
147+
needToSelect.choices = question.map((o) => {
148+
return { name: o, value: o }
149+
})
150+
files = getQuestionFileInDir(
151+
path.resolve(baseDir, await select(needToSelect))
152+
)
153+
break
154+
}
155+
await checkOne(files)
156+
}
122157
process.exit(0)
123158
}
124159
}
125-
126160
// 执行指令分发
127161
callModeAction[mode](args)

common/utils/cli-utils/commonMode.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import path from 'node:path'
2+
import { easyUpdateView } from '#common/view/update.view.js'
3+
import { getQuestionLanguage } from '#common/utils/question-handler/questionLanguage.js'
4+
import { easyLanguageView } from '#common/view/language.view.js'
5+
import { logger } from '#common/utils/logger/logger.js'
6+
7+
/**
8+
* 执行逻辑:
9+
* 目录检测 - 设置基础目录
10+
* 模式检测 - 检测是不是easy mode
11+
* [参数检测 - 执行对应参数]
12+
*/
13+
/**
14+
* 通用参数的执行逻辑
15+
* @param cmdOpts{{directory:string,language:string|boolean,easy:boolean,update:boolean,[key:string]:*}}
16+
* @param easyCallback{(baseDir:string)=>Promise<any>}
17+
* @returns {Promise<string>}
18+
*/
19+
export async function commonMode(cmdOpts, easyCallback) {
20+
// 根据dir 参数来设置基本目录
21+
const baseDir = cmdOpts.directory
22+
? path.join(process.cwd(), cmdOpts.directory)
23+
: process.cwd()
24+
/**
25+
* 语言设置
26+
* -带参设置语言
27+
* -无参获取语言
28+
*/
29+
if (cmdOpts.language) {
30+
if (cmdOpts.language !== true) {
31+
await easyLanguageView(cmdOpts.language)
32+
} else {
33+
const lang = await getQuestionLanguage()
34+
logger.info(`当前CLI语言环境为:${lang}`)
35+
}
36+
process.exit(0)
37+
}
38+
if (cmdOpts?.easy) {
39+
await easyCallback(baseDir)
40+
process.exit(0)
41+
}
42+
// 检测更新
43+
if (cmdOpts.update) {
44+
await easyUpdateView()
45+
process.exit(0)
46+
}
47+
return baseDir
48+
}

0 commit comments

Comments
 (0)