diff --git a/packages/wxt/src/cli/__tests__/index.test.ts b/packages/wxt/src/cli/__tests__/index.test.ts index ec9ec27f..26ef0e6c 100644 --- a/packages/wxt/src/cli/__tests__/index.test.ts +++ b/packages/wxt/src/cli/__tests__/index.test.ts @@ -8,7 +8,7 @@ import { initialize, } from '../../core'; import { mock } from 'vitest-mock-extended'; -import consola from 'consola'; +import consola, { LogLevels } from 'consola'; vi.mock('../../core/build'); const buildMock = vi.mocked(build); @@ -139,6 +139,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); }); }); @@ -231,6 +246,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); }); }); @@ -312,6 +342,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 () => { @@ -381,6 +426,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); }); }); @@ -415,6 +475,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 690e40f0..1347df33 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 d9e8c82a..be5de7ba 100644 --- a/packages/wxt/src/cli/commands.ts +++ b/packages/wxt/src/cli/commands.ts @@ -9,6 +9,10 @@ import { const cli = cac('wxt'); cli.option('--debug', 'enable debug mode'); +cli.option( + '--level ', + 'specify log level ("silent" | "fatal" | "error" | "warn" | "log" | "info" | "success" | "fail" | "ready" | "start" | "box" | "debug" | "trace" | "verbose")', +); // DEV cli