Skip to content

Commit 3417bd3

Browse files
feat: Add log --level cli option (#1973)
Co-authored-by: nickbar01234 <nickbar01234gmail.com> Co-authored-by: Aaron <aaronklinker1@gmail.com>
1 parent 9470d2b commit 3417bd3

File tree

3 files changed

+86
-2
lines changed

3 files changed

+86
-2
lines changed

packages/wxt/src/cli/__tests__/index.test.ts

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
initialize,
99
} from '../../core';
1010
import { mock } from 'vitest-mock-extended';
11-
import consola from 'consola';
11+
import consola, { LogLevels } from 'consola';
1212

1313
vi.mock('../../core/build');
1414
const buildMock = vi.mocked(build);
@@ -139,6 +139,21 @@ describe('CLI', () => {
139139
expect(createServerMock).toBeCalledWith({
140140
debug: true,
141141
});
142+
expect(consola.level).toBe(LogLevels.debug);
143+
});
144+
145+
it('should set log --level', async () => {
146+
mockArgv('--level', 'warn');
147+
await importCli();
148+
149+
expect(consola.level).toBe(LogLevels.warn);
150+
});
151+
152+
it('--debug should override --level', async () => {
153+
mockArgv('--debug', '--level', 'silent');
154+
await importCli();
155+
156+
expect(consola.level).toBe(LogLevels.debug);
142157
});
143158
});
144159

@@ -231,6 +246,21 @@ describe('CLI', () => {
231246
expect(buildMock).toBeCalledWith({
232247
debug: true,
233248
});
249+
expect(consola.level).toBe(LogLevels.debug);
250+
});
251+
252+
it('should set log --level', async () => {
253+
mockArgv('build', '--level', 'warn');
254+
await importCli();
255+
256+
expect(consola.level).toBe(LogLevels.warn);
257+
});
258+
259+
it('--debug should override --level', async () => {
260+
mockArgv('build', '--debug', '--level', 'silent');
261+
await importCli();
262+
263+
expect(consola.level).toBe(LogLevels.debug);
234264
});
235265
});
236266

@@ -312,6 +342,21 @@ describe('CLI', () => {
312342
debug: true,
313343
zip: {},
314344
});
345+
expect(consola.level).toBe(LogLevels.debug);
346+
});
347+
348+
it('should set log --level', async () => {
349+
mockArgv('zip', '--level', 'warn');
350+
await importCli();
351+
352+
expect(consola.level).toBe(LogLevels.warn);
353+
});
354+
355+
it('--debug should override --level', async () => {
356+
mockArgv('zip', '--debug', '--level', 'silent');
357+
await importCli();
358+
359+
expect(consola.level).toBe(LogLevels.debug);
315360
});
316361

317362
it('should pass undefined for zipSources when --sources is not passed', async () => {
@@ -381,6 +426,21 @@ describe('CLI', () => {
381426
expect(prepareMock).toBeCalledWith({
382427
debug: true,
383428
});
429+
expect(consola.level).toBe(LogLevels.debug);
430+
});
431+
432+
it('should set log --level', async () => {
433+
mockArgv('prepare', '--level', 'warn');
434+
await importCli();
435+
436+
expect(consola.level).toBe(LogLevels.warn);
437+
});
438+
439+
it('--debug should override --level', async () => {
440+
mockArgv('prepare', '--debug', '--level', 'silent');
441+
await importCli();
442+
443+
expect(consola.level).toBe(LogLevels.debug);
384444
});
385445
});
386446

@@ -415,6 +475,21 @@ describe('CLI', () => {
415475
expect(cleanMock).toBeCalledWith({
416476
debug: true,
417477
});
478+
expect(consola.level).toBe(LogLevels.debug);
479+
});
480+
481+
it('should set log --level', async () => {
482+
mockArgv('clean', '--level', 'warn');
483+
await importCli();
484+
485+
expect(consola.level).toBe(LogLevels.warn);
486+
});
487+
488+
it('--debug should override --level', async () => {
489+
mockArgv('clean', '--debug', '--level', 'silent');
490+
await importCli();
491+
492+
expect(consola.level).toBe(LogLevels.debug);
418493
});
419494
});
420495

packages/wxt/src/cli/cli-utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { CAC, Command } from 'cac';
2-
import consola, { LogLevels } from 'consola';
2+
import consola, { LogLevels, LogType } from 'consola';
33
import { filterTruthy, toArray } from '../core/utils/arrays';
44
import { printHeader } from '../core/utils/log';
55
import { formatDuration } from '../core/utils/time';
@@ -19,6 +19,11 @@ export function wrapAction(
1919
},
2020
) {
2121
return async (...args: any[]) => {
22+
const level: LogType | undefined = args.find((arg) => arg?.level)?.level;
23+
if (level && Object.keys(LogLevels).includes(level)) {
24+
consola.level = LogLevels[level];
25+
}
26+
2227
// Enable consola's debug mode globally at the start of all commands when
2328
// the `--debug` flag is passed
2429
const isDebug = !!args.find((arg) => arg?.debug);

packages/wxt/src/cli/commands.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import {
99
const cli = cac('wxt');
1010

1111
cli.option('--debug', 'enable debug mode');
12+
cli.option(
13+
'--level <level>',
14+
'specify log level ("silent" | "fatal" | "error" | "warn" | "log" | "info" | "success" | "fail" | "ready" | "start" | "box" | "debug" | "trace" | "verbose")',
15+
);
1216

1317
// DEV
1418
cli

0 commit comments

Comments
 (0)