From fe31456958761a9557e374e0e0443ec7a314bc74 Mon Sep 17 00:00:00 2001 From: nickbar01234 Date: Sat, 8 Nov 2025 13:19:50 -0500 Subject: [PATCH] Add log --level cli option --- packages/wxt/src/cli/__tests__/index.test.ts | 77 +++++++++++++++++++- packages/wxt/src/cli/cli-utils.ts | 7 +- packages/wxt/src/cli/commands.ts | 1 + 3 files changed, 83 insertions(+), 2 deletions(-) diff --git a/packages/wxt/src/cli/__tests__/index.test.ts b/packages/wxt/src/cli/__tests__/index.test.ts index 074934338..669dcf830 100644 --- a/packages/wxt/src/cli/__tests__/index.test.ts +++ b/packages/wxt/src/cli/__tests__/index.test.ts @@ -6,7 +6,7 @@ import { prepare } from '../../core/prepare'; import { clean } from '../../core/clean'; import { initialize } from '../../core/initialize'; import { mock } from 'vitest-mock-extended'; -import consola from 'consola'; +import consola, { LogLevels } from 'consola'; vi.mock('../../core/build'); const buildMock = vi.mocked(build); @@ -137,6 +137,21 @@ describe('CLI', () => { expect(createServerMock).toBeCalledWith({ debug: true, }); + expect(consola.level).toBe(LogLevels.debug); + }); + + it('should set log --level', async () => { + mockArgv('--level', 'warn'); + await importCli(); + + expect(consola.level).toBe(LogLevels.warn); + }); + + it('--debug should override --level', async () => { + mockArgv('--debug', '--level', 'silent'); + await importCli(); + + expect(consola.level).toBe(LogLevels.debug); }); }); @@ -229,6 +244,21 @@ describe('CLI', () => { expect(buildMock).toBeCalledWith({ debug: true, }); + expect(consola.level).toBe(LogLevels.debug); + }); + + it('should set log --level', async () => { + mockArgv('build', '--level', 'warn'); + await importCli(); + + expect(consola.level).toBe(LogLevels.warn); + }); + + it('--debug should override --level', async () => { + mockArgv('build', '--debug', '--level', 'silent'); + await importCli(); + + expect(consola.level).toBe(LogLevels.debug); }); }); @@ -310,6 +340,21 @@ describe('CLI', () => { debug: true, zip: {}, }); + expect(consola.level).toBe(LogLevels.debug); + }); + + it('should set log --level', async () => { + mockArgv('zip', '--level', 'warn'); + await importCli(); + + expect(consola.level).toBe(LogLevels.warn); + }); + + it('--debug should override --level', async () => { + mockArgv('zip', '--debug', '--level', 'silent'); + await importCli(); + + expect(consola.level).toBe(LogLevels.debug); }); it('should pass undefined for zipSources when --sources is not passed', async () => { @@ -379,6 +424,21 @@ describe('CLI', () => { expect(prepareMock).toBeCalledWith({ debug: true, }); + expect(consola.level).toBe(LogLevels.debug); + }); + + it('should set log --level', async () => { + mockArgv('prepare', '--level', 'warn'); + await importCli(); + + expect(consola.level).toBe(LogLevels.warn); + }); + + it('--debug should override --level', async () => { + mockArgv('prepare', '--debug', '--level', 'silent'); + await importCli(); + + expect(consola.level).toBe(LogLevels.debug); }); }); @@ -413,6 +473,21 @@ describe('CLI', () => { expect(cleanMock).toBeCalledWith({ debug: true, }); + expect(consola.level).toBe(LogLevels.debug); + }); + + it('should set log --level', async () => { + mockArgv('clean', '--level', 'warn'); + await importCli(); + + expect(consola.level).toBe(LogLevels.warn); + }); + + it('--debug should override --level', async () => { + mockArgv('clean', '--debug', '--level', 'silent'); + await importCli(); + + expect(consola.level).toBe(LogLevels.debug); }); }); diff --git a/packages/wxt/src/cli/cli-utils.ts b/packages/wxt/src/cli/cli-utils.ts index 2f525ae6f..86f1d4b1c 100644 --- a/packages/wxt/src/cli/cli-utils.ts +++ b/packages/wxt/src/cli/cli-utils.ts @@ -1,5 +1,5 @@ import { CAC, Command } from 'cac'; -import consola, { LogLevels } from 'consola'; +import consola, { LogLevels, LogType } from 'consola'; import { filterTruthy, toArray } from '../core/utils/arrays'; import { printHeader } from '../core/utils/log'; import { formatDuration } from '../core/utils/time'; @@ -19,6 +19,11 @@ export function wrapAction( }, ) { return async (...args: any[]) => { + const level: LogType | undefined = args.find((arg) => arg?.level)?.level; + if (level && Object.keys(LogLevels).includes(level)) { + consola.level = LogLevels[level]; + } + // Enable consola's debug mode globally at the start of all commands when the `--debug` flag is // passed const isDebug = !!args.find((arg) => arg?.debug); diff --git a/packages/wxt/src/cli/commands.ts b/packages/wxt/src/cli/commands.ts index d9e8c82ab..2b5fc7d06 100644 --- a/packages/wxt/src/cli/commands.ts +++ b/packages/wxt/src/cli/commands.ts @@ -9,6 +9,7 @@ import { const cli = cac('wxt'); cli.option('--debug', 'enable debug mode'); +cli.option('--level ', 'specify log level'); // DEV cli