@@ -13,6 +13,13 @@ import prompts from 'prompts';
13
13
import glob from 'fast-glob' ;
14
14
import { createRequire } from 'module' ;
15
15
import { execSync } from 'child_process' ;
16
+ import {
17
+ getConfig ,
18
+ rawConfigSchema ,
19
+ resolveConfigPaths ,
20
+ DEFAULT_COMPONENTS ,
21
+ DEFAULT_LIB ,
22
+ } from '@/src/utils/get-config' ;
16
23
17
24
const filePath = fileURLToPath ( import . meta. url ) ;
18
25
const fileDir = path . dirname ( filePath ) ;
@@ -61,19 +68,66 @@ async function installDependencies(cwd: string, spinner: Ora) {
61
68
}
62
69
}
63
70
64
- async function updateTsConfig ( cwd : string , spinner : Ora ) {
71
+ async function promptForConfig ( cwd : string ) {
72
+ const highlight = ( text : string ) => chalk . cyan ( text ) ;
73
+
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
+ } ) ;
95
+
96
+ const { proceed } = await prompts ( {
97
+ type : 'confirm' ,
98
+ name : 'proceed' ,
99
+ message : `Write configuration to ${ highlight ( 'components.json' ) } . Proceed?` ,
100
+ initial : true ,
101
+ } ) ;
102
+
103
+ if ( proceed ) {
104
+ logger . info ( '' ) ;
105
+ const spinner = ora ( `Writing components.json...` ) . start ( ) ;
106
+ const targetPath = path . resolve ( cwd , 'components.json' ) ;
107
+ await fs . writeFile ( targetPath , JSON . stringify ( config , null , 2 ) , 'utf8' ) ;
108
+ spinner . succeed ( ) ;
109
+ }
110
+
111
+ return await resolveConfigPaths ( cwd , config ) ;
112
+ }
113
+
114
+ async function updateTsConfig ( cwd : string , config : any , spinner : Ora ) {
65
115
try {
66
116
spinner . text = 'Configuring path aliases...' ;
67
117
const tsconfigPath = path . join ( cwd , 'tsconfig.json' ) ;
68
118
const tsconfig = existsSync ( tsconfigPath )
69
119
? JSON . parse ( await fs . readFile ( tsconfigPath , 'utf8' ) )
70
120
: { } ;
71
121
122
+ const componentBase = config . aliases . components . split ( '/' ) [ 0 ] ;
123
+ const libBase = config . aliases . lib . split ( '/' ) [ 0 ] ;
124
+ const basePath = componentBase === libBase ? componentBase : '@' ;
125
+
72
126
tsconfig . compilerOptions = {
73
127
...tsconfig . compilerOptions ,
74
128
baseUrl : '.' ,
75
129
paths : {
76
- '~/*' : [ '*' ] ,
130
+ [ ` ${ basePath } /*` ] : [ '*' ] ,
77
131
...tsconfig . compilerOptions ?. paths ,
78
132
} ,
79
133
} ;
@@ -195,7 +249,9 @@ async function validateProjectDirectory(cwd: string) {
195
249
}
196
250
197
251
if ( ! existsSync ( path . join ( cwd , 'package.json' ) ) ) {
198
- logger . error ( 'No package.json found. Please run this command in a React Native project directory.' ) ;
252
+ logger . error (
253
+ 'No package.json found. Please run this command in a React Native project directory.'
254
+ ) ;
199
255
process . exit ( 1 ) ;
200
256
}
201
257
}
@@ -205,7 +261,8 @@ async function checkGitStatus(cwd: string) {
205
261
const { proceed } = await prompts ( {
206
262
type : 'confirm' ,
207
263
name : 'proceed' ,
208
- message : 'The Git repository is dirty (uncommitted changes). It is recommended to commit your changes before proceeding. Do you want to continue?' ,
264
+ message :
265
+ 'The Git repository is dirty (uncommitted changes). It is recommended to commit your changes before proceeding. Do you want to continue?' ,
209
266
initial : false ,
210
267
} ) ;
211
268
@@ -218,12 +275,17 @@ async function checkGitStatus(cwd: string) {
218
275
219
276
async function initializeProject ( cwd : string , overwrite : boolean ) {
220
277
const spinner = ora ( `Initializing project...` ) . start ( ) ;
221
- const templatesDir = path . dirname (
222
- createRequire ( import . meta. url ) . resolve ( '@rnr/starter-base' )
223
- ) ;
278
+
279
+ let config = await getConfig ( cwd ) ;
280
+
281
+ if ( ! config ) {
282
+ config = await promptForConfig ( cwd ) ;
283
+ }
284
+
285
+ const templatesDir = path . dirname ( createRequire ( import . meta. url ) . resolve ( '@rnr/starter-base' ) ) ;
224
286
225
287
await installDependencies ( cwd , spinner ) ;
226
- await updateTsConfig ( cwd , spinner ) ;
288
+ await updateTsConfig ( cwd , config , spinner ) ;
227
289
228
290
spinner . text = 'Copying template files...' ;
229
291
for ( const file of TEMPLATE_FILES ) {
0 commit comments