1
1
#! /usr/bin/env node
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'
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 path from "path" ;
24
11
25
- const version = process . env . VERSION ?? DefaultVer
12
+ import { getQuestionRandom } from "#common/utils/question-getter/getQuestionRandom.js" ;
13
+
14
+ import { easyCreateView } from "#common/view/create.view.js" ;
15
+ 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" ;
19
+ import { DefaultVer } from "#common/constants/question.const.js" ;
20
+ import { create , createQuestionById } from "#common/utils/create-check/createUtil.js" ;
21
+
22
+ const version = process . env . VERSION ?? DefaultVer ;
26
23
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 )
24
+ . version ( version )
25
+ . description ( `${ description } \n${ artFontLogo } \n${ aim } ` )
26
+ . addHelpText ( 'after' , lcExamples + love )
27
+ . arguments ( " [identity]" )
28
+ . option ( '-t, --today' , 'Get a question today.' )
29
+ . option ( '-i, --identity <identity>' , 'Specify a question by identity.' )
30
+ . option ( '-r, --random' , 'Get a question randomly.' )
31
+ . option ( '-e, --easy' , 'Use easy mode.' )
32
+ . option ( '-d, --directory <directory>' , 'Set the question directory.' )
33
+ . option ( '-l, --language [language]' , 'Set/Get the code language of question.' )
34
+ . option ( '-u, --update' , 'Check the version to determine whether to update to the latest one.' )
35
+ . parse ( process . argv )
39
36
40
37
const cmdArgs = program . args
41
38
const cmdOpts = program . opts ( )
@@ -51,14 +48,13 @@ const cmdOpts = program.opts()
51
48
* -无参获取语言
52
49
*/
53
50
if ( cmdOpts . language ) {
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 )
51
+ if ( cmdOpts . language !== true ) {
52
+ await easyLanguageView ( cmdOpts . language ) ;
53
+ } else {
54
+ const lang = await getQuestionLanguage ( ) ;
55
+ console . log ( "当前CLI语言环境为:" + lang ) ;
56
+ }
57
+ process . exit ( 0 ) ;
62
58
}
63
59
// 根据dir 参数来设置基本目录
64
60
const baseDir = cmdOpts . directory ? path . join ( process . cwd ( ) , cmdOpts . directory ) : process . cwd ( )
@@ -72,46 +68,24 @@ if (cmdOpts.update) {
72
68
process . exit ( 0 )
73
69
}
74
70
// 创建
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 )
85
- } )
86
- } )
87
- }
71
+
88
72
// 模式对应的action
89
- const callModeAction = {
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
- } ,
73
+ export const callModeAction = {
74
+ 'today' : ( ) => {
75
+ getQuestionToday ( ) . then ( question => {
76
+ create ( "today" , question , baseDir ) . then ( ( ) => {
77
+ process . exit ( 0 )
78
+ } ) ;
79
+ } )
80
+ } ,
81
+ 'random' : ( ) => {
82
+ getQuestionRandom ( ) . then ( question => {
83
+ create ( "random" , question , baseDir ) . then ( ( ) => {
84
+ process . exit ( 0 )
85
+ } ) ;
86
+ } )
87
+ } ,
88
+ 'identity' :( id ) => createQuestionById ( id , baseDir ) ,
115
89
}
116
90
// 获取模式和参数
117
91
const mode = referMode ( cmdArgs , cmdOpts )
0 commit comments