11import { existsSync , promises as fs , rmSync } from 'node:fs'
2- import path from 'node:path'
32import process from 'node:process'
4- import chalk from 'chalk'
3+ import path from 'pathe'
4+ import { consola } from 'consola'
5+ import { colors } from 'consola/utils'
56import { Command } from 'commander'
6- import { execa } from 'execa'
77import ora from 'ora'
88import prompts from 'prompts'
9- import * as z from 'zod'
9+ import { z } from 'zod'
10+ import { addDependency , addDevDependency } from 'nypm'
1011import { transform } from '@/src/utils/transformers'
1112import { getConfig } from '@/src/utils/get-config'
12- import { getPackageManager } from '@/src/utils/get-package-manager'
1313import { handleError } from '@/src/utils/handle-error'
14- import { logger } from '@/src/utils/logger'
1514import {
1615 fetchTree ,
1716 getItemTargetPath ,
@@ -52,15 +51,15 @@ export const add = new Command()
5251 const cwd = path . resolve ( options . cwd )
5352
5453 if ( ! existsSync ( cwd ) ) {
55- logger . error ( `The path ${ cwd } does not exist. Please try again.` )
54+ consola . error ( `The path ${ cwd } does not exist. Please try again.` )
5655 process . exit ( 1 )
5756 }
5857
5958 const config = await getConfig ( cwd )
59+
6060 if ( ! config ) {
61- logger . warn (
62- `Configuration is missing. Please run ${ chalk . green ( 'init' ) } to create a components.json file.` ,
63- )
61+ consola . warn ( `Configuration is missing. Please run ${ colors . green ( 'init' ) } to create a components.json file.` )
62+
6463 process . exit ( 1 )
6564 }
6665
@@ -88,7 +87,7 @@ export const add = new Command()
8887 }
8988
9089 if ( ! selectedComponents ?. length ) {
91- logger . warn ( 'No components selected. Exiting.' )
90+ consola . warn ( 'No components selected. Exiting.' )
9291 process . exit ( 0 )
9392 }
9493
@@ -97,7 +96,7 @@ export const add = new Command()
9796 const baseColor = await getRegistryBaseColor ( config . tailwind . baseColor )
9897
9998 if ( ! payload . length ) {
100- logger . warn ( 'Selected components not found. Exiting.' )
99+ consola . warn ( 'Selected components not found. Exiting.' )
101100 process . exit ( 0 )
102101 }
103102
@@ -114,7 +113,6 @@ export const add = new Command()
114113 }
115114
116115 const spinner = ora ( 'Installing components...' ) . start ( )
117- const skippedDeps = new Set < string > ( )
118116 for ( const item of payload ) {
119117 spinner . text = `Installing ${ item . name } ...`
120118 const targetDir = getItemTargetPath (
@@ -144,8 +142,8 @@ export const add = new Command()
144142 } )
145143
146144 if ( ! overwrite ) {
147- logger . info (
148- `Skipped ${ item . name } . To overwrite, run with the ${ chalk . green (
145+ consola . info (
146+ `Skipped ${ item . name } . To overwrite, run with the ${ colors . green (
149147 '--overwrite' ,
150148 ) } flag.`,
151149 )
@@ -159,6 +157,20 @@ export const add = new Command()
159157 }
160158 }
161159
160+ // Install dependencies.
161+ await Promise . allSettled (
162+ [
163+ item . dependencies ?. length && await addDependency ( item . dependencies , {
164+ cwd,
165+ silent : true ,
166+ } ) ,
167+ item . devDependencies ?. length && await addDevDependency ( item . devDependencies , {
168+ cwd,
169+ silent : true ,
170+ } ) ,
171+ ] ,
172+ )
173+
162174 const componentDir = path . resolve ( targetDir , item . name )
163175 if ( ! existsSync ( componentDir ) )
164176 await fs . mkdir ( componentDir , { recursive : true } )
@@ -201,25 +213,6 @@ export const add = new Command()
201213
202214 await fs . writeFile ( filePath , content )
203215 }
204-
205- // Install dependencies.
206- if ( item . dependencies ?. length ) {
207- item . dependencies . forEach ( dep =>
208- skippedDeps . add ( dep ) ,
209- )
210-
211- const packageManager = await getPackageManager ( cwd )
212- await execa (
213- packageManager ,
214- [
215- packageManager === 'npm' ? 'install' : 'add' ,
216- ...item . dependencies ,
217- ] ,
218- {
219- cwd,
220- } ,
221- )
222- }
223216 }
224217 spinner . succeed ( 'Done.' )
225218 }
0 commit comments