diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5cf67057de..c89bf1822b 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1200,9 +1200,9 @@ importers: '@inquirer/prompts': specifier: ^7.2.1 version: 7.2.1(@types/node@22.10.5) - cac: - specifier: ^6.7.14 - version: 6.7.14 + commander: + specifier: ^13.0.0 + version: 13.0.0 execa: specifier: ^9.5.2 version: 9.5.2 @@ -1268,9 +1268,9 @@ importers: tools/vp-update: dependencies: - cac: - specifier: ^6.7.14 - version: 6.7.14 + commander: + specifier: ^13.0.0 + version: 13.0.0 semver: specifier: ^7.6.3 version: 7.6.3 @@ -3927,6 +3927,10 @@ packages: comma-separated-tokens@2.0.3: resolution: {integrity: sha512-Fu4hJdvzeylCfQPp9SGWidpzrMs7tTrlu6Vb8XGaRGck8QSNZJJp538Wrb60Lax4fPwR64ViY468OIUTbRlGZg==} + commander@13.0.0: + resolution: {integrity: sha512-oPYleIY8wmTVzkvQq10AEok6YcTC4sRUBl8F9gVuwchGVUCTbl/vhLTaQqutuuySYOsu8YTgV+OxKc/8Yvx+mQ==} + engines: {node: '>=18'} + commander@2.20.3: resolution: {integrity: sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==} @@ -11701,6 +11705,8 @@ snapshots: comma-separated-tokens@2.0.3: {} + commander@13.0.0: {} + commander@2.20.3: {} commander@7.2.0: {} diff --git a/tools/create-vuepress/package.json b/tools/create-vuepress/package.json index 0ca90957ef..23e17f449c 100644 --- a/tools/create-vuepress/package.json +++ b/tools/create-vuepress/package.json @@ -43,7 +43,7 @@ }, "dependencies": { "@inquirer/prompts": "^7.2.1", - "cac": "^6.7.14", + "commander": "^13.0.0", "execa": "^9.5.2" }, "peerDependencies": { diff --git a/tools/create-vuepress/src/action.ts b/tools/create-vuepress/src/action.ts index c69b35b4e8..af5376f193 100644 --- a/tools/create-vuepress/src/action.ts +++ b/tools/create-vuepress/src/action.ts @@ -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' @@ -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 => { +): Promise { // get language const { lang, locale } = await getLanguage() @@ -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) diff --git a/tools/create-vuepress/src/index.ts b/tools/create-vuepress/src/index.ts index a554fe1716..1d362b472f 100644 --- a/tools/create-vuepress/src/index.ts +++ b/tools/create-vuepress/src/index.ts @@ -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 to use') - .option('-p, --preset ', 'Preset to use, can be docs or blog') - .usage( +program + .argument('', '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 +· npm init vuepress@latest +· yarn create vuepress +`, ) - .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() diff --git a/tools/vp-update/package.json b/tools/vp-update/package.json index 9dfb1e5943..d5092cd72f 100644 --- a/tools/vp-update/package.json +++ b/tools/vp-update/package.json @@ -38,7 +38,7 @@ "clean": "rimraf --glob ./lib ./*.tsbuildinfo" }, "dependencies": { - "cac": "^6.7.14", + "commander": "^13.0.0", "semver": "^7.6.3" }, "devDependencies": { diff --git a/tools/vp-update/src/index.ts b/tools/vp-update/src/index.ts index 729cdd8cbd..6bd2b4ace0 100644 --- a/tools/vp-update/src/index.ts +++ b/tools/vp-update/src/index.ts @@ -3,7 +3,7 @@ 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, @@ -11,21 +11,26 @@ import { 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 => { + .action(async (targetDir: string = ''): Promise => { 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() @@ -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()