2
2
import fs from 'node:fs'
3
3
import path from 'node:path'
4
4
import { program } from 'commander'
5
+ import select from '@inquirer/select'
5
6
import { artFontLogo } from '#resources/text/art-font-logo.js'
6
7
import { lkExamples } from '#resources/text/examples.js'
7
8
import { love } from '#resources/text/love.js'
8
9
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'
12
13
import { getQuestionByMode } from '#common/utils/store/controller/question.js'
13
- import { getQuestionById } from '#common/utils/question-getter/getQuestionById.js'
14
14
import { getQuestionFileName } from '#common/utils/question-handler/getQuestionFileName.js'
15
15
import { getQuestionChineseName } from '#common/utils/question-handler/getQuestionChineseName.js'
16
16
import { easyCheckView } from '#common/view/check.view.js'
17
17
import { description } from '#resources/text/description.js'
18
- import { easyUpdateView } from '#common/view/update.view.js'
19
18
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'
21
20
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'
22
24
23
25
const version = process . env . VERSION ?? DefaultVer
24
26
program
@@ -44,43 +46,10 @@ program
44
46
const cmdArgs = program . args
45
47
const cmdOpts = program . opts ( )
46
48
// 获取模式和参数
47
-
48
49
const mode = referMode ( cmdArgs , cmdOpts )
49
50
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 )
84
53
// 检测函数
85
54
async function check ( mode , question ) {
86
55
if ( ! question ) {
@@ -98,7 +67,7 @@ async function check(mode, question) {
98
67
logger . info (
99
68
`MODE: ${ mode } \n题目[${ getQuestionChineseName ( question ) } ]检测结果:`
100
69
)
101
- await checkQuestion ( filePath )
70
+ await checkQuestionByPath ( filePath )
102
71
}
103
72
return true
104
73
}
@@ -115,13 +84,78 @@ const callModeAction = {
115
84
process . exit ( 0 )
116
85
} ,
117
86
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
+ }
122
157
process . exit ( 0 )
123
158
}
124
159
}
125
-
126
160
// 执行指令分发
127
161
callModeAction [ mode ] ( args )
0 commit comments