From d05f126eb316862dcd9cb4b1ff0df1d4ea1cee05 Mon Sep 17 00:00:00 2001 From: Aaron Date: Sun, 13 Aug 2023 14:04:43 -0500 Subject: [PATCH] feat: Add `--debug` flag for printing debug logs for all CLI commands (#75) --- src/cli/commands/build.ts | 2 ++ src/cli/commands/dev.ts | 2 ++ src/cli/commands/init.ts | 5 ++++- src/cli/commands/prepare.ts | 2 ++ src/cli/commands/publish.ts | 5 ++++- src/cli/commands/zip.ts | 2 ++ src/cli/index.ts | 2 ++ src/cli/utils/defineCommand.ts | 9 ++++++++- src/core/types/external.ts | 8 ++++++++ src/core/types/internal.ts | 1 + src/core/utils/getInternalConfig.ts | 5 ++++- src/core/vite-plugins/multipageMove.ts | 4 +++- src/testing/fake-objects.ts | 1 + 13 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/cli/commands/build.ts b/src/cli/commands/build.ts index fd246d39..cda45d7d 100644 --- a/src/cli/commands/build.ts +++ b/src/cli/commands/build.ts @@ -10,6 +10,7 @@ export const build = defineCommand< browser?: wxt.TargetBrowser; mv3?: boolean; mv2?: boolean; + debug?: boolean; }, ] >(async (root, flags) => { @@ -20,6 +21,7 @@ export const build = defineCommand< browser: flags.browser, manifestVersion: flags.mv3 ? 3 : flags.mv2 ? 2 : undefined, configFile: flags.config, + debug: flags.debug, }; await wxt.build(cliConfig); diff --git a/src/cli/commands/dev.ts b/src/cli/commands/dev.ts index 8868d84b..a1dc0b9c 100644 --- a/src/cli/commands/dev.ts +++ b/src/cli/commands/dev.ts @@ -10,6 +10,7 @@ export const dev = defineCommand< browser?: wxt.TargetBrowser; mv3?: boolean; mv2?: boolean; + debug?: boolean; }, ] >(async (root, flags) => { @@ -20,6 +21,7 @@ export const dev = defineCommand< browser: flags.browser, manifestVersion: flags.mv3 ? 3 : flags.mv2 ? 2 : undefined, configFile: flags.config, + debug: flags.debug, }; const server = await wxt.createServer(cliConfig); diff --git a/src/cli/commands/init.ts b/src/cli/commands/init.ts index 34744b8e..d2cc594e 100644 --- a/src/cli/commands/init.ts +++ b/src/cli/commands/init.ts @@ -9,7 +9,10 @@ import pc from 'picocolors'; import { Formatter } from 'picocolors/types'; export const init = defineCommand< - [directory: string | undefined, options: { template?: string; pm?: string }] + [ + directory: string | undefined, + options: { template?: string; pm?: string; debug?: boolean }, + ] >( async (userDirectory, flags) => { consola.info('Initalizing new project'); diff --git a/src/cli/commands/prepare.ts b/src/cli/commands/prepare.ts index 06bcbe45..d9aaddf0 100644 --- a/src/cli/commands/prepare.ts +++ b/src/cli/commands/prepare.ts @@ -9,12 +9,14 @@ export const prepare = defineCommand< root: string | undefined, flags: { config?: string; + debug?: boolean; }, ] >(async (root, flags) => { const cliConfig: wxt.InlineConfig = { root, configFile: flags.config, + debug: flags.debug, }; const config = await getInternalConfig(cliConfig, 'build'); diff --git a/src/cli/commands/publish.ts b/src/cli/commands/publish.ts index 281be606..2e150668 100644 --- a/src/cli/commands/publish.ts +++ b/src/cli/commands/publish.ts @@ -2,7 +2,10 @@ import { consola } from 'consola'; import { defineCommand } from '../utils/defineCommand'; export const publish = defineCommand( - async (root: any, { config: configFile }: any) => { + async ( + root: any, + { config: configFile, debug }: { config?: string; debug?: string }, + ) => { consola.warn('wxt publish: Not implemented'); }, ); diff --git a/src/cli/commands/zip.ts b/src/cli/commands/zip.ts index e1856952..e53284f6 100644 --- a/src/cli/commands/zip.ts +++ b/src/cli/commands/zip.ts @@ -13,6 +13,7 @@ export const zip = defineCommand< browser?: wxt.TargetBrowser; mv3?: boolean; mv2?: boolean; + debug?: boolean; }, ] >(async (root, flags) => { @@ -23,6 +24,7 @@ export const zip = defineCommand< browser: flags.browser, manifestVersion: flags.mv3 ? 3 : flags.mv2 ? 2 : undefined, configFile: flags.config, + debug: flags.debug, }; const config = await getInternalConfig(cliConfig, 'build'); diff --git a/src/cli/index.ts b/src/cli/index.ts index 0b3939c4..def74eb7 100644 --- a/src/cli/index.ts +++ b/src/cli/index.ts @@ -6,6 +6,8 @@ const cli = cac('wxt'); cli.help(); cli.version(version); +cli.option('--debug', 'enable debug mode'); + // DEV cli .command('[root]', 'start dev server') diff --git a/src/cli/utils/defineCommand.ts b/src/cli/utils/defineCommand.ts index 48675495..3e8849a5 100644 --- a/src/cli/utils/defineCommand.ts +++ b/src/cli/utils/defineCommand.ts @@ -1,4 +1,4 @@ -import { consola } from 'consola'; +import { LogLevels, consola } from 'consola'; import { printHeader } from '../../core/log/printHeader'; import { formatDuration } from '../../core/utils/formatDuration'; @@ -9,6 +9,13 @@ export function defineCommand( }, ) { return async (...args: TArgs) => { + // 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); + if (isDebug) { + consola.level = LogLevels.debug; + } + const startTime = Date.now(); try { printHeader(); diff --git a/src/core/types/external.ts b/src/core/types/external.ts index 353f7338..1b803d57 100644 --- a/src/core/types/external.ts +++ b/src/core/types/external.ts @@ -2,6 +2,7 @@ import * as vite from 'vite'; import { Manifest, Scripting } from 'webextension-polyfill'; import { UnimportOptions } from 'unimport'; import { EntrypointGroup } from '.'; +import { LogLevel } from 'consola'; export interface InlineConfig { /** @@ -34,6 +35,12 @@ export interface InlineConfig { * @default "wxt.config.ts" */ configFile?: string | false; + /** + * Set to `true` to show debug logs. Overriden by the command line `--debug` option. + * + * @default false + */ + debug?: boolean; /** * ID of the extension for each store. Used for publishing. */ @@ -218,6 +225,7 @@ export interface Logger { error(...args: any[]): void; fatal(...args: any[]): void; success(...args: any[]): void; + level: LogLevel; } export interface BaseEntrypoint { diff --git a/src/core/types/internal.ts b/src/core/types/internal.ts index 0a67bbd9..129cd5af 100644 --- a/src/core/types/internal.ts +++ b/src/core/types/internal.ts @@ -20,6 +20,7 @@ export interface InternalConfig { entrypointsDir: string; outBaseDir: string; outDir: string; + debug: boolean; storeIds: { chrome?: string; firefox?: string; diff --git a/src/core/utils/getInternalConfig.ts b/src/core/utils/getInternalConfig.ts index 5ee638d4..1846b0d2 100644 --- a/src/core/utils/getInternalConfig.ts +++ b/src/core/utils/getInternalConfig.ts @@ -9,7 +9,7 @@ import { } from '../types'; import path, { resolve } from 'node:path'; import * as vite from 'vite'; -import { consola } from 'consola'; +import { LogLevels, consola } from 'consola'; import * as plugins from '../vite-plugins'; import { createFsCache } from './createFsCache'; import { getGlobals } from './globals'; @@ -33,6 +33,8 @@ export async function getInternalConfig( const outBaseDir = path.resolve(root, '.output'); const outDir = path.resolve(outBaseDir, `${browser}-mv${manifestVersion}`); const logger = config.logger ?? consola; + const debug = !!config.debug; + if (debug) logger.level = LogLevels.debug; const baseConfig: InternalConfigNoUserDirs = { root, @@ -43,6 +45,7 @@ export async function getInternalConfig( manifestVersion, mode, command, + debug, logger, vite: config.vite ?? {}, imports: config.imports ?? {}, diff --git a/src/core/vite-plugins/multipageMove.ts b/src/core/vite-plugins/multipageMove.ts index b6ae253e..430190c3 100644 --- a/src/core/vite-plugins/multipageMove.ts +++ b/src/core/vite-plugins/multipageMove.ts @@ -33,7 +33,9 @@ export function multipageMove( (entry) => !!normalizePath(entry.inputPath).endsWith(oldBundlePath), ); if (entrypoint == null) { - config.logger.debug('No entrypoint found for', oldBundlePath); + config.logger.debug( + `No entrypoint found for ${oldBundlePath}, leaving in chunks directory`, + ); continue; } diff --git a/src/testing/fake-objects.ts b/src/testing/fake-objects.ts index f673c94b..ae57b4d0 100644 --- a/src/testing/fake-objects.ts +++ b/src/testing/fake-objects.ts @@ -212,6 +212,7 @@ export const fakeInternalConfig = fakeObjectCreator(() => ({ runnerConfig: { config: {}, }, + debug: faker.datatype.boolean(), srcDir: fakeDir(), storeIds: {}, typesDir: fakeDir(),