Skip to content
Open
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
4 changes: 4 additions & 0 deletions packages/rspeedy/core/src/cli/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export async function build(
const shouldExit = process.env['RSDOCTOR'] !== 'true' || isCI()
const isWatch = buildOptions.watch ?? false

if (buildOptions.logLevel) {
logger.level = buildOptions.logLevel
}

let onBeforeRestart: (() => Promise<void>)[] = []
try {
const { rspeedyConfig, configPath, createRspeedyOptions } = await init(
Expand Down
5 changes: 5 additions & 0 deletions packages/rspeedy/core/src/cli/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export interface CommonOptions {
noEnv?: boolean
mode?: RsbuildMode
root?: string
logLevel?: 'info' | 'warn' | 'error' | 'silent'
}

function applyCommonOptions(command: Command) {
Expand All @@ -43,6 +44,10 @@ function applyCommonOptions(command: Command) {
'-r --root <root>',
'set the project root directory (absolute path or relative to cwd)',
)
.option(
'--log-level <level>',
'Set the log level (info | warn | error | silent)',
)
Comment on lines +47 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major

🧩 Analysis chain

Add validation for log level values.

Commander.js will accept any string for --log-level, but only 'info' | 'warn' | 'error' | 'silent' are valid. Without validation, invalid values will be silently passed through, potentially causing runtime errors.

Apply this diff to add validation using Commander's .choices():

     .option(
       '--log-level <level>',
       'Set the log level (info | warn | error | silent)',
     )
+    .addOption(
+      new Option('--log-level <level>', 'Set the log level')
+        .choices(['info', 'warn', 'error', 'silent'])
+    )

Note: You'll need to import Option from commander:

 import type { Command } from 'commander'
+import { Option } from 'commander'

Run the following script to verify that all command implementations (build, dev, preview) consume the logLevel option consistently:


🏁 Script executed:

#!/bin/bash
# Description: Verify that all CLI commands implement logLevel option handling

echo "=== Checking build.ts for logLevel usage ==="
rg -n -C3 'buildOptions\.logLevel|logger\.level' packages/rspeedy/core/src/cli/build.ts

echo "=== Checking dev.ts for logLevel usage ==="
rg -n -C3 'devOptions\.logLevel|logger\.level' packages/rspeedy/core/src/cli/dev.ts

echo "=== Checking preview.ts for logLevel usage ==="
rg -n -C3 'previewOptions\.logLevel|logger\.level' packages/rspeedy/core/src/cli/preview.ts

echo "=== Checking inspect.ts for logLevel usage (for comparison) ==="
rg -n -C3 'inspectOptions\.logLevel|logger\.level' packages/rspeedy/core/src/cli/inspect.ts

Length of output: 1035


Add validation for --log-level via Commander.js .choices()
Restrict allowed values to info | warn | error | silent by replacing the loose .option() call. For example:

 import type { Command } from 'commander'
+import { Option } from 'commander'

   command
-    .option(
-      '--log-level <level>',
-      'Set the log level (info | warn | error | silent)',
-    )
+    .addOption(
+      new Option('--log-level <level>', 'Set the log level')
+        .choices(['info', 'warn', 'error', 'silent']),
+    )

All commands (build, dev, preview, inspect) already read logLevel and set logger.level.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
.option(
'--log-level <level>',
'Set the log level (info | warn | error | silent)',
)
import type { Command } from 'commander'
import { Option } from 'commander'
command
.addOption(
new Option('--log-level <level>', 'Set the log level')
.choices(['info', 'warn', 'error', 'silent']),
)
🤖 Prompt for AI Agents
In packages/rspeedy/core/src/cli/commands.ts around lines 47 to 50, the
--log-level option is currently defined without validation; change the Commander
option to restrict allowed values to the set ['info','warn','error','silent']
using Commander.js .choices() (and optionally provide a sensible default like
'info'), so parsing will fail on invalid inputs and downstream code that reads
logLevel/logger.level always receives one of the allowed values.

}

function resolveRoot(cwd: string, root?: string): string {
Expand Down
4 changes: 4 additions & 0 deletions packages/rspeedy/core/src/cli/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ export async function dev(
cwd: string,
devOptions: DevOptions,
): Promise<void> {
if (devOptions.logLevel) {
logger.level = devOptions.logLevel
}

let onBeforeRestart: (() => Promise<void>)[] = []
try {
const { rspeedyConfig, configPath, createRspeedyOptions } = await init(
Expand Down
4 changes: 4 additions & 0 deletions packages/rspeedy/core/src/cli/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ export async function inspect(
cwd: string,
inspectOptions: InspectOptions,
): Promise<void> {
if (inspectOptions.logLevel) {
logger.level = inspectOptions.logLevel
}

try {
const { createRspeedyOptions } = await init(cwd, inspectOptions)
const rspeedy = await createRspeedy(createRspeedyOptions)
Expand Down
4 changes: 4 additions & 0 deletions packages/rspeedy/core/src/cli/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export async function preview(
cwd: string,
previewOptions: PreviewOptions,
): Promise<void> {
if (previewOptions.logLevel) {
logger.level = previewOptions.logLevel
}

try {
const { createRspeedyOptions } = await init(cwd, previewOptions)

Expand Down