@@ -4,28 +4,49 @@ const { cwd } = require('process')
44const dayjs = require ( 'dayjs' )
55const confirmExit = require ( './confirmExit' )
66
7+ /**
8+ * Get Config
9+ * @description Format content from configuration file and check validity.
10+ * @tips The `dateRange` will be transformed from `string[]` to `number[]`
11+ * @returns An object from the configuration file
12+ */
713module . exports = function ( ) {
14+ let isEN = true
15+
816 try {
17+ // Read content from configuration file
918 const configFile = resolve ( `${ cwd ( ) } /config.json` )
1019 const configStr = readFileSync ( configFile )
1120 if ( ! configStr ) {
1221 return null
1322 }
1423
15- // 格式化配置
1624 const config = JSON . parse ( configStr )
1725 const { dateRange } = config
1826
19- // 校验数据
27+ // Check validity
2028 const keys = Object . keys ( config )
2129 for ( let i = 0 ; i < keys . length ; i ++ ) {
2230 const key = keys [ i ]
2331 const value = config [ key ]
2432
25- // 格式化是个对象
33+ // Check language, Set the default language to English
34+ if ( key === 'lang' ) {
35+ if ( ! [ 'en' , 'zh' ] . includes ( value ) ) {
36+ config [ 'lang' ] = 'en'
37+ }
38+ }
39+ isEN = config [ 'lang' ] === 'en'
40+
41+ // Check object
2642 if ( key === 'format' ) {
2743 if ( Object . prototype . toString . call ( value ) !== '[object Object]' ) {
28- confirmExit ( `${ key } 必须是一个 { [key: string]: string } 对象` )
44+ confirmExit ( {
45+ msg : isEN
46+ ? `${ key } must be an object as { [key: string]: string }`
47+ : `${ key } 必须是一个 { [key: string]: string } 对象` ,
48+ isEN,
49+ } )
2950 return null
3051 }
3152
@@ -34,35 +55,61 @@ module.exports = function () {
3455 Object . hasOwnProperty . call ( value , k ) &&
3556 typeof value [ k ] !== 'string'
3657 ) {
37- confirmExit ( `${ key } 的 ${ k } 的值必须是一个 string 字符串` )
58+ confirmExit ( {
59+ msg : isEN
60+ ? `The value of ${ k } of ${ key } must be a string`
61+ : `${ key } 的 ${ k } 的值必须是一个 string 字符串` ,
62+ isEN,
63+ } )
3864 return null
3965 }
4066 }
4167 }
42- // 其他都是数组
43- else {
68+
69+ // Check array
70+ if (
71+ [ 'authors' , 'dateRange' , 'repos' , 'includes' , 'excludes' ] . includes ( key )
72+ ) {
4473 if ( ! Array . isArray ( value ) ) {
45- confirmExit ( `${ key } 必须是一个 string[] 数组` )
74+ confirmExit ( {
75+ msg : isEN
76+ ? `${ key } must be a string[] array`
77+ : `${ key } 必须是一个 string[] 数组` ,
78+ isEN,
79+ } )
4680 return null
4781 }
4882 if ( [ 'authors' , 'repos' ] . includes ( key ) && ! value . length ) {
49- confirmExit ( `${ key } 不能为空` )
83+ confirmExit ( {
84+ msg : isEN ? `${ key } cannot be empty` : `${ key } 不能为空` ,
85+ isEN,
86+ } )
5087 return null
5188 }
5289 if ( value . length ) {
5390 for ( let e = 0 ; e < value . length ; e ++ ) {
5491 if ( typeof value [ e ] !== 'string' ) {
55- confirmExit ( `${ key } 的每个 item 都必须是 string 格式` )
92+ confirmExit ( {
93+ msg : isEN
94+ ? `Each item of ${ key } must be a string`
95+ : `${ key } 的每个 item 都必须是 string 格式` ,
96+ isEN,
97+ } )
5698 return null
5799 }
58100 }
59101 }
60102 }
61103 }
62104
63- // 处理起止日期
105+ // Handle start date and end date, transform to timestamp
64106 if ( dateRange . length && dateRange . length !== 2 ) {
65- confirmExit ( `dateRange 只能有 2 个值,[开始日期, 结束日期]` )
107+ confirmExit ( {
108+ msg : isEN
109+ ? `dateRange can only have 2 values, [start date, end date]`
110+ : `dateRange 只能有 2 个值,[开始日期, 结束日期]` ,
111+ isEN,
112+ } )
66113 return null
67114 }
68115 let startTime = dayjs ( ) . startOf ( 'day' ) . unix ( ) * 1000
@@ -75,7 +122,10 @@ module.exports = function () {
75122
76123 return config
77124 } catch ( e ) {
78- confirmExit ( e )
125+ confirmExit ( {
126+ msg : e ,
127+ isEN,
128+ } )
79129 return null
80130 }
81131}
0 commit comments