1
1
#! /usr/bin/env node
2
- import { program } from "commander" ;
3
- import { artFontLogo } from "#resources/text/art-font-logo.js" ;
4
- import { lcExamples } from " #resources/text/examples .js" ;
5
- import { love } from " #resources/text/love .js" ;
6
- import { aim } from " #resources/text/aim .js" ;
7
- import { referMode } from "#common/utils/create-check/refer-mode .js" ;
8
- import { getArgs } from " #common/utils/create-check/get-args .js" ;
9
- import { getQuestionToday } from " #common/utils/question-getter/getQuestionToday .js" ;
10
- import { createQuestion } from " #common/utils/question-handler/createQuestion .js" ;
11
- import path from "path" ;
12
- import { getQuestionFileName } from " #common/utils/question-handler/getQuestionFileName.js" ;
13
- import { createQuestionCopy } from " #common/utils/question-handler/createQuestionCopy.js" ;
14
- import { getQuestionRandom } from " #common/utils/question-getter/getQuestionRandom.js" ;
15
- import { getQuestionById } from " #common/utils/question-getter/getQuestionById.js" ;
16
- import { setQuestion } from " #common/utils/store/controller/question.js" ;
17
- import { getQuestionChineseName } from " #common/utils/question-handler/getQuestionChineseName.js" ;
18
- import { easyCreateView } from " #common/view/create.view.js" ;
19
- import { description } from " #resources/text/description.js" ;
20
- import { easyUpdateView } from " #common/view/update.view.js" ;
21
- import { getQuestionLanguage } from " #common/utils/question-handler/questionLanguage.js" ;
22
- import { easyLanguageView } from " #common/view/language.view.js" ;
23
- import { DefaultVer } from " #common/constants/question.const.js" ;
2
+ import path from 'node:path'
3
+ import { program } from 'commander'
4
+ import { artFontLogo } from ' #resources/text/art-font-logo .js'
5
+ import { lcExamples } from ' #resources/text/examples .js'
6
+ import { love } from ' #resources/text/love .js'
7
+ 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'
10
+ import { getQuestionToday } from ' #common/utils/question-getter/getQuestionToday .js'
11
+ import { createQuestion } from '#common/utils/question-handler/createQuestion.js'
12
+ import { getQuestionFileName } from ' #common/utils/question-handler/getQuestionFileName.js'
13
+ import { createQuestionCopy } from ' #common/utils/question-handler/createQuestionCopy.js'
14
+ import { getQuestionRandom } from ' #common/utils/question-getter/getQuestionRandom.js'
15
+ import { getQuestionById } from ' #common/utils/question-getter/getQuestionById.js'
16
+ import { setQuestion } from ' #common/utils/store/controller/question.js'
17
+ import { getQuestionChineseName } from ' #common/utils/question-handler/getQuestionChineseName.js'
18
+ import { easyCreateView } from ' #common/view/create.view.js'
19
+ import { description } from ' #resources/text/description.js'
20
+ import { easyUpdateView } from ' #common/view/update.view.js'
21
+ import { getQuestionLanguage } from ' #common/utils/question-handler/questionLanguage.js'
22
+ import { easyLanguageView } from ' #common/view/language.view.js'
23
+ import { DefaultVer } from ' #common/constants/question.const.js'
24
24
25
- const version = process . env . VERSION ?? DefaultVer ;
25
+ const version = process . env . VERSION ?? DefaultVer
26
26
program
27
- . version ( version )
28
- . description ( `${ description } \n${ artFontLogo } \n${ aim } ` )
29
- . addHelpText ( 'after' , lcExamples + love )
30
- . arguments ( " [identity]" )
31
- . option ( '-t, --today' , 'Get a question today.' )
32
- . option ( '-i, --identity <identity>' , 'Specify a question by identity.' )
33
- . option ( '-r, --random' , 'Get a question randomly.' )
34
- . option ( '-e, --easy' , 'Use easy mode.' )
35
- . option ( '-d, --directory <directory>' , 'Set the question directory.' )
36
- . option ( '-l, --language [language]' , 'Set/Get the code language of question.' )
37
- . option ( '-u, --update' , 'Check the version to determine whether to update to the latest one.' )
38
- . parse ( process . argv )
27
+ . version ( version )
28
+ . description ( `${ description } \n${ artFontLogo } \n${ aim } ` )
29
+ . addHelpText ( 'after' , lcExamples + love )
30
+ . arguments ( ' [identity]' )
31
+ . option ( '-t, --today' , 'Get a question today.' )
32
+ . option ( '-i, --identity <identity>' , 'Specify a question by identity.' )
33
+ . option ( '-r, --random' , 'Get a question randomly.' )
34
+ . option ( '-e, --easy' , 'Use easy mode.' )
35
+ . option ( '-d, --directory <directory>' , 'Set the question directory.' )
36
+ . option ( '-l, --language [language]' , 'Set/Get the code language of question.' )
37
+ . option ( '-u, --update' , 'Check the version to determine whether to update to the latest one.' )
38
+ . parse ( process . argv )
39
39
40
- const cmdArgs = program . args ;
41
- const cmdOpts = program . opts ( ) ;
40
+ const cmdArgs = program . args
41
+ const cmdOpts = program . opts ( )
42
42
/**
43
43
* 执行逻辑:
44
44
* 目录检测 - 设置基础目录
@@ -51,68 +51,70 @@ const cmdOpts = program.opts();
51
51
* -无参获取语言
52
52
*/
53
53
if ( cmdOpts . language ) {
54
- if ( cmdOpts . language !== true ) {
55
- await easyLanguageView ( cmdOpts . language ) ;
56
- } else {
57
- const lang = await getQuestionLanguage ( ) ;
58
- console . log ( "当前CLI语言环境为:" + lang ) ;
59
- }
60
- process . exit ( 0 ) ;
54
+ if ( cmdOpts . language !== true ) {
55
+ await easyLanguageView ( cmdOpts . language )
56
+ }
57
+ else {
58
+ const lang = await getQuestionLanguage ( )
59
+ console . log ( `当前CLI语言环境为:${ lang } ` )
60
+ }
61
+ process . exit ( 0 )
61
62
}
62
63
// 根据dir 参数来设置基本目录
63
- const baseDir = cmdOpts . directory ? path . join ( process . cwd ( ) , cmdOpts . directory ) : process . cwd ( ) ;
64
- if ( cmdOpts . easy ) {
65
- await easyCreateView ( ) ;
66
- process . exit ( 0 ) ;
64
+ const baseDir = cmdOpts . directory ? path . join ( process . cwd ( ) , cmdOpts . directory ) : process . cwd ( )
65
+ if ( cmdOpts . easy ) {
66
+ await easyCreateView ( )
67
+ process . exit ( 0 )
67
68
}
68
69
// 检测更新
69
- if ( cmdOpts . update ) {
70
- await easyUpdateView ( ) ;
71
- process . exit ( 0 ) ;
70
+ if ( cmdOpts . update ) {
71
+ await easyUpdateView ( )
72
+ process . exit ( 0 )
72
73
}
73
74
// 创建
74
- const create = ( mode , question ) => {
75
- console . log ( `MODE: ${ mode } ` ) ;
76
- return new Promise ( resolve => {
77
- setQuestion ( mode , question ) ;
78
- const questionDir = path . join ( baseDir , getQuestionFileName ( question ) )
79
- createQuestion ( question , questionDir ) . then ( async ( path ) => {
80
- if ( ! path ) path = await createQuestionCopy ( question , questionDir ) ;
81
- console . log ( `题目[ ${ getQuestionChineseName ( question ) } ]获取成功!\n题目文件地址为: ${ path } ` )
82
- resolve ( true )
83
- } )
75
+ function create ( mode , question ) {
76
+ console . log ( `MODE: ${ mode } ` )
77
+ return new Promise ( ( resolve ) => {
78
+ setQuestion ( mode , question )
79
+ const questionDir = path . join ( baseDir , getQuestionFileName ( question ) )
80
+ createQuestion ( question , questionDir ) . then ( async ( path ) => {
81
+ if ( ! path )
82
+ path = await createQuestionCopy ( question , questionDir )
83
+ console . log ( `题目[ ${ getQuestionChineseName ( question ) } ]获取成功!\n题目文件地址为: ${ path } ` )
84
+ resolve ( true )
84
85
} )
86
+ } )
85
87
}
86
88
// 模式对应的action
87
89
const callModeAction = {
88
- ' today' : ( ) => {
89
- getQuestionToday ( ) . then ( question => {
90
- create ( " today" , question ) . then ( ( ) => {
91
- process . exit ( 0 )
92
- } ) ;
93
- } )
94
- } ,
95
- ' random' : ( ) => {
96
- getQuestionRandom ( ) . then ( question => {
97
- create ( " random" , question ) . then ( ( ) => {
98
- process . exit ( 0 )
99
- } ) ;
100
- } )
101
- } ,
102
- ' identity' : ( id ) => {
103
- getQuestionById ( id ) . then ( question => {
104
- if ( ! question ?. id ) {
105
- console . log ( `指定编号: [ ${ id } ] 题目不存在.` )
106
- process . exit ( 0 )
107
- }
108
- create ( " identity" , question ) . then ( ( ) => {
109
- process . exit ( 0 )
110
- } ) ;
111
- } )
112
- } ,
90
+ today : ( ) => {
91
+ getQuestionToday ( ) . then ( ( question ) => {
92
+ create ( ' today' , question ) . then ( ( ) => {
93
+ process . exit ( 0 )
94
+ } )
95
+ } )
96
+ } ,
97
+ random : ( ) => {
98
+ getQuestionRandom ( ) . then ( ( question ) => {
99
+ create ( ' random' , question ) . then ( ( ) => {
100
+ process . exit ( 0 )
101
+ } )
102
+ } )
103
+ } ,
104
+ identity : ( id ) => {
105
+ getQuestionById ( id ) . then ( ( question ) => {
106
+ if ( ! question ?. id ) {
107
+ console . log ( `指定编号: [ ${ id } ] 题目不存在.` )
108
+ process . exit ( 0 )
109
+ }
110
+ create ( ' identity' , question ) . then ( ( ) => {
111
+ process . exit ( 0 )
112
+ } )
113
+ } )
114
+ } ,
113
115
}
114
116
// 获取模式和参数
115
- const mode = referMode ( cmdArgs , cmdOpts ) ;
116
- const args = getArgs ( mode , cmdArgs , cmdOpts ) ;
117
+ const mode = referMode ( cmdArgs , cmdOpts )
118
+ const args = getArgs ( mode , cmdArgs , cmdOpts )
117
119
// 执行指令分发
118
- await callModeAction [ mode ] ( args ) ;
120
+ await callModeAction [ mode ] ( args )
0 commit comments