feat: Add log --level cli option (#1973)

Co-authored-by: nickbar01234 <nickbar01234gmail.com>
Co-authored-by: Aaron <aaronklinker1@gmail.com>
This commit is contained in:
Nick Doan
2026-02-10 23:57:27 -05:00
committed by GitHub
parent 9470d2b696
commit 3417bd3ed2
3 changed files with 86 additions and 2 deletions
+76 -1
View File
@@ -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);
});
});
+6 -1
View File
@@ -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);
+4
View File
@@ -9,6 +9,10 @@ import {
const cli = cac('wxt');
cli.option('--debug', 'enable debug mode');
cli.option(
'--level <level>',
'specify log level ("silent" | "fatal" | "error" | "warn" | "log" | "info" | "success" | "fail" | "ready" | "start" | "box" | "debug" | "trace" | "verbose")',
);
// DEV
cli