Skip to content

Commit

Permalink
feat: change commander by citty
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-ub committed Sep 24, 2024
1 parent 9b6c70f commit 72e3fdb
Show file tree
Hide file tree
Showing 7 changed files with 728 additions and 317 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"dependencies": {
"@antfu/ni": "^0.23.0",
"chalk": "5.2.0",
"commander": "^10.0.0",
"citty": "^0.1.6",
"cosmiconfig": "^8.1.3",
"diff": "^5.1.0",
"execa": "^7.0.0",
Expand Down Expand Up @@ -83,7 +83,6 @@
"lint-staged": "^15.2.10",
"pnpm": "^9.9.0",
"simple-git-hooks": "^2.11.1",
"type-fest": "^3.8.0",
"typescript": "^5.5.4",
"unbuild": "^2.0.0",
"vite": "^5.4.3",
Expand Down
849 changes: 608 additions & 241 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

39 changes: 18 additions & 21 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
#!/usr/bin/env node
import process from 'node:process'
import { Command } from 'commander'

import { defineCommand, runMain } from 'citty'

import { description, name, version } from '../package.json'

import { add } from './commands/add'
import { diff } from './commands/diff'
import { init } from './commands/init'

import { getPackageInfo } from './utils/get-package-info'

process.on('SIGINT', () => process.exit(0))
process.on('SIGTERM', () => process.exit(0))

async function main(): Promise<void> {
const packageInfo = await getPackageInfo()

const program = new Command()
.name('shadcn-ng')
.description('add components and dependencies to your project')
.version(
packageInfo.version || '1.0.0',
'-v, --version',
'display the version number',
)

program.addCommand(init).addCommand(add).addCommand(diff)

program.parse()
}

main()
const main = defineCommand({
meta: {
name,
version,
description,
},
subCommands: {
init,
add,
diff,
},
})

runMain(main)
63 changes: 46 additions & 17 deletions src/commands/add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import process from 'node:process'

import chalk from 'chalk'
import { Command } from 'commander'
import { defineCommand } from 'citty'
import { execa } from 'execa'
import ora from 'ora'
import prompts from 'prompts'
Expand Down Expand Up @@ -31,24 +31,52 @@ const addOptionsSchema = z.object({
path: z.string().optional(),
})

export const add = new Command()
.name('add')
.description('add a component to your project')
.argument('[components...]', 'the components to add')
.option('-y, --yes', 'skip confirmation prompt.', true)
.option('-o, --overwrite', 'overwrite existing files.', false)
.option(
'-c, --cwd <cwd>',
'the working directory. defaults to the current directory.',
process.cwd(),
)
.option('-a, --all', 'add all available components', false)
.option('-p, --path <path>', 'the path to add the component to.')
.action(async (components, opts) => {
export const add = defineCommand({
meta: {
description: 'add a component to your project',
},
args: {
components: {
type: 'positional',
description: 'the components to add',
required: true,
},
yes: {
type: 'boolean',
description: 'skip confirmation prompt.',
default: true,
alias: 'y',
},
overwrite: {
type: 'boolean',
description: 'overwrite existing files.',
default: false,
alias: 'o',
},
cwd: {
type: 'string',
description: 'the working directory. defaults to the current directory.',
default: process.cwd(),
alias: 'c',
},
all: {
type: 'boolean',
description: 'add all available components',
default: false,
alias: 'a',
},
path: {
type: 'string',
description: 'the path to add the component to.',
alias: 'p',
},
},
run: async ({ args: opts }) => {
const { _: components } = opts
try {
const options = addOptionsSchema.parse({
components,
...opts,
components,
})

const cwd = path.resolve(options.cwd)
Expand Down Expand Up @@ -215,4 +243,5 @@ export const add = new Command()
catch (error) {
handleError(error)
}
})
},
})
41 changes: 27 additions & 14 deletions src/commands/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import process from 'node:process'

import chalk from 'chalk'
import { Command } from 'commander'
import { defineCommand } from 'citty'
import { type Change, diffLines } from 'diff'
import { z } from 'zod'

Expand All @@ -27,20 +27,32 @@ const updateOptionsSchema = z.object({
path: z.string().optional(),
})

export const diff = new Command()
.name('diff')
.description('check for updates against the registry')
.argument('[component]', 'the component name')
.option('-y, --yes', 'skip confirmation prompt.', false)
.option(
'-c, --cwd <cwd>',
'the working directory. defaults to the current directory.',
process.cwd(),
)
.action(async (name, opts) => {
export const diff = defineCommand({
meta: {
description: 'check for updates against the registry',
},
args: {
component: {
type: 'positional',
description: 'the component name',
required: false,
},
yes: {
type: 'boolean',
description: 'skip confirmation prompt.',
default: false,
alias: 'y',
},
cwd: {
type: 'string',
description: 'the working directory. defaults to the current directory.',
default: process.cwd(),
alias: 'c',
},
},
run: async ({ args: opts }) => {
try {
const options = updateOptionsSchema.parse({
component: name,
...opts,
})

Expand Down Expand Up @@ -137,7 +149,8 @@ export const diff = new Command()
catch (error) {
handleError(error)
}
})
},
})

async function diffComponent(
component: z.infer<typeof registryIndexSchema>[number],
Expand Down
41 changes: 28 additions & 13 deletions src/commands/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import path from 'node:path'
import process from 'node:process'

import chalk from 'chalk'
import { Command } from 'commander'
import { defineCommand } from 'citty'
import { execa } from 'execa'
import template from 'lodash.template'
import ora from 'ora'
Expand Down Expand Up @@ -46,17 +46,31 @@ const initOptionsSchema = z.object({
defaults: z.boolean(),
})

export const init = new Command()
.name('init')
.description('initialize your project and install dependencies')
.option('-y, --yes', 'skip confirmation prompt.', false)
.option('-d, --defaults,', 'use default configuration.', false)
.option(
'-c, --cwd <cwd>',
'the working directory. defaults to the current directory.',
process.cwd(),
)
.action(async (opts) => {
export const init = defineCommand({
meta: {
description: 'initialize your project and install dependencies',
},
args: {
yes: {
type: 'boolean',
description: 'skip confirmation prompt.',
default: false,
alias: 'y',
},
defaults: {
type: 'boolean',
description: 'use default configuration.',
default: false,
alias: 'd',
},
cwd: {
type: 'string',
description: 'the working directory. defaults to the current directory.',
default: process.cwd(),
alias: 'c',
},
},
run: async ({ args: opts }) => {
try {
const options = initOptionsSchema.parse(opts)
const cwd = path.resolve(options.cwd)
Expand Down Expand Up @@ -96,7 +110,8 @@ export const init = new Command()
catch (error) {
handleError(error)
}
})
},
})

export async function promptForConfig(
cwd: string,
Expand Down
9 changes: 0 additions & 9 deletions src/utils/get-package-info.ts

This file was deleted.

0 comments on commit 72e3fdb

Please sign in to comment.