@@ -11,6 +11,7 @@ import {
11
11
type Config ,
12
12
} from "@/src/utils/get-config"
13
13
import { getPackageManager } from "@/src/utils/get-package-manager"
14
+ import { getProjectConfig , preFlight } from "@/src/utils/get-project-info"
14
15
import { handleError } from "@/src/utils/handle-error"
15
16
import { logger } from "@/src/utils/logger"
16
17
import {
@@ -39,12 +40,14 @@ const PROJECT_DEPENDENCIES = [
39
40
const initOptionsSchema = z . object ( {
40
41
cwd : z . string ( ) ,
41
42
yes : z . boolean ( ) ,
43
+ defaults : z . boolean ( ) ,
42
44
} )
43
45
44
46
export const init = new Command ( )
45
47
. name ( "init" )
46
48
. description ( "initialize your project and install dependencies" )
47
49
. option ( "-y, --yes" , "skip confirmation prompt." , false )
50
+ . option ( "-d, --defaults," , "use default configuration." , false )
48
51
. option (
49
52
"-c, --cwd <cwd>" ,
50
53
"the working directory. defaults to the current directory." ,
@@ -61,15 +64,28 @@ export const init = new Command()
61
64
process . exit ( 1 )
62
65
}
63
66
64
- // Read config.
65
- const existingConfig = await getConfig ( cwd )
66
- const config = await promptForConfig ( cwd , existingConfig , options . yes )
67
+ preFlight ( cwd )
67
68
68
- await runInit ( cwd , config )
69
+ const projectConfig = await getProjectConfig ( cwd )
70
+ if ( projectConfig ) {
71
+ const config = await promptForMinimalConfig (
72
+ cwd ,
73
+ projectConfig ,
74
+ opts . defaults
75
+ )
76
+ await runInit ( cwd , config )
77
+ } else {
78
+ // Read config.
79
+ const existingConfig = await getConfig ( cwd )
80
+ const config = await promptForConfig ( cwd , existingConfig , options . yes )
81
+ await runInit ( cwd , config )
82
+ }
69
83
70
84
logger . info ( "" )
71
85
logger . info (
72
- `${ chalk . green ( "Success!" ) } Project initialization completed.`
86
+ `${ chalk . green (
87
+ "Success!"
88
+ ) } Project initialization completed. You may now add components.`
73
89
)
74
90
logger . info ( "" )
75
91
} catch ( error ) {
@@ -213,6 +229,81 @@ export async function promptForConfig(
213
229
return await resolveConfigPaths ( cwd , config )
214
230
}
215
231
232
+ export async function promptForMinimalConfig (
233
+ cwd : string ,
234
+ defaultConfig : Config ,
235
+ defaults = false
236
+ ) {
237
+ const highlight = ( text : string ) => chalk . cyan ( text )
238
+ let style = defaultConfig . style
239
+ let baseColor = defaultConfig . tailwind . baseColor
240
+ let cssVariables = defaultConfig . tailwind . cssVariables
241
+
242
+ if ( ! defaults ) {
243
+ const styles = await getRegistryStyles ( )
244
+ const baseColors = await getRegistryBaseColors ( )
245
+
246
+ const options = await prompts ( [
247
+ {
248
+ type : "select" ,
249
+ name : "style" ,
250
+ message : `Which ${ highlight ( "style" ) } would you like to use?` ,
251
+ choices : styles . map ( ( style ) => ( {
252
+ title : style . label ,
253
+ value : style . name ,
254
+ } ) ) ,
255
+ } ,
256
+ {
257
+ type : "select" ,
258
+ name : "tailwindBaseColor" ,
259
+ message : `Which color would you like to use as ${ highlight (
260
+ "base color"
261
+ ) } ?`,
262
+ choices : baseColors . map ( ( color ) => ( {
263
+ title : color . label ,
264
+ value : color . name ,
265
+ } ) ) ,
266
+ } ,
267
+ {
268
+ type : "toggle" ,
269
+ name : "tailwindCssVariables" ,
270
+ message : `Would you like to use ${ highlight (
271
+ "CSS variables"
272
+ ) } for colors?`,
273
+ initial : defaultConfig ?. tailwind . cssVariables ,
274
+ active : "yes" ,
275
+ inactive : "no" ,
276
+ } ,
277
+ ] )
278
+
279
+ style = options . style
280
+ baseColor = options . tailwindBaseColor
281
+ cssVariables = options . tailwindCssVariables
282
+ }
283
+
284
+ const config = rawConfigSchema . parse ( {
285
+ $schema : defaultConfig ?. $schema ,
286
+ style,
287
+ tailwind : {
288
+ ...defaultConfig ?. tailwind ,
289
+ baseColor,
290
+ cssVariables,
291
+ } ,
292
+ rsc : defaultConfig ?. rsc ,
293
+ tsx : defaultConfig ?. tsx ,
294
+ aliases : defaultConfig ?. aliases ,
295
+ } )
296
+
297
+ // Write to file.
298
+ logger . info ( "" )
299
+ const spinner = ora ( `Writing components.json...` ) . start ( )
300
+ const targetPath = path . resolve ( cwd , "components.json" )
301
+ await fs . writeFile ( targetPath , JSON . stringify ( config , null , 2 ) , "utf8" )
302
+ spinner . succeed ( )
303
+
304
+ return await resolveConfigPaths ( cwd , config )
305
+ }
306
+
216
307
export async function runInit ( cwd : string , config : Config ) {
217
308
const spinner = ora ( `Initializing project...` ) ?. start ( )
218
309
0 commit comments