@@ -34,69 +34,56 @@ const onPromptState = (state: {
34
34
let projectPath = "" ;
35
35
36
36
async function main ( ) {
37
-
38
37
const program = new Command ( )
39
38
. name ( packageJson . name )
40
39
. version ( packageJson . version , "-v, --version" , "display the version number" )
41
40
. usage ( `${ chalk . green ( "create <id>" ) } [options]` ) ;
42
41
43
-
44
42
program
45
43
. command ( "create <id>" )
46
44
. description ( "Create a new project based on the specified version id" )
47
45
. option ( "--dir <project-directory>" , "directory name" )
48
- . option (
49
- "--skip-code-package" ,
50
- "Skip installation of npm packages imported in the code"
51
- )
46
+ . option ( "--skip-code-package" , "Skip installation of npm packages imported in the code" )
52
47
. option ( "--main-only" , "Dumps API code only." )
53
48
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
54
49
. action ( async ( versionId , options ) => {
55
50
const online = await getOnline ( ) ;
56
51
57
52
if ( ! online ) {
58
- console . error (
59
- "You appear to be offline. Please check your internet connection and try again."
60
- ) ;
53
+ console . error ( "You appear to be offline. Please check your internet connection and try again." ) ;
61
54
process . exit ( 1 ) ;
62
55
}
63
56
64
57
const config = readConfig ( ) ;
65
58
66
59
if ( ! config ?. api_token ) {
67
- console . info ( chalk . yellow ( `Warning: To access a private application, set the token with ${ chalk . bold ( "@hanabi.rest/cli config set" ) } ` ) ) ;
60
+ console . info (
61
+ chalk . yellow ( `Warning: To access a private application, set the token with ${ chalk . bold ( "@hanabi.rest/cli config set" ) } ` ) ,
62
+ ) ;
68
63
console . info ( ) ;
69
64
}
70
65
71
66
if ( options . mainOnly ) {
72
67
const { source } = await getFiles ( versionId ) ;
68
+ if ( ! source ) {
69
+ console . error ( "Failed to fetch files. Your API may be in the process of being generated or may have failed to generate." ) ;
70
+ process . exit ( 1 ) ;
71
+ }
73
72
74
73
const fileExists = fs . existsSync ( path . join ( process . cwd ( ) , "index.ts" ) ) ;
75
74
76
75
if ( fileExists ) {
77
- console . info (
78
- "The index.ts file already exists. Please retry with a different name."
79
- ) ;
76
+ console . info ( "The index.ts file already exists. Please retry with a different name." ) ;
80
77
process . exit ( 1 ) ;
81
78
}
82
79
83
80
fs . writeFileSync ( path . join ( process . cwd ( ) , "index.ts" ) , source ) ;
84
- console . info (
85
- `${ chalk . green ( "Success!" ) } Created index.ts file in ${ chalk . green (
86
- process . cwd ( )
87
- ) } `
88
- ) ;
81
+ console . info ( `${ chalk . green ( "Success!" ) } Created index.ts file in ${ chalk . green ( process . cwd ( ) ) } ` ) ;
89
82
90
83
return ;
91
84
}
92
85
93
- const packageManager = options . useNpm
94
- ? "npm"
95
- : options . usePnpm
96
- ? "pnpm"
97
- : options . useYarn
98
- ? "yarn"
99
- : getPkgManager ( ) ;
86
+ const packageManager = options . useNpm ? "npm" : options . usePnpm ? "pnpm" : options . useYarn ? "yarn" : getPkgManager ( ) ;
100
87
101
88
projectPath = options . dir ;
102
89
@@ -108,9 +95,7 @@ async function main() {
108
95
message : "What is your project named?" ,
109
96
initial : "my-app" ,
110
97
validate : ( name ) => {
111
- const validation = validateNpmName (
112
- path . basename ( path . resolve ( name ) )
113
- ) ;
98
+ const validation = validateNpmName ( path . basename ( path . resolve ( name ) ) ) ;
114
99
if ( validation . valid ) {
115
100
return true ;
116
101
}
@@ -128,11 +113,7 @@ async function main() {
128
113
const validation = validateNpmName ( projectName ) ;
129
114
130
115
if ( ! validation . valid ) {
131
- console . error (
132
- `Could not create a project called ${ chalk . red (
133
- `"${ projectName } "`
134
- ) } because of npm naming restrictions:`
135
- ) ;
116
+ console . error ( `Could not create a project called ${ chalk . red ( `"${ projectName } "` ) } because of npm naming restrictions:` ) ;
136
117
137
118
for ( const p of validation . problems ) {
138
119
console . error ( `${ chalk . red ( chalk . bold ( "*" ) ) } ${ p } ` ) ;
@@ -144,9 +125,7 @@ async function main() {
144
125
const folderExists = fs . existsSync ( root ) ;
145
126
146
127
if ( folderExists ) {
147
- console . info (
148
- "The directory already exists. Please try again with a different name."
149
- ) ;
128
+ console . info ( "The directory already exists. Please try again with a different name." ) ;
150
129
process . exit ( 1 ) ;
151
130
}
152
131
@@ -159,35 +138,34 @@ async function main() {
159
138
} )
160
139
. allowUnknownOption ( ) ;
161
140
162
- const configCommand = program . command ( 'config' )
163
- . description ( 'Configure the CLI tool' ) ;
141
+ const configCommand = program . command ( "config" ) . description ( "Configure the CLI tool" ) ;
164
142
165
143
configCommand
166
- . command ( ' set' )
167
- . description ( ' Set a configuration option' )
168
- . option ( ' --api-key [key]' , ' Set the access token for authentication' )
144
+ . command ( " set" )
145
+ . description ( " Set a configuration option" )
146
+ . option ( " --api-key [key]" , " Set the access token for authentication" )
169
147
. action ( async ( options ) => {
170
148
let apiKey : string = options . apiKey ;
171
149
172
150
if ( ! apiKey ) {
173
- console . info ( chalk . blue ( "To get your access token, visit: https://hanabi.rest/settings/tokens" ) )
174
- console . info ( )
151
+ console . info ( chalk . blue ( "To get your access token, visit: https://hanabi.rest/settings/tokens" ) ) ;
152
+ console . info ( ) ;
175
153
176
154
const response = await prompts ( {
177
- type : ' password' ,
178
- name : ' key' ,
179
- message : ' Enter your Access token:' ,
155
+ type : " password" ,
156
+ name : " key" ,
157
+ message : " Enter your Access token:" ,
180
158
} ) ;
181
159
apiKey = response . key ;
182
160
}
183
161
184
162
if ( apiKey ) {
185
163
writeAuthConfigFile ( { api_token : apiKey } ) ;
186
164
187
- console . info ( )
188
- console . info ( chalk . green ( ' access token saved successfully.' ) ) ;
165
+ console . info ( ) ;
166
+ console . info ( chalk . green ( " access token saved successfully." ) ) ;
189
167
} else {
190
- console . error ( chalk . red ( ' access token is required.' ) ) ;
168
+ console . error ( chalk . red ( " access token is required." ) ) ;
191
169
}
192
170
} ) ;
193
171
0 commit comments