Skip to content

Commit

Permalink
feat: replace cac with commander
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jan 10, 2025
1 parent 69d5d66 commit ef0ba57
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 50 deletions.
18 changes: 12 additions & 6 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tools/create-vuepress/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
},
"dependencies": {
"@inquirer/prompts": "^7.2.1",
"cac": "^6.7.14",
"commander": "^13.0.0",
"execa": "^9.5.2"
},
"peerDependencies": {
Expand Down
18 changes: 8 additions & 10 deletions tools/create-vuepress/src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { existsSync, readdirSync } from 'node:fs'
import { resolve } from 'node:path'
import { confirm, select } from '@inquirer/prompts'
import type { Command } from 'commander'
import { execaCommand, execaCommandSync } from 'execa'
import { KNOWN_THEME_COMMANDS } from './config/index.js'
import { createPackageJson, generateTemplate } from './flow/index.js'
Expand All @@ -26,10 +27,11 @@ interface CreateOptions {
const bundlers: Bundler[] = ['vite', 'webpack']
const presets: Preset[] = ['blog', 'docs']

export const mainAction = async (
export async function mainAction(
this: Command,
targetDir: string,
{ bundler, preset, theme = '@vuepress/theme-default' }: CreateOptions,
): Promise<void> => {
): Promise<void> {
// get language
const { lang, locale } = await getLanguage()

Expand All @@ -52,28 +54,24 @@ export const mainAction = async (

// check bundler
if (bundler && !['vite', 'webpack'].includes(bundler)) {
console.error(locale.error.bundler)
return
this.error(locale.error.bundler)
}

// check presets
if (preset && !['docs', 'blog'].includes(preset)) {
console.error(locale.error.preset)
return
this.error(locale.error.preset)
}

// check if the user is a noob and warn him
if (!targetDir || (targetDir.startsWith('[') && targetDir.endsWith(']'))) {
console.error(locale.error.dirMissing(packageManager))
return
this.error(locale.error.dirMissing(packageManager))
}

const targetDirPath = resolve(process.cwd(), targetDir)

// check if the user is trying to cover his files
if (existsSync(targetDirPath) && readdirSync(targetDirPath).length) {
console.error(locale.error.dirNotEmpty(targetDir))
return
this.error(locale.error.dirNotEmpty(targetDir))
}

ensureDirExistSync(targetDirPath)
Expand Down
29 changes: 15 additions & 14 deletions tools/create-vuepress/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
#!/usr/bin/env node
import { cac } from 'cac'
import { createCommand } from 'commander'
import { mainAction } from './action.js'
import { version } from './utils/index.js'

const cli = cac('create-vuepress')
const program = createCommand('create-vuepress')

cli
.command('[dir]', 'Generate a new vuepress project in [dir]')
.option('-t, --theme <theme>', 'Theme to use')
.option('-p, --preset <preset>', 'Preset to use, can be docs or blog')
.usage(
program
.argument('<dir>', 'Dir to create the template in')
.option('-t, --theme [theme]', 'Theme to use')
.option('-p, --preset [preset]', 'Preset to use, docs or blog only')
.description(
`\
[dir]
Generate a new vuepress template
Generate vuepress template in dir.`,
· pnpm create vuepress <dir>
· npm init vuepress@latest <dir>
· yarn create vuepress <dir>
`,
)
.example('vuepress-project')
.action(mainAction)

cli.help()
program.version(version)
program.showHelpAfterError('add --help for additional information')

cli.version(version)

cli.parse()
await program.parseAsync()
2 changes: 1 addition & 1 deletion tools/vp-update/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"clean": "rimraf --glob ./lib ./*.tsbuildinfo"
},
"dependencies": {
"cac": "^6.7.14",
"commander": "^13.0.0",
"semver": "^7.6.3"
},
"devDependencies": {
Expand Down
34 changes: 16 additions & 18 deletions tools/vp-update/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,34 @@
import { spawnSync } from 'node:child_process'
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { cac } from 'cac'
import { createCommand } from 'commander'
import { VERSION } from './config/index.js'
import {
checkTaobaoRegistry,
getPackageManager,
updatePackages,
} from './utils/index.js'

const cli = cac('vp-update')
const program = createCommand('vp-update')

cli
.command('[dir]', 'Update VuePress project')
program
.summary('Update VuePress project')
.argument('[dir]', 'Dir of VuePress project', '')
.usage(
'pnpm dlx vp-update [dir] / npx vp-update [dir] / bunx vp-update [dir]',
`
pnpm dlx vp-update [dir] / npx vp-update [dir] / bunx vp-update [dir]\
`,
)
.example('docs')
.action(async (targetDir: string = ''): Promise<Error | void> => {
.action(async (targetDir: string = ''): Promise<void> => {
console.log('Bumping deps...')

const dir = resolve(process.cwd(), targetDir)
const packageJSON = resolve(dir, 'package.json')

if (!existsSync(packageJSON))
return new Error(`No package.json found in ${targetDir || 'current dir'}`)
return program.error(
`No package.json found in ${targetDir || 'current dir'}`,
)

const packageManager = getPackageManager()

Expand Down Expand Up @@ -81,14 +86,7 @@ cli
)
})

cli.help(() => [
{
title:
'pnpm dlx vp-update [dir] / npx vp-update [dir] / bunx vp-update [dir]',
body: 'Update VuePress project in [dir]',
},
])

cli.version(VERSION)
program.version(VERSION)
program.showHelpAfterError('add --help for additional information')

cli.parse()
await program.parseAsync()

0 comments on commit ef0ba57

Please sign in to comment.