From 6f3f2705735a77543c5cc630e24890bccde2a086 Mon Sep 17 00:00:00 2001 From: Asakura Mizu Date: Fri, 28 Jun 2024 22:05:40 +0800 Subject: [PATCH] fix: Respect custom `outDir` when cleaning and zipping (#774) --- packages/wxt/src/cli/__tests__/index.test.ts | 22 ++++++++++- packages/wxt/src/cli/commands.ts | 3 +- packages/wxt/src/core/clean.ts | 39 ++++++++++++++----- .../src/core/utils/building/resolve-config.ts | 5 ++- 4 files changed, 56 insertions(+), 13 deletions(-) diff --git a/packages/wxt/src/cli/__tests__/index.test.ts b/packages/wxt/src/cli/__tests__/index.test.ts index 14a1a3c0..c3e5adb0 100644 --- a/packages/wxt/src/cli/__tests__/index.test.ts +++ b/packages/wxt/src/cli/__tests__/index.test.ts @@ -345,14 +345,32 @@ describe('CLI', () => { mockArgv('clean'); await importCli(); - expect(cleanMock).toBeCalledWith(undefined); + expect(cleanMock).toBeCalledWith({}); }); it('should respect passing a custom root', async () => { mockArgv('clean', 'path/to/root'); await importCli(); - expect(cleanMock).toBeCalledWith('path/to/root'); + expect(cleanMock).toBeCalledWith({ root: 'path/to/root' }); + }); + + it('should respect a custom config file', async () => { + mockArgv('clean', '-c', './path/to/config.ts'); + await importCli(); + + expect(cleanMock).toBeCalledWith({ + configFile: './path/to/config.ts', + }); + }); + + it('should respect passing --debug', async () => { + mockArgv('clean', '--debug'); + await importCli(); + + expect(cleanMock).toBeCalledWith({ + debug: true, + }); }); }); diff --git a/packages/wxt/src/cli/commands.ts b/packages/wxt/src/cli/commands.ts index c17a39d0..e204e423 100644 --- a/packages/wxt/src/cli/commands.ts +++ b/packages/wxt/src/cli/commands.ts @@ -126,9 +126,10 @@ cli cli .command('clean [root]', 'clean generated files and caches') .alias('cleanup') + .option('-c, --config ', 'use specified config file') .action( wrapAction(async (root, flags) => { - await clean(root); + await clean({ root, configFile: flags.config, debug: flags.debug }); }), ); diff --git a/packages/wxt/src/core/clean.ts b/packages/wxt/src/core/clean.ts index 261283e0..7befd378 100644 --- a/packages/wxt/src/core/clean.ts +++ b/packages/wxt/src/core/clean.ts @@ -1,44 +1,65 @@ import path from 'node:path'; import glob from 'fast-glob'; import fs from 'fs-extra'; -import { consola } from 'consola'; import pc from 'picocolors'; +import { InlineConfig } from '~/types'; +import { registerWxt, wxt } from './wxt'; /** * Remove generated/temp files from the directory. * + * @param config Optional config that will override your `/wxt.config.ts`. + * + * @example + * await clean(); + */ +export async function clean(config?: InlineConfig): Promise; +/** + * Remove generated/temp files from the directory. + * + * @deprecated + * * @param root The directory to look for generated/temp files in. Defaults to `process.cwd()`. Can be relative to `process.cwd()` or absolute. * * @example * await clean(); */ -export async function clean(root = process.cwd()) { - consola.info('Cleaning Project'); +export async function clean(root?: string): Promise; + +export async function clean(config?: string | InlineConfig) { + if (typeof config === 'string') { + config = { root: config }; + } + + await registerWxt('build', config); + wxt.logger.info('Cleaning Project'); + + const root = wxt.config.root; const tempDirs = [ 'node_modules/.vite', 'node_modules/.cache', '**/.wxt', - '.output/*', + `${path.relative(root, wxt.config.outBaseDir)}/*`, ]; - consola.debug('Looking for:', tempDirs.map(pc.cyan).join(', ')); + wxt.logger.debug('Looking for:', tempDirs.map(pc.cyan).join(', ')); const directories = await glob(tempDirs, { - cwd: path.resolve(root), + cwd: root, absolute: true, onlyDirectories: true, deep: 2, }); if (directories.length === 0) { - consola.debug('No generated files found.'); + wxt.logger.debug('No generated files found.'); return; } - consola.debug( + wxt.logger.debug( 'Found:', directories.map((dir) => pc.cyan(path.relative(root, dir))).join(', '), ); for (const directory of directories) { await fs.rm(directory, { force: true, recursive: true }); - consola.debug('Deleted ' + pc.cyan(path.relative(root, directory))); + wxt.logger.debug('Deleted ' + pc.cyan(path.relative(root, directory))); } } diff --git a/packages/wxt/src/core/utils/building/resolve-config.ts b/packages/wxt/src/core/utils/building/resolve-config.ts index 3f7f52a4..3535b865 100644 --- a/packages/wxt/src/core/utils/building/resolve-config.ts +++ b/packages/wxt/src/core/utils/building/resolve-config.ts @@ -167,7 +167,7 @@ export async function resolveConfig( srcDir, typesDir, wxtDir, - zip: resolveZipConfig(root, mergedConfig), + zip: resolveZipConfig(root, outBaseDir, mergedConfig), transformManifest: mergedConfig.transformManifest, analysis: resolveAnalysisConfig(root, mergedConfig), userConfigMetadata: userConfigMetadata ?? {}, @@ -241,6 +241,7 @@ async function mergeInlineConfig( function resolveZipConfig( root: string, + outBaseDir: string, mergedConfig: InlineConfig, ): NullablyRequired { const downloadedPackagesDir = path.resolve(root, '.wxt/local_modules'); @@ -261,6 +262,8 @@ function resolveZipConfig( // Tests '**/__tests__/**', '**/*.+(test|spec).?(c|m)+(j|t)s?(x)', + // Output directory + `${path.relative(root, outBaseDir)}/**`, // From user ...(mergedConfig.zip?.excludeSources ?? []), ],