Skip to content

Commit a1d0c42

Browse files
committed
refactor!: enhance CLI behaviour and logs
1 parent e6282bf commit a1d0c42

File tree

5 files changed

+72
-55
lines changed

5 files changed

+72
-55
lines changed

packages/core/bin/rslib.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ if (enableCompileCache) {
1313
}
1414

1515
async function main() {
16-
const { logger, prepareCli, runCli } = await import('../dist/index.js');
17-
prepareCli();
18-
19-
try {
20-
runCli();
21-
} catch (err) {
22-
logger.error(err);
23-
}
16+
const { runCLI } = await import('../dist/index.js');
17+
runCLI();
2418
}
2519

2620
main();

packages/core/src/cli/commands.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { LogLevel, RsbuildMode, RsbuildPlugin } from '@rsbuild/core';
22
import cac, { type CAC } from 'cac';
33
import type { ConfigLoader } from '../config';
4-
import type { Format, Syntax } from '../types/config';
4+
import type { Format, Syntax } from '../types';
55
import { color } from '../utils/color';
66
import { logger } from '../utils/logger';
77
import { build } from './build';
@@ -10,6 +10,8 @@ import { inspect } from './inspect';
1010
import { startMFDevServer } from './mf';
1111
import { watchFilesForRestart } from './restart';
1212

13+
export const RSPACK_BUILD_ERROR = 'Rspack build failed.';
14+
1315
export type CommonOptions = {
1416
root?: string;
1517
config?: string;
@@ -81,11 +83,12 @@ const applyCommonOptions = (cli: CAC) => {
8183
);
8284
};
8385

84-
export function runCli(): void {
86+
export function setupCommands(): void {
8587
const cli = cac('rslib');
8688

8789
cli.version(RSLIB_VERSION);
8890

91+
// Apply common options to all commands
8992
applyCommonOptions(cli);
9093

9194
const buildDescription = `build the library for production ${color.dim('(default if no command is given)')}`;
@@ -162,7 +165,11 @@ export function runCli(): void {
162165

163166
await cliBuild();
164167
} catch (err) {
165-
logger.error('Failed to build.');
168+
const isRspackError =
169+
err instanceof Error && err.message === RSPACK_BUILD_ERROR;
170+
if (!isRspackError) {
171+
logger.error('Failed to build.');
172+
}
166173
if (err instanceof AggregateError) {
167174
for (const error of err.errors) {
168175
logger.error(error);

packages/core/src/cli/index.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import type { LogLevel } from '@rsbuild/core';
2+
import { isDebug, logger } from '../utils/logger';
3+
import { setupCommands } from './commands';
4+
5+
function initNodeEnv() {
6+
if (!process.env.NODE_ENV) {
7+
const command = process.argv[2] ?? '';
8+
process.env.NODE_ENV = ['build'].includes(command)
9+
? 'production'
10+
: 'development';
11+
}
12+
}
13+
14+
function showGreeting() {
15+
// Ensure consistent spacing before the greeting message.
16+
// Different package managers handle output formatting differently - some automatically
17+
// add a blank line before command output, while others do not.
18+
const { npm_execpath, npm_lifecycle_event, NODE_RUN_SCRIPT_NAME } =
19+
process.env;
20+
const isNpx = npm_lifecycle_event === 'npx';
21+
const isBun = npm_execpath?.includes('.bun');
22+
const isNodeRun = Boolean(NODE_RUN_SCRIPT_NAME);
23+
const prefix = isNpx || isBun || isNodeRun ? '\n' : '';
24+
logger.greet(`${prefix}Rslib v${RSLIB_VERSION}\n`);
25+
}
26+
27+
// ensure log level is set before any log is printed
28+
function setupLogLevel() {
29+
const logLevelIndex = process.argv.findIndex(
30+
(item) => item === '--log-level' || item === '--logLevel',
31+
);
32+
if (logLevelIndex !== -1) {
33+
const level = process.argv[logLevelIndex + 1];
34+
if (level && ['warn', 'error', 'silent'].includes(level) && !isDebug()) {
35+
logger.level = level as LogLevel;
36+
}
37+
}
38+
}
39+
40+
export function runCLI(): void {
41+
// make it easier to identify the process via activity monitor or other tools
42+
process.title = 'rslib-node';
43+
44+
initNodeEnv();
45+
setupLogLevel();
46+
showGreeting();
47+
48+
try {
49+
setupCommands();
50+
} catch (err) {
51+
logger.error('Failed to start Rslib CLI.');
52+
logger.error(err);
53+
}
54+
}

packages/core/src/cli/prepare.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

packages/core/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1+
/**
2+
* The methods and types exported from this file are considered as
3+
* the public API of @rslib/core.
4+
*/
5+
6+
export { runCLI } from './cli';
17
export { build } from './cli/build';
2-
export { runCli } from './cli/commands';
38
export { inspect } from './cli/inspect';
49
export { startMFDevServer } from './cli/mf';
5-
export { prepareCli } from './cli/prepare';
610
export {
711
composeCreateRsbuildConfig as unstable_composeCreateRsbuildConfig,
812
defineConfig,

0 commit comments

Comments
 (0)