Skip to content

Commit 0e84af7

Browse files
feat: use unjs modules and improve cli from main shadcn-ui source, custom ui dir (#324)
* feat: add devDeps, add nypm for installing deps * feat: custom ui dir * refactor: use consola instead of chalk * test: ui alias * refactor: import { z } from 'zod' instead of *, replace node:path with pathe * chore: add components name to `configFile` option * chore: update `c12` which fix json5 parse issue and it also supports .config directory * chore: update `https-proxy-agent` * fix: await until dependencies are installed then run detypes process * feat: add tailwind prefix * test: tw-prefix snapshot * chore: add prefix option to init * test: apply prefix * fix: tw-prefix parse wrongly * chore: hide prefix temporarily --------- Co-authored-by: zernonia <zernonia@gmail.com>
1 parent c487137 commit 0e84af7

34 files changed

+1037
-946
lines changed

apps/www/src/content/docs/components/data-table.md

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ description: Powerful table and datagrids built using TanStack Table.
44
primitive: https://tanstack.com/table/v8/docs/guide/introduction
55
---
66

7-
87
<ComponentPreview name="DataTableDemo" />
98

109
## Introduction
@@ -56,7 +55,6 @@ npm install @tanstack/vue-table
5655

5756
<ComponentPreview name="DataTableColumnPinningDemo" />
5857

59-
6058
## Prerequisites
6159

6260
We are going to build a table to show recent payments. Here's what our data looks like:
@@ -219,7 +217,6 @@ const table = useVueTable({
219217

220218
</Callout>
221219

222-
223220
### Render the table
224221

225222
Finally, we'll render our table in our index component.
@@ -270,7 +267,6 @@ Let's format the amount cell to display the dollar amount. We'll also align the
270267

271268
Update the `header` and `cell` definitions for amount as follows:
272269

273-
274270
```ts:line-numbers title="components/payments/columns.ts" {5-17}
275271
import { h } from 'vue'
276272
@@ -345,7 +341,6 @@ function copy(id: string) {
345341

346342
Update our columns definition to add a new `actions` column. The `actions` cell returns a `<Dropdown />` component.
347343

348-
349344
```ts:line-numbers showLineNumber{2,6-16}
350345
import { ColumnDef } from "@tanstack/vue-table"
351346
import DropdownAction from '@/components/DataTableDropDown.vue'

apps/www/src/public/schema.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
},
4343
"components": {
4444
"type": "string"
45+
},
46+
"ui": {
47+
"type": "string"
4548
}
4649
},
4750
"required": ["utils", "components"]

eslint.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export default antfu(
2424
'no-tabs': 0,
2525
'import/first': 0,
2626
'node/prefer-global/process': 0,
27+
'style/no-tabs': 0,
2728
},
2829
},
2930
)

packages/cli/package.json

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"dev": "tsup --watch",
3333
"build": "tsup",
3434
"typecheck": "tsc --noEmit",
35-
"clean": "rimraf dist && rimraf components",
35+
"clean": "node ./scripts/rimraf.js",
3636
"lint": "eslint .",
3737
"lint:fix": "eslint --fix .",
3838
"start:dev": "COMPONENTS_REGISTRY_URL=http://localhost:3001 node dist/index.js",
@@ -45,41 +45,39 @@
4545
"test:ui": "vitest --ui"
4646
},
4747
"dependencies": {
48-
"@antfu/ni": "^0.21.8",
49-
"@babel/core": "^7.22.17",
50-
"@babel/parser": "^7.22.16",
51-
"@babel/plugin-transform-typescript": "^7.22.15",
48+
"@babel/core": "^7.24.0",
49+
"@babel/parser": "^7.24.0",
5250
"@vue/compiler-sfc": "^3.4",
53-
"chalk": "5.3.0",
54-
"commander": "^11.0.0",
55-
"cosmiconfig": "^8.3.6",
51+
"c12": "^1.9.0",
52+
"commander": "^12.0.0",
53+
"consola": "^3.2.3",
5654
"detype": "npm:detypes@^0.7.9",
57-
"diff": "^5.1.0",
58-
"execa": "^8.0.1",
59-
"fs-extra": "^11.1.1",
60-
"https-proxy-agent": "^7.0.2",
55+
"diff": "^5.2.0",
56+
"fs-extra": "^11.2.0",
57+
"https-proxy-agent": "^7.0.4",
6158
"lodash.template": "^4.5.0",
62-
"magic-string": "^0.30.3",
63-
"node-fetch": "^3.3.2",
64-
"ora": "^7.0.1",
59+
"magic-string": "^0.30.8",
60+
"nypm": "^0.3.8",
61+
"ofetch": "^1.3.3",
62+
"ora": "^8.0.1",
63+
"pathe": "^1.1.2",
6564
"prompts": "^2.4.2",
66-
"radix-vue": "^1.4.8",
67-
"recast": "^0.23.4",
68-
"rimraf": "^5.0.1",
69-
"ts-morph": "^19.0.0",
65+
"radix-vue": "^1.4.9",
66+
"ts-morph": "^21.0.1",
7067
"tsconfig-paths": "^4.2.0",
71-
"vite-tsconfig-paths": "^4.2.1",
72-
"zod": "^3.22.2"
68+
"zod": "^3.22.4"
7369
},
7470
"devDependencies": {
75-
"@types/babel__core": "^7.20.1",
76-
"@types/diff": "^5.0.3",
77-
"@types/fs-extra": "^11.0.1",
78-
"@types/lodash.template": "^4.5.1",
79-
"@types/prompts": "^2.4.4",
71+
"@types/babel__core": "^7.20.5",
72+
"@types/diff": "^5.0.9",
73+
"@types/fs-extra": "^11.0.4",
74+
"@types/lodash.template": "^4.5.3",
75+
"@types/node": "^20.11.24",
76+
"@types/prompts": "^2.4.9",
8077
"@vitest/ui": "^0.34.4",
81-
"tsup": "^7.2.0",
82-
"type-fest": "^4.3.1",
83-
"typescript": "^5.2.2"
78+
"tsup": "^8.0.2",
79+
"type-fest": "^4.10.3",
80+
"typescript": "^5.3.3",
81+
"vite-tsconfig-paths": "^4.3.1"
8482
}
8583
}

packages/cli/scripts/rimraf.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import fsp from 'node:fs/promises'
2+
3+
function rmdir(dirs) {
4+
dirs.forEach(async (dir) => {
5+
await fsp.unlink(dir).catch(() => {})
6+
await fsp.rm(dir, { recursive: true, force: true }).catch(() => {})
7+
})
8+
}
9+
10+
rmdir(['dist', 'components'])

packages/cli/src/commands/add.ts

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { existsSync, promises as fs, rmSync } from 'node:fs'
2-
import path from 'node:path'
32
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'
56
import { Command } from 'commander'
6-
import { execa } from 'execa'
77
import ora from 'ora'
88
import prompts from 'prompts'
9-
import * as z from 'zod'
9+
import { z } from 'zod'
10+
import { addDependency, addDevDependency } from 'nypm'
1011
import { transform } from '@/src/utils/transformers'
1112
import { getConfig } from '@/src/utils/get-config'
12-
import { getPackageManager } from '@/src/utils/get-package-manager'
1313
import { handleError } from '@/src/utils/handle-error'
14-
import { logger } from '@/src/utils/logger'
1514
import {
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
}

packages/cli/src/commands/diff.ts

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { existsSync, promises as fs } from 'node:fs'
2-
import path from 'node:path'
32
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'
56
import { Command } from 'commander'
67
import { type Change, diffLines } from 'diff'
7-
import * as z from 'zod'
8+
import { z } from 'zod'
89
import type { Config } from '@/src/utils/get-config'
910
import { getConfig } from '@/src/utils/get-config'
1011
import { handleError } from '@/src/utils/handle-error'
11-
import { logger } from '@/src/utils/logger'
1212
import {
1313
fetchTree,
1414
getItemTargetPath,
@@ -45,14 +45,14 @@ export const diff = new Command()
4545
const cwd = path.resolve(options.cwd)
4646

4747
if (!existsSync(cwd)) {
48-
logger.error(`The path ${cwd} does not exist. Please try again.`)
48+
consola.error(`The path ${cwd} does not exist. Please try again.`)
4949
process.exit(1)
5050
}
5151

5252
const config = await getConfig(cwd)
5353
if (!config) {
54-
logger.warn(
55-
`Configuration is missing. Please run ${chalk.green(
54+
consola.warn(
55+
`Configuration is missing. Please run ${colors.green(
5656
'init',
5757
)} to create a components.json file.`,
5858
)
@@ -88,19 +88,20 @@ export const diff = new Command()
8888
}
8989

9090
if (!componentsWithUpdates.length) {
91-
logger.info('No updates found.')
91+
consola.info('No updates found.')
9292
process.exit(0)
9393
}
9494

95-
logger.info('The following components have updates available:')
95+
consola.info('The following components have updates available:')
9696
for (const component of componentsWithUpdates) {
97-
logger.info(`- ${component.name}`)
97+
consola.info(`- ${component.name}`)
9898
for (const change of component.changes)
99-
logger.info(` - ${change.filePath}`)
99+
consola.info(` - ${change.filePath}`)
100100
}
101-
logger.break()
102-
logger.info(
103-
`Run ${chalk.green('diff <component>')} to see the changes.`,
101+
102+
consola.log('')
103+
consola.info(
104+
`Run ${colors.green('diff <component>')} to see the changes.`,
104105
)
105106
process.exit(0)
106107
}
@@ -111,23 +112,23 @@ export const diff = new Command()
111112
)
112113

113114
if (!component) {
114-
logger.error(
115-
`The component ${chalk.green(options.component)} does not exist.`,
115+
consola.error(
116+
`The component ${colors.green(options.component)} does not exist.`,
116117
)
117118
process.exit(1)
118119
}
119120

120121
const changes = await diffComponent(component, config)
121122

122123
if (!changes.length) {
123-
logger.info(`No updates found for ${options.component}.`)
124+
consola.info(`No updates found for ${options.component}.`)
124125
process.exit(0)
125126
}
126127

127128
for (const change of changes) {
128-
logger.info(`- ${change.filePath}`)
129+
consola.info(`- ${change.filePath}`)
129130
printDiff(change.patch)
130-
logger.info('')
131+
consola.log('')
131132
}
132133
}
133134
catch (error) {
@@ -184,10 +185,10 @@ function printDiff(diff: Change[]) {
184185
diff.forEach((part) => {
185186
if (part) {
186187
if (part.added)
187-
return process.stdout.write(chalk.green(part.value))
188+
return process.stdout.write(colors.green(part.value))
188189

189190
if (part.removed)
190-
return process.stdout.write(chalk.red(part.value))
191+
return process.stdout.write(colors.red(part.value))
191192

192193
return process.stdout.write(part.value)
193194
}

0 commit comments

Comments
 (0)