@@ -62,53 +62,66 @@ async function installDependencies(cwd: string, spinner: Ora) {
62
62
cwd,
63
63
stdio : 'inherit' ,
64
64
} ) ;
65
+ spinner . text = 'Dependencies installed successfully' ;
65
66
} catch ( error ) {
66
67
spinner . fail ( 'Failed to install dependencies' ) ;
67
68
handleError ( error ) ;
69
+ process . exit ( 1 ) ;
68
70
}
69
71
}
70
72
71
73
async function promptForConfig ( cwd : string ) {
72
74
const highlight = ( text : string ) => chalk . cyan ( text ) ;
73
75
74
- const options = await prompts ( [
75
- {
76
- type : 'text' ,
77
- name : 'components' ,
78
- message : `Configure the import alias for ${ highlight ( 'components' ) } :` ,
79
- initial : DEFAULT_COMPONENTS ,
80
- } ,
81
- {
82
- type : 'text' ,
83
- name : 'lib' ,
84
- message : `Configure the import alias for ${ highlight ( 'lib' ) } :` ,
85
- initial : DEFAULT_LIB ,
86
- } ,
87
- ] ) ;
88
-
89
- const config = rawConfigSchema . parse ( {
90
- aliases : {
91
- lib : options . lib ,
92
- components : options . components ,
93
- } ,
94
- } ) ;
76
+ try {
77
+ const options = await prompts ( [
78
+ {
79
+ type : 'text' ,
80
+ name : 'components' ,
81
+ message : `Configure the import alias for ${ highlight ( 'components' ) } :` ,
82
+ initial : DEFAULT_COMPONENTS ,
83
+ } ,
84
+ {
85
+ type : 'text' ,
86
+ name : 'lib' ,
87
+ message : `Configure the import alias for ${ highlight ( 'lib' ) } :` ,
88
+ initial : DEFAULT_LIB ,
89
+ } ,
90
+ ] ) ;
95
91
96
- const { proceed } = await prompts ( {
97
- type : 'confirm' ,
98
- name : 'proceed' ,
99
- message : `Write configuration to ${ highlight ( 'components.json' ) } . Proceed?` ,
100
- initial : true ,
101
- } ) ;
92
+ const components = options . components || DEFAULT_COMPONENTS ;
93
+ const lib = options . lib || DEFAULT_LIB ;
94
+
95
+ const config = rawConfigSchema . parse ( {
96
+ aliases : {
97
+ components,
98
+ lib,
99
+ } ,
100
+ } ) ;
101
+
102
+ const { proceed } = await prompts ( {
103
+ type : 'confirm' ,
104
+ name : 'proceed' ,
105
+ message : `Write configuration to ${ highlight ( 'components.json' ) } . Proceed?` ,
106
+ initial : true ,
107
+ } ) ;
108
+
109
+ if ( ! proceed ) {
110
+ logger . info ( 'Configuration cancelled.' ) ;
111
+ process . exit ( 0 ) ;
112
+ }
102
113
103
- if ( proceed ) {
104
114
logger . info ( '' ) ;
105
115
const spinner = ora ( `Writing components.json...` ) . start ( ) ;
106
116
const targetPath = path . resolve ( cwd , 'components.json' ) ;
107
117
await fs . writeFile ( targetPath , JSON . stringify ( config , null , 2 ) , 'utf8' ) ;
108
118
spinner . succeed ( ) ;
109
- }
110
119
111
- return await resolveConfigPaths ( cwd , config ) ;
120
+ return await resolveConfigPaths ( cwd , config ) ;
121
+ } catch ( error ) {
122
+ logger . error ( 'Failed to configure project.' ) ;
123
+ process . exit ( 1 ) ;
124
+ }
112
125
}
113
126
114
127
async function updateTsConfig ( cwd : string , config : any , spinner : Ora ) {
@@ -250,25 +263,33 @@ async function checkGitStatus(cwd: string) {
250
263
async function initializeProject ( cwd : string , overwrite : boolean ) {
251
264
const spinner = ora ( `Initializing project...` ) . start ( ) ;
252
265
253
- let config = await getConfig ( cwd ) ;
266
+ try {
267
+ let config = await getConfig ( cwd ) ;
254
268
255
- if ( ! config ) {
256
- config = await promptForConfig ( cwd ) ;
257
- }
269
+ if ( ! config ) {
270
+ spinner . stop ( ) ;
271
+ config = await promptForConfig ( cwd ) ;
272
+ spinner . start ( ) ;
273
+ }
258
274
259
- const templatesDir = path . dirname ( createRequire ( import . meta. url ) . resolve ( '@rnr/starter-base' ) ) ;
275
+ const templatesDir = path . dirname ( createRequire ( import . meta. url ) . resolve ( '@rnr/starter-base' ) ) ;
260
276
261
- await installDependencies ( cwd , spinner ) ;
262
- await updateTsConfig ( cwd , config , spinner ) ;
277
+ await installDependencies ( cwd , spinner ) ;
278
+ await updateTsConfig ( cwd , config , spinner ) ;
263
279
264
- spinner . text = 'Copying template files...' ;
265
- for ( const file of TEMPLATE_FILES ) {
266
- await copyTemplateFile ( file , templatesDir , cwd , spinner , overwrite ) ;
267
- }
280
+ spinner . text = 'Copying template files...' ;
281
+ for ( const file of TEMPLATE_FILES ) {
282
+ await copyTemplateFile ( file , templatesDir , cwd , spinner , overwrite ) ;
283
+ }
268
284
269
- await updateLayoutFile ( cwd , spinner ) ;
285
+ await updateLayoutFile ( cwd , spinner ) ;
270
286
271
- spinner . succeed ( 'Initialization completed successfully!' ) ;
287
+ spinner . succeed ( 'Initialization completed successfully!' ) ;
288
+ } catch ( error ) {
289
+ spinner . fail ( 'Initialization failed' ) ;
290
+ handleError ( error ) ;
291
+ process . exit ( 1 ) ;
292
+ }
272
293
}
273
294
274
295
export const init = new Command ( )
0 commit comments