@@ -11,6 +11,7 @@ import {
1111 type Config ,
1212} from "@/src/utils/get-config"
1313import { getPackageManager } from "@/src/utils/get-package-manager"
14+ import { getProjectConfig , preFlight } from "@/src/utils/get-project-info"
1415import { handleError } from "@/src/utils/handle-error"
1516import { logger } from "@/src/utils/logger"
1617import {
@@ -39,12 +40,14 @@ const PROJECT_DEPENDENCIES = [
3940const initOptionsSchema = z . object ( {
4041 cwd : z . string ( ) ,
4142 yes : z . boolean ( ) ,
43+ defaults : z . boolean ( ) ,
4244} )
4345
4446export const init = new Command ( )
4547 . name ( "init" )
4648 . description ( "initialize your project and install dependencies" )
4749 . option ( "-y, --yes" , "skip confirmation prompt." , false )
50+ . option ( "-d, --defaults," , "use default configuration." , false )
4851 . option (
4952 "-c, --cwd <cwd>" ,
5053 "the working directory. defaults to the current directory." ,
@@ -61,15 +64,28 @@ export const init = new Command()
6164 process . exit ( 1 )
6265 }
6366
64- // Read config.
65- const existingConfig = await getConfig ( cwd )
66- const config = await promptForConfig ( cwd , existingConfig , options . yes )
67+ preFlight ( cwd )
6768
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+ }
6983
7084 logger . info ( "" )
7185 logger . info (
72- `${ chalk . green ( "Success!" ) } Project initialization completed.`
86+ `${ chalk . green (
87+ "Success!"
88+ ) } Project initialization completed. You may now add components.`
7389 )
7490 logger . info ( "" )
7591 } catch ( error ) {
@@ -213,6 +229,81 @@ export async function promptForConfig(
213229 return await resolveConfigPaths ( cwd , config )
214230}
215231
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+
216307export async function runInit ( cwd : string , config : Config ) {
217308 const spinner = ora ( `Initializing project...` ) ?. start ( )
218309
0 commit comments