@@ -71,76 +71,90 @@ export const init = new Command()
71
71
} ) ;
72
72
73
73
async function validateProjectDirectory ( cwd : string ) {
74
- if ( ! existsSync ( cwd ) || ! existsSync ( path . join ( cwd , 'package.json' ) ) ) {
75
- const { proceed } = await prompts ( {
76
- type : 'confirm' ,
77
- name : 'proceed' ,
78
- message : 'No package.json found. Would you like to create a new project?' ,
74
+ if ( existsSync ( cwd ) && existsSync ( path . join ( cwd , 'package.json' ) ) ) {
75
+ const { option } = await prompts ( {
76
+ type : 'select' ,
77
+ name : 'option' ,
78
+ message :
79
+ 'Package.json found. How would you like to proceed? (Selecting "Cancel" will exit the process)' ,
80
+ choices : [
81
+ { title : 'Automatically configure your existing project' , value : 'existing-project' } ,
82
+ { title : 'Init new project' , value : 'new-project' } ,
83
+ { title : 'Cancel' , value : 'cancel' } ,
84
+ ] ,
79
85
initial : false ,
80
86
} ) ;
81
87
82
- if ( ! proceed ) {
83
- logger . info ( 'Initialization cancelled.' ) ;
88
+ if ( option === 'cancel' ) {
89
+ logger . info ( 'Installation cancelled.' ) ;
84
90
process . exit ( 0 ) ;
85
91
}
86
92
87
- const { projectName } = await prompts ( {
88
- type : 'text' ,
89
- name : 'projectName' ,
90
- message : `What is the name of your project?` ,
91
- initial : 'starter-base' ,
92
- } ) ;
93
-
94
- const spinner = ora ( `Initializing ${ projectName } ...` ) . start ( ) ;
95
-
96
- const projectPath = path . join ( cwd , projectName ) ;
97
- if ( ! existsSync ( projectPath ) ) {
98
- await fs . mkdir ( projectPath , { recursive : true } ) ;
93
+ if ( option === 'existing-project' ) {
94
+ return ;
99
95
}
96
+ }
97
+
98
+ const { projectName } = await prompts ( {
99
+ type : 'text' ,
100
+ name : 'projectName' ,
101
+ message : `What is the name of your project?` ,
102
+ initial : 'starter-base' ,
103
+ } ) ;
100
104
101
- await copyFolder ( path . join ( fileDir , '../__generated/starter-base' ) , projectPath ) ;
105
+ const { packageManager } = await prompts ( {
106
+ type : 'select' ,
107
+ name : 'packageManager' ,
108
+ message : 'Which package manager would you like to use?' ,
109
+ choices : [
110
+ { title : 'npm' , value : 'npm' } ,
111
+ { title : 'yarn' , value : 'yarn' } ,
112
+ { title : 'pnpm' , value : 'pnpm' } ,
113
+ { title : 'bun' , value : 'bun' } ,
114
+ ] ,
115
+ } ) ;
102
116
103
- await Promise . all ( [
104
- replaceAllInJsonFile ( path . join ( projectPath , 'app.json' ) , 'starter-base' , projectName ) ,
105
- replaceAllInJsonFile (
106
- path . join ( projectPath , 'package.json' ) ,
107
- '@rnr/starter-base' ,
108
- projectName
109
- ) ,
110
- ] ) ;
117
+ const spinner = ora ( `Initializing ${ projectName } ...` ) . start ( ) ;
111
118
112
- spinner . stop ( ) ;
113
- const { packageManager } = await prompts ( {
114
- type : 'select' ,
115
- name : 'packageManager' ,
116
- message : 'Which package manager would you like to use?' ,
117
- choices : [
118
- { title : 'npm' , value : 'npm' } ,
119
- { title : 'yarn' , value : 'yarn' } ,
120
- { title : 'pnpm' , value : 'pnpm' } ,
121
- { title : 'bun' , value : 'bun' } ,
122
- ] ,
123
- } ) ;
119
+ const projectPath = path . join ( cwd , projectName ) ;
120
+ if ( ! existsSync ( projectPath ) ) {
121
+ await fs . mkdir ( projectPath , { recursive : true } ) ;
122
+ }
124
123
125
- spinner . start ( 'Installing dependencies...' ) ;
126
- await execa ( packageManager , [ 'install' ] , {
127
- cwd : projectPath ,
128
- } ) ;
129
- spinner . text = 'Verifying and updating any invalid package versions if needed...' ;
130
- await execa ( 'npx' , [ 'expo' , 'install' , '--fix' ] , {
131
- cwd : projectPath ,
132
- } ) ;
124
+ const filesToIgnore = [ ] ;
133
125
134
- spinner . succeed ( 'New project initialized successfully!' ) ;
135
- console . log ( `\nTo get started, run the following commands:\n` ) ;
136
- console . log ( chalk . cyan ( `cd ${ projectName } ` ) ) ;
137
- console . log (
138
- chalk . cyan (
139
- `${ packageManager } ${ packageManager === 'npm' || packageManager === 'bun' ? 'run ' : '' } dev`
140
- )
141
- ) ;
142
- process . exit ( 0 ) ;
126
+ if ( packageManager !== 'pnpm' ) {
127
+ filesToIgnore . push ( 'npmrc-template' ) ;
143
128
}
129
+
130
+ await copyFolder ( path . join ( fileDir , '../__generated/starter-base' ) , projectPath , {
131
+ ignore : filesToIgnore ,
132
+ renameTemplateFiles : true ,
133
+ } ) ;
134
+
135
+ await Promise . all ( [
136
+ replaceAllInJsonFile ( path . join ( projectPath , 'app.json' ) , 'starter-base' , projectName ) ,
137
+ replaceAllInJsonFile ( path . join ( projectPath , 'package.json' ) , '@rnr/starter-base' , projectName ) ,
138
+ ] ) ;
139
+
140
+ spinner . start ( 'Installing dependencies...' ) ;
141
+ await execa ( packageManager , [ 'install' ] , {
142
+ cwd : projectPath ,
143
+ } ) ;
144
+ spinner . text = 'Verifying and updating any invalid package versions if needed...' ;
145
+ await execa ( 'npx' , [ 'expo' , 'install' , '--fix' ] , {
146
+ cwd : projectPath ,
147
+ } ) ;
148
+
149
+ spinner . succeed ( 'New project initialized successfully!' ) ;
150
+ console . log ( `\nTo get started, run the following commands:\n` ) ;
151
+ console . log ( chalk . cyan ( `cd ${ projectName } ` ) ) ;
152
+ console . log (
153
+ chalk . cyan (
154
+ `${ packageManager } ${ packageManager === 'npm' || packageManager === 'bun' ? 'run ' : '' } dev`
155
+ )
156
+ ) ;
157
+ process . exit ( 0 ) ;
144
158
}
145
159
146
160
async function replaceAllInJsonFile ( path : string , searchValue : string , replaceValue : string ) {
0 commit comments