1
1
#!/usr/bin/env node
2
2
import arg from "arg" ;
3
3
import chalk from "chalk" ;
4
- import { fileURLToPath } from 'url' ;
4
+ import { fileURLToPath } from "url" ;
5
+ import { dirname , join } from "path" ;
6
+ import { readFileSync } from "fs" ;
7
+ import boxen from "boxen" ;
5
8
import { start } from "../src/commands/start" ;
6
9
import { reset } from "../src/commands/reset" ;
7
10
import { importKey } from "../src/commands/import" ;
11
+ import { GitKeyKitError , GitKeyKitCodes } from "../src/gitkeykitCodes" ;
8
12
import createLogger from "../src/utils/logger" ;
9
- import boxen from 'boxen' ;
10
- import { GitKeyKitCodes } from "../src/gitkeykitCodes" ;
11
- import { dirname , join } from "path" ;
12
- import { readFileSync } from "fs" ;
13
13
14
- process . on ( "SIGINT" , ( ) => process . exit ( GitKeyKitCodes . SUCCESS ) ) ;
15
- process . on ( "SIGTERM" , ( ) => process . exit ( GitKeyKitCodes . SUCCESS ) ) ;
14
+ const logger = createLogger ( "bin" ) ;
16
15
17
16
const __filename = fileURLToPath ( import . meta. url ) ;
18
17
const __dirname = dirname ( __filename ) ;
19
- const packageJson = JSON . parse (
20
- readFileSync ( join ( __dirname , '../package.json' ) , 'utf8' )
21
- ) ;
18
+ const packageJson = JSON . parse ( readFileSync ( join ( __dirname , "../package.json" ) , "utf8" ) ) ;
22
19
const { version } = packageJson ;
23
20
24
- const logger = createLogger ( "bin" ) ;
25
-
26
21
function usage ( ) {
27
22
console . log ( "\n" ) ;
28
- console . log ( chalk . blueBright ( boxen ( ' GitKeyKit - Simplify PGP key🔑 setup and signing commits on Linux and Windows machines.' , { padding : 1 , borderStyle : ' round' } ) ) ) ;
23
+ console . log ( chalk . blueBright ( boxen ( " GitKeyKit - Simplify PGP key🔑 setup and signing commits on Linux and Windows machines." , { padding : 1 , borderStyle : " round" } ) ) ) ;
29
24
console . log ( chalk . whiteBright ( "Usage: gitkeykit\n" ) ) ;
30
25
console . log ( chalk . whiteBright ( "Options:" ) ) ;
31
26
console . log ( chalk . blueBright ( "--reset\t\t\tReset Git and GPG configurations" ) ) ;
@@ -43,85 +38,90 @@ function usage() {
43
38
console . log ( "\n" ) ;
44
39
}
45
40
46
- async function handleImport ( keyPath : string ) : Promise < number > {
41
+ async function handleImport ( keyPath : string ) : Promise < void > {
47
42
try {
48
43
await importKey ( keyPath ) ;
49
44
logger . log ( `Imported key from ${ keyPath } ` ) ;
50
45
await start ( ) ;
51
- return GitKeyKitCodes . SUCCESS ;
52
46
} catch ( error ) {
53
- console . error ( `Error importing key from ${ keyPath } :` , error ) ;
54
- return GitKeyKitCodes . ERR_KEY_IMPORT ;
47
+ if ( error instanceof GitKeyKitError ) {
48
+ throw error ;
49
+ }
50
+ throw new GitKeyKitError ( `Failed to import key from ${ keyPath } ` , GitKeyKitCodes . KEY_IMPORT_ERROR , error ) ;
55
51
}
56
52
}
57
53
58
- async function handleReset ( ) : Promise < number > {
54
+ async function handleReset ( ) : Promise < void > {
59
55
try {
60
- reset ( ) ;
61
- return GitKeyKitCodes. SUCCESS ;
62
- } catch ( error : any ) {
63
- logger . warning ( ( error as Error ) . message ) ;
64
- console . log ( ) ;
65
- usage ( ) ;
66
- return GitKeyKitCodes . ERR_GIT_CONFIG_RESET ;
56
+ await reset ( ) ;
57
+ } catch ( error ) {
58
+ if ( error instanceof GitKeyKitError ) {
59
+ throw error ;
60
+ }
61
+ throw new GitKeyKitError ( "Failed to reset configurations" , GitKeyKitCodes . GIT_CONFIG_RESET_ERROR , error ) ;
67
62
}
68
63
}
69
64
70
- async function main ( ) : Promise < number > {
65
+ async function main ( ) : Promise < void > {
71
66
try {
72
67
const args = arg ( {
73
68
"--reset" : Boolean ,
74
69
"--help" : Boolean ,
75
70
"--import" : String ,
76
- "--version" : Boolean
71
+ "--version" : Boolean ,
77
72
} ) ;
78
73
79
74
logger . debug ( "Received args" , args ) ;
80
75
81
76
if ( Object . keys ( args ) . length === 1 ) {
82
77
await start ( ) ;
83
- return GitKeyKitCodes . SUCCESS ;
78
+ return ;
84
79
}
85
80
86
81
if ( args [ "--reset" ] ) {
87
- return handleReset ( ) ;
82
+ await handleReset ( ) ;
83
+ return ;
88
84
}
89
85
90
86
if ( args [ "--help" ] ) {
91
87
usage ( ) ;
92
- return GitKeyKitCodes . SUCCESS ;
88
+ return ;
93
89
}
94
90
95
91
if ( args [ "--import" ] ) {
96
92
const keyPath = args [ "--import" ] ;
97
- return handleImport ( keyPath ) ;
93
+ await handleImport ( keyPath ) ;
94
+ return ;
98
95
}
99
96
100
97
if ( args [ "--version" ] ) {
101
98
console . log ( `v${ version } ` ) ;
102
- return GitKeyKitCodes . SUCCESS ;
99
+ return ;
103
100
}
104
101
105
102
usage ( ) ;
106
- return GitKeyKitCodes. ERR_INVALID_ARGS ;
107
- } catch ( error : any ) {
108
- if ( error ?. code === 'ARG_UNKNOWN_OPTION' ) {
103
+ } catch ( error ) {
104
+ if ( error instanceof arg . ArgError && error ?. code === "ARG_UNKNOWN_OPTION" ) {
109
105
logger . error ( `Invalid argument: ${ error . message } ` ) ;
110
- console . log ( ' ------' ) ;
106
+ console . log ( " ------" ) ;
111
107
usage ( ) ;
112
- return GitKeyKitCodes . ERR_INVALID_ARGS ;
108
+ process . exit ( 1 ) ;
109
+ }
110
+
111
+ if ( error instanceof GitKeyKitError ) {
112
+ logger . error ( `Error: ${ error . message } (${ error . code } )` ) ;
113
+ if ( error . details ) {
114
+ logger . debug ( "Error details:" , error . details ) ;
115
+ }
116
+ process . exit ( 1 ) ;
113
117
}
114
-
115
- // Handle any other unexpected errors
116
- logger . error ( 'An unexpected error occurred:' , error ) ;
117
- return GitKeyKitCodes . ERR_INVALID_ARGS ;
118
+
119
+ logger . error ( "An unexpected error occurred:" , error ) ;
120
+ process . exit ( 1 ) ;
118
121
}
119
122
}
120
123
121
- // Execute and handle exit codes
122
- main ( )
123
- . then ( exitCode => process . exit ( exitCode ) )
124
- . catch ( error => {
125
- console . error ( 'Unexpected error:' , error ) ;
126
- process . exit ( 1 ) ;
127
- } ) ;
124
+ main ( ) . catch ( ( error ) => {
125
+ logger . error ( "Fatal error:" , error ) ;
126
+ process . exit ( 1 ) ;
127
+ } ) ;
0 commit comments