File tree Expand file tree Collapse file tree 6 files changed +39
-12
lines changed
Expand file tree Collapse file tree 6 files changed +39
-12
lines changed Original file line number Diff line number Diff line change @@ -27,11 +27,18 @@ export const getPkgJson = () => {
2727}
2828
2929export const getCurrentConfig = ( ) => {
30- const currentUserName = execSync ( 'git config --get user.name' ) . toString ( ) . trim ( )
31- const currentUserEmail = execSync ( 'git config --get user.email' ) . toString ( ) . trim ( )
32- return {
33- name : currentUserName ,
34- email : currentUserEmail
30+ try {
31+ const currentUserName = execSync ( 'git config --get user.name' ) . toString ( ) . trim ( )
32+ const currentUserEmail = execSync ( 'git config --get user.email' ) . toString ( ) . trim ( )
33+ if ( ! currentUserName || ! currentUserEmail ) {
34+ return null
35+ }
36+ return {
37+ name : currentUserName ,
38+ email : currentUserEmail
39+ }
40+ } catch {
41+ return null
3542 }
3643}
3744
Original file line number Diff line number Diff line change @@ -5,12 +5,20 @@ import pc from 'picocolors'
55export const current = ( ) => {
66 const allConfigs = getAllUserConfigs ( )
77 const currentConfig = getCurrentConfig ( )
8+ if ( ! currentConfig ) {
9+ console . log ( pc . red ( '未检测到当前 git 配置(user.name/user.email)。' ) )
10+ return
11+ }
812 const currentInConfigJson = allConfigs . find ( ( config : UserConfig ) => {
9- return config . name === currentConfig . name && config . email === config . email
13+ return config . name === currentConfig . name && config . email === currentConfig . email
1014 } )
1115 if ( currentInConfigJson ) {
1216 console . log ( pc . green ( `当前使用的配置:
1317user.name: ${ currentConfig . name }
18+ user.email: ${ currentConfig . email } ` ) )
19+ } else {
20+ console . log ( pc . yellow ( `当前配置未在列表中:
21+ user.name: ${ currentConfig . name }
1422user.email: ${ currentConfig . email } ` ) )
1523 }
1624}
Original file line number Diff line number Diff line change @@ -12,6 +12,10 @@ export const doctor = () => {
1212 const currentRemote = currentRemoteOrigin . split ( '\n' ) [ 0 ]
1313 const allConfigs = getAllUserConfigs ( )
1414 const currentConfig = getCurrentConfig ( )
15+ if ( ! currentConfig ) {
16+ console . log ( '未检测到当前 git 用户配置,请先设置 user.name 和 user.email' )
17+ return
18+ }
1519 const curMatchedItem : UserConfig | undefined = allConfigs . find ( ( config : UserConfig ) => currentRemote . includes ( config . origin ) )
1620 if ( curMatchedItem ) {
1721 if ( currentConfig . name === curMatchedItem . name && currentConfig . email === curMatchedItem . email ) {
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ alias: ${config.alias}
1313name: ${ config . name }
1414email: ${ config . email }
1515 `
16- if ( config . name === currentConfig . name && config . email === currentConfig . email ) {
16+ if ( currentConfig && config . name === currentConfig . name && config . email === currentConfig . email ) {
1717 console . log ( pc . green ( configStr ) ) ;
1818 } else {
1919 console . log ( configStr )
Original file line number Diff line number Diff line change @@ -20,6 +20,7 @@ export const isValidUserConfig = (configStr: string) => {
2020
2121export const isCurrentConfig = ( configStr : string ) => {
2222 const currentConfig = getCurrentConfig ( )
23+ if ( ! currentConfig ) return false
2324 return `${ currentConfig . name } <${ currentConfig . email } >` === configStr
2425}
2526
Original file line number Diff line number Diff line change @@ -29,11 +29,18 @@ export const getPkgJson = () => {
2929}
3030
3131export const getCurrentConfig = ( ) => {
32- const currentUserName = execSync ( 'git config --get user.name' ) . toString ( ) . trim ( )
33- const currentUserEmail = execSync ( 'git config --get user.email' ) . toString ( ) . trim ( )
34- return {
35- name : currentUserName ,
36- email : currentUserEmail
32+ try {
33+ const currentUserName = execSync ( 'git config --get user.name' ) . toString ( ) . trim ( )
34+ const currentUserEmail = execSync ( 'git config --get user.email' ) . toString ( ) . trim ( )
35+ if ( ! currentUserName || ! currentUserEmail ) {
36+ return null
37+ }
38+ return {
39+ name : currentUserName ,
40+ email : currentUserEmail
41+ }
42+ } catch {
43+ return null
3744 }
3845}
3946
You can’t perform that action at this time.
0 commit comments