Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 13 additions & 6 deletions e2e/cases/cli/log-level/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
import { execSync } from 'node:child_process';
import { stripVTControlCharacters as stripAnsi } from 'node:util';
import { rspackOnlyTest } from '@e2e/helper';
import { expect } from '@playwright/test';

rspackOnlyTest('should run build command with log level: info', async () => {
const result = execSync('npx rsbuild build --logLevel info', {
cwd: __dirname,
}).toString();
const result = stripAnsi(
execSync('npx rsbuild build --logLevel info', {
cwd: __dirname,
}).toString(),
);

expect(result).toContain('Rsbuild v');
expect(result).toContain('build started...');
expect(result).toContain('built in');
});

rspackOnlyTest('should run build command with log level: warn', async () => {
const result = execSync('npx rsbuild build --logLevel warn', {
cwd: __dirname,
}).toString();
const result = stripAnsi(
execSync('npx rsbuild build --logLevel warn', {
cwd: __dirname,
}).toString(),
);

expect(result).not.toContain('Rsbuild v');
expect(result).not.toContain('build started...');
expect(result).not.toContain('built in');
});
12 changes: 12 additions & 0 deletions packages/core/src/cli/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '../logger';
import type { LogLevel } from '../types';
import { setupCommands } from './commands';

function initNodeEnv() {
Expand All @@ -16,6 +17,17 @@ export async function runCLI(): Promise<void> {
// make it easier to identify the process via activity monitor or other tools
process.title = 'rsbuild-node';

// ensure log level is set before any log is printed
const logLevelIndex = process.argv.findIndex(
(item) => item === '--log-level' || item === '--logLevel',
);
if (logLevelIndex !== -1) {
const level = process.argv[logLevelIndex + 1];
if (level && ['warn', 'error', 'silent'].includes(level)) {
logger.level = level as LogLevel;
}
}

// Print a blank line to keep the greet log nice.
// Some package managers automatically output a blank line, some do not.
const { npm_execpath } = process.env;
Expand Down
Loading