Skip to content

Commit 79b47e9

Browse files
authored
feat(CLI): run rslib without any sub-command to trigger build (#1316)
1 parent 2326c3c commit 79b47e9

File tree

4 files changed

+57
-7
lines changed

4 files changed

+57
-7
lines changed

packages/core/src/cli/commands.ts

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { LogLevel, RsbuildMode } from '@rsbuild/core';
22
import cac, { type CAC } from 'cac';
33
import type { ConfigLoader } from '../config';
44
import type { Format, Syntax } from '../types/config';
5+
import { color } from '../utils/color';
56
import { logger } from '../utils/logger';
67
import { build } from './build';
78
import { initConfig } from './initConfig';
@@ -42,6 +43,8 @@ export type InspectOptions = CommonOptions & {
4243
verbose?: boolean;
4344
};
4445

46+
export type MfDevOptions = CommonOptions;
47+
4548
const applyCommonOptions = (cli: CAC) => {
4649
cli
4750
.option(
@@ -81,12 +84,12 @@ const applyCommonOptions = (cli: CAC) => {
8184
export function runCli(): void {
8285
const cli = cac('rslib');
8386

84-
cli.help();
8587
cli.version(RSLIB_VERSION);
8688

8789
applyCommonOptions(cli);
8890

89-
const buildCommand = cli.command('build', 'build the library for production');
91+
const buildDescription = `build the library for production ${color.dim('(default if no command is given)')}`;
92+
const buildCommand = cli.command('', buildDescription).alias('build');
9093
const inspectCommand = cli.command(
9194
'inspect',
9295
'inspect the Rsbuild / Rspack configs of Rslib projects',
@@ -180,7 +183,7 @@ export function runCli(): void {
180183
}
181184
});
182185

183-
mfDevCommand.action(async (options: CommonOptions) => {
186+
mfDevCommand.action(async (options: MfDevOptions) => {
184187
try {
185188
const cliMfDev = async () => {
186189
const { config, watchFiles } = await initConfig(options);
@@ -201,5 +204,36 @@ export function runCli(): void {
201204
}
202205
});
203206

207+
cli.help((sections) => {
208+
// remove the default version log as we already log it in greeting
209+
sections.shift();
210+
211+
for (const section of sections) {
212+
// Fix the command usage
213+
if (section.title === 'Usage') {
214+
section.body = section.body.replace(
215+
'$ rslib',
216+
color.yellow('$ rslib [command] [options]'),
217+
);
218+
}
219+
220+
// Fix the build command name
221+
if (section.title === 'Commands') {
222+
section.body = section.body.replace(
223+
` ${buildDescription}`,
224+
`build ${buildDescription}`,
225+
);
226+
}
227+
228+
// Simplify the help output for sub-commands
229+
if (section.title?.startsWith('For more info')) {
230+
section.title = color.dim(' For details on a sub-command, run');
231+
section.body = color.dim(' $ rslib <command> -h');
232+
} else {
233+
section.title = color.cyan(section.title);
234+
}
235+
}
236+
});
237+
204238
cli.parse();
205239
}

tests/integration/cli/build/build.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ describe('build command', async () => {
2626
`);
2727
});
2828

29+
test('without any sub-command', async () => {
30+
await fse.remove(path.join(__dirname, 'dist'));
31+
runCliSync('', {
32+
cwd: __dirname,
33+
});
34+
35+
const files = await globContentJSON(path.join(__dirname, 'dist'));
36+
const fileNames = Object.keys(files).sort();
37+
expect(fileNames).toMatchInlineSnapshot(`
38+
[
39+
"<ROOT>/tests/integration/cli/build/dist/cjs/index.cjs",
40+
"<ROOT>/tests/integration/cli/build/dist/esm/index.js",
41+
]
42+
`);
43+
});
44+
2945
test('--lib', async () => {
3046
await fse.remove(path.join(__dirname, 'dist'));
3147
runCliSync('build --lib esm', {

website/docs/en/guide/basic/cli.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ The output is shown below:
1414

1515
```bash
1616
Usage:
17-
$ rslib <command> [options]
17+
$ rslib [command] [options]
1818

1919
Commands:
20-
build build the library for production
20+
build build the library for production (default if no command is given)
2121
inspect inspect the Rsbuild / Rspack configs of Rslib projects
2222
mf-dev start Rsbuild dev server of Module Federation format
2323
```

website/docs/zh/guide/basic/cli.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ npx rslib -h
1414

1515
```bash
1616
Usage:
17-
$ rslib <command> [options]
17+
$ rslib [command] [options]
1818

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

0 commit comments

Comments
 (0)