Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 29 additions & 3 deletions packages/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -42,6 +43,8 @@ export type InspectOptions = CommonOptions & {
verbose?: boolean;
};

export type MfDevOptions = CommonOptions;

const applyCommonOptions = (cli: CAC) => {
cli
.option(
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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);
Expand All @@ -201,5 +204,28 @@ 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 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 <command> -h');
} else {
section.title = color.cyan(section.title);
}
}
});

cli.parse();
}
16 changes: 16 additions & 0 deletions tests/integration/cli/build/build.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(`
[
"<ROOT>/tests/integration/cli/build/dist/cjs/index.cjs",
"<ROOT>/tests/integration/cli/build/dist/esm/index.js",
]
`);
});

test('--lib', async () => {
await fse.remove(path.join(__dirname, 'dist'));
runCliSync('build --lib esm', {
Expand Down
4 changes: 2 additions & 2 deletions website/docs/en/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ The output is shown below:

```bash
Usage:
$ rslib <command> [options]
$ rslib

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
```
Expand Down
4 changes: 2 additions & 2 deletions website/docs/zh/guide/basic/cli.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ npx rslib -h

```bash
Usage:
$ rslib <command> [options]
$ rslib

Commands:
build 构建用于生产环境的产物
build 构建用于生产环境的产物(未指定命令时默认执行)
inspect 检查 Rslib 项目的 Rsbuild 配置和 Rspack 配置
mf-dev 为 Module Federation 格式的库启用 Rsbuild 开发服务器
```
Expand Down
Loading