1
1
import { existsSync , promises as fs , rmSync } from 'node:fs'
2
- import path from 'node:path'
3
2
import 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'
5
6
import { Command } from 'commander'
6
- import { execa } from 'execa'
7
7
import ora from 'ora'
8
8
import prompts from 'prompts'
9
- import * as z from 'zod'
9
+ import { z } from 'zod'
10
+ import { addDependency , addDevDependency } from 'nypm'
10
11
import { transform } from '@/src/utils/transformers'
11
12
import { getConfig } from '@/src/utils/get-config'
12
- import { getPackageManager } from '@/src/utils/get-package-manager'
13
13
import { handleError } from '@/src/utils/handle-error'
14
- import { logger } from '@/src/utils/logger'
15
14
import {
16
15
fetchTree ,
17
16
getItemTargetPath ,
@@ -52,15 +51,15 @@ export const add = new Command()
52
51
const cwd = path . resolve ( options . cwd )
53
52
54
53
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.` )
56
55
process . exit ( 1 )
57
56
}
58
57
59
58
const config = await getConfig ( cwd )
59
+
60
60
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
+
64
63
process . exit ( 1 )
65
64
}
66
65
@@ -88,7 +87,7 @@ export const add = new Command()
88
87
}
89
88
90
89
if ( ! selectedComponents ?. length ) {
91
- logger . warn ( 'No components selected. Exiting.' )
90
+ consola . warn ( 'No components selected. Exiting.' )
92
91
process . exit ( 0 )
93
92
}
94
93
@@ -97,7 +96,7 @@ export const add = new Command()
97
96
const baseColor = await getRegistryBaseColor ( config . tailwind . baseColor )
98
97
99
98
if ( ! payload . length ) {
100
- logger . warn ( 'Selected components not found. Exiting.' )
99
+ consola . warn ( 'Selected components not found. Exiting.' )
101
100
process . exit ( 0 )
102
101
}
103
102
@@ -114,7 +113,6 @@ export const add = new Command()
114
113
}
115
114
116
115
const spinner = ora ( 'Installing components...' ) . start ( )
117
- const skippedDeps = new Set < string > ( )
118
116
for ( const item of payload ) {
119
117
spinner . text = `Installing ${ item . name } ...`
120
118
const targetDir = getItemTargetPath (
@@ -144,8 +142,8 @@ export const add = new Command()
144
142
} )
145
143
146
144
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 (
149
147
'--overwrite' ,
150
148
) } flag.`,
151
149
)
@@ -159,6 +157,20 @@ export const add = new Command()
159
157
}
160
158
}
161
159
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
+
162
174
const componentDir = path . resolve ( targetDir , item . name )
163
175
if ( ! existsSync ( componentDir ) )
164
176
await fs . mkdir ( componentDir , { recursive : true } )
@@ -201,25 +213,6 @@ export const add = new Command()
201
213
202
214
await fs . writeFile ( filePath , content )
203
215
}
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
- }
223
216
}
224
217
spinner . succeed ( 'Done.' )
225
218
}
0 commit comments