@@ -7,7 +7,6 @@ import path from 'path';
7
7
import { z } from 'zod' ;
8
8
import { handleError } from '@/src/utils/handle-error' ;
9
9
import { logger } from '@/src/utils/logger' ;
10
- import { fileURLToPath } from 'url' ;
11
10
import chalk from 'chalk' ;
12
11
import prompts from 'prompts' ;
13
12
import glob from 'fast-glob' ;
@@ -21,9 +20,6 @@ import {
21
20
DEFAULT_LIB ,
22
21
} from '@/src/utils/get-config' ;
23
22
24
- const filePath = fileURLToPath ( import . meta. url ) ;
25
- const fileDir = path . dirname ( filePath ) ;
26
-
27
23
const initOptionsSchema = z . object ( {
28
24
cwd : z . string ( ) ,
29
25
overwrite : z . boolean ( ) ,
@@ -106,41 +102,55 @@ async function promptForConfig(cwd: string) {
106
102
initial : true ,
107
103
} ) ;
108
104
109
- if ( ! proceed ) {
110
- logger . info ( 'Configuration cancelled.' ) ;
111
- process . exit ( 0 ) ;
105
+ if ( proceed ) {
106
+ logger . info ( '' ) ;
107
+ const spinner = ora ( `Writing components.json...` ) . start ( ) ;
108
+ const targetPath = path . resolve ( cwd , 'components.json' ) ;
109
+ await fs . writeFile ( targetPath , JSON . stringify ( config , null , 2 ) , 'utf8' ) ;
110
+ spinner . succeed ( ) ;
112
111
}
113
112
114
- logger . info ( '' ) ;
115
- const spinner = ora ( `Writing components.json...` ) . start ( ) ;
116
- const targetPath = path . resolve ( cwd , 'components.json' ) ;
117
- await fs . writeFile ( targetPath , JSON . stringify ( config , null , 2 ) , 'utf8' ) ;
118
- spinner . succeed ( ) ;
119
-
120
113
return await resolveConfigPaths ( cwd , config ) ;
121
114
} catch ( error ) {
122
115
logger . error ( 'Failed to configure project.' ) ;
123
116
process . exit ( 1 ) ;
124
117
}
125
118
}
126
119
120
+ const NON_PATH_ALIAS_BASES = [ '' , '.' , '/' ] ;
121
+
127
122
async function updateTsConfig ( cwd : string , config : any , spinner : Ora ) {
128
123
try {
129
- spinner . text = 'Configuring path aliases...' ;
130
124
const tsconfigPath = path . join ( cwd , 'tsconfig.json' ) ;
131
125
const tsconfig = existsSync ( tsconfigPath )
132
126
? JSON . parse ( await fs . readFile ( tsconfigPath , 'utf8' ) )
133
127
: { } ;
134
128
135
129
const componentBase = config . aliases . components . split ( '/' ) [ 0 ] ;
136
130
const libBase = config . aliases . lib . split ( '/' ) [ 0 ] ;
137
- const basePath = componentBase === libBase ? componentBase : '@' ;
131
+
132
+ if ( NON_PATH_ALIAS_BASES . includes ( componentBase ) || NON_PATH_ALIAS_BASES . includes ( libBase ) ) {
133
+ return ;
134
+ }
135
+
136
+ const tsconfigPaths = tsconfig . compilerOptions ?. paths ?? { } ;
137
+
138
+ if (
139
+ tsconfigPaths [ `${ componentBase } /*` ] ?. [ 0 ] === '*' &&
140
+ tsconfigPaths [ `${ libBase } /*` ] ?. [ 0 ] === '*'
141
+ ) {
142
+ spinner . succeed ( 'Path aliases already configured' ) ;
143
+ return ;
144
+ }
145
+
146
+ spinner . text = 'Updating path aliases...' ;
138
147
139
148
tsconfig . compilerOptions = {
140
149
...tsconfig . compilerOptions ,
141
150
baseUrl : '.' ,
142
151
paths : {
143
- [ `${ basePath } /*` ] : [ '*' ] ,
152
+ [ `${ componentBase } /*` ] : [ '*' ] ,
153
+ [ `${ libBase } /*` ] : [ '*' ] ,
144
154
...tsconfig . compilerOptions ?. paths ,
145
155
} ,
146
156
} ;
@@ -192,7 +202,6 @@ async function copyTemplateFile(
192
202
193
203
async function updateLayoutFile ( cwd : string , spinner : Ora ) {
194
204
try {
195
- spinner . text = 'Updating layout file...' ;
196
205
const layoutFiles = await glob (
197
206
[ 'app/_layout.{ts,tsx,js,jsx}' , '(app)/_layout.{ts,tsx,js,jsx}' ] ,
198
207
{
@@ -202,14 +211,15 @@ async function updateLayoutFile(cwd: string, spinner: Ora) {
202
211
) ;
203
212
204
213
if ( ! layoutFiles . length ) {
205
- spinner . warn ( 'No _layout file found in app directory ' ) ;
214
+ spinner . warn ( 'Could not find the root _layout file ' ) ;
206
215
return ;
207
216
}
208
217
209
218
const layoutPath = path . join ( cwd , layoutFiles [ 0 ] ) ;
210
219
const content = await fs . readFile ( layoutPath , 'utf8' ) ;
211
220
212
221
if ( ! content . includes ( 'import "../global.css"' ) ) {
222
+ spinner . text = 'Updating layout file...' ;
213
223
await fs . writeFile ( layoutPath , `import "../global.css";\n${ content } ` ) ;
214
224
spinner . succeed ( `Updated ${ layoutFiles [ 0 ] } with global CSS import` ) ;
215
225
}
@@ -277,7 +287,7 @@ async function initializeProject(cwd: string, overwrite: boolean) {
277
287
await installDependencies ( cwd , spinner ) ;
278
288
await updateTsConfig ( cwd , config , spinner ) ;
279
289
280
- spinner . text = 'Copying template files...' ;
290
+ spinner . text = 'Adding config and utility files...' ;
281
291
for ( const file of TEMPLATE_FILES ) {
282
292
await copyTemplateFile ( file , templatesDir , cwd , spinner , overwrite ) ;
283
293
}
@@ -294,7 +304,7 @@ async function initializeProject(cwd: string, overwrite: boolean) {
294
304
295
305
export const init = new Command ( )
296
306
. name ( 'init' )
297
- . description ( 'Initialize the React Native project with required configuration ' )
307
+ . description ( 'Initialize the required configuration for your React Native project ' )
298
308
. option (
299
309
'-c, --cwd <cwd>' ,
300
310
'the working directory. defaults to the current directory.' ,
0 commit comments