diff --git a/packages/core/src/cli/commands.ts b/packages/core/src/cli/commands.ts index 48aab9452..b3ff138c3 100644 --- a/packages/core/src/cli/commands.ts +++ b/packages/core/src/cli/commands.ts @@ -2,6 +2,7 @@ import type { LogLevel, RsbuildMode } from '@rsbuild/core'; import cac, { type CAC } from 'cac'; import type { ConfigLoader } from '../config'; import type { Format, Syntax } from '../types/config'; +import { color } from '../utils/color'; import { logger } from '../utils/logger'; import { build } from './build'; import { initConfig } from './initConfig'; @@ -42,6 +43,8 @@ export type InspectOptions = CommonOptions & { verbose?: boolean; }; +export type MfDevOptions = CommonOptions; + const applyCommonOptions = (cli: CAC) => { cli .option( @@ -81,12 +84,12 @@ const applyCommonOptions = (cli: CAC) => { export function runCli(): void { const cli = cac('rslib'); - cli.help(); cli.version(RSLIB_VERSION); applyCommonOptions(cli); - const buildCommand = cli.command('build', 'build the library for production'); + const buildDescription = `build the library for production ${color.dim('(default if no command is given)')}`; + const buildCommand = cli.command('', buildDescription).alias('build'); const inspectCommand = cli.command( 'inspect', 'inspect the Rsbuild / Rspack configs of Rslib projects', @@ -180,7 +183,7 @@ export function runCli(): void { } }); - mfDevCommand.action(async (options: CommonOptions) => { + mfDevCommand.action(async (options: MfDevOptions) => { try { const cliMfDev = async () => { const { config, watchFiles } = await initConfig(options); @@ -201,5 +204,36 @@ export function runCli(): void { } }); + cli.help((sections) => { + // remove the default version log as we already log it in greeting + sections.shift(); + + for (const section of sections) { + // Fix the command usage + if (section.title === 'Usage') { + section.body = section.body.replace( + '$ rslib', + color.yellow('$ rslib [command] [options]'), + ); + } + + // Fix the build command name + if (section.title === 'Commands') { + section.body = section.body.replace( + ` ${buildDescription}`, + `build ${buildDescription}`, + ); + } + + // Simplify the help output for sub-commands + if (section.title?.startsWith('For more info')) { + section.title = color.dim(' For details on a sub-command, run'); + section.body = color.dim(' $ rslib -h'); + } else { + section.title = color.cyan(section.title); + } + } + }); + cli.parse(); } diff --git a/tests/integration/cli/build/build.test.ts b/tests/integration/cli/build/build.test.ts index 1f3be98f5..7844f3749 100644 --- a/tests/integration/cli/build/build.test.ts +++ b/tests/integration/cli/build/build.test.ts @@ -26,6 +26,22 @@ describe('build command', async () => { `); }); + test('without any sub-command', async () => { + await fse.remove(path.join(__dirname, 'dist')); + runCliSync('', { + cwd: __dirname, + }); + + const files = await globContentJSON(path.join(__dirname, 'dist')); + const fileNames = Object.keys(files).sort(); + expect(fileNames).toMatchInlineSnapshot(` + [ + "/tests/integration/cli/build/dist/cjs/index.cjs", + "/tests/integration/cli/build/dist/esm/index.js", + ] + `); + }); + test('--lib', async () => { await fse.remove(path.join(__dirname, 'dist')); runCliSync('build --lib esm', { diff --git a/website/docs/en/guide/basic/cli.mdx b/website/docs/en/guide/basic/cli.mdx index af3a55056..11933af78 100644 --- a/website/docs/en/guide/basic/cli.mdx +++ b/website/docs/en/guide/basic/cli.mdx @@ -14,10 +14,10 @@ The output is shown below: ```bash Usage: - $ rslib [options] + $ rslib [command] [options] Commands: - build build the library for production + build build the library for production (default if no command is given) inspect inspect the Rsbuild / Rspack configs of Rslib projects mf-dev start Rsbuild dev server of Module Federation format ``` diff --git a/website/docs/zh/guide/basic/cli.mdx b/website/docs/zh/guide/basic/cli.mdx index adc91c49d..7e6438cc5 100644 --- a/website/docs/zh/guide/basic/cli.mdx +++ b/website/docs/zh/guide/basic/cli.mdx @@ -14,10 +14,10 @@ npx rslib -h ```bash Usage: - $ rslib [options] + $ rslib [command] [options] Commands: - build 构建用于生产环境的产物 + build 构建用于生产环境的产物(未指定命令时默认执行) inspect 检查 Rslib 项目的 Rsbuild 配置和 Rspack 配置 mf-dev 为 Module Federation 格式的库启用 Rsbuild 开发服务器 ```