From 08c77cf6369150fc44d5961bd8f487f7e71df3da Mon Sep 17 00:00:00 2001 From: Kubi Date: Wed, 8 Jan 2025 23:22:22 +0100 Subject: [PATCH] feat: additional dirs for cleanDir command --- src/commands/cleanup.ts | 11 ++++++++++- src/utils/nuxt.ts | 32 +++++++++++++++++++------------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/src/commands/cleanup.ts b/src/commands/cleanup.ts index aeb0debf..8be1f8ae 100644 --- a/src/commands/cleanup.ts +++ b/src/commands/cleanup.ts @@ -13,11 +13,20 @@ export default defineCommand({ args: { ...cwdArgs, ...legacyRootDirArgs, + cleanDir: { + type: 'string', + description: 'Additional directories to clean up', + }, }, async run(ctx) { const cwd = resolve(ctx.args.cwd || ctx.args.rootDir) const { loadNuxtConfig } = await loadKit(cwd) const nuxtOptions = await loadNuxtConfig({ cwd, overrides: { dev: true } }) - await cleanupNuxtDirs(nuxtOptions.rootDir, nuxtOptions.buildDir) + + const customDirs = ctx.args.cleanDir + ? ctx.args.cleanDir.split(',').map((dir) => resolve(cwd, dir.trim())) + : [] + + await cleanupNuxtDirs(nuxtOptions.rootDir, nuxtOptions.buildDir, customDirs) }, }) diff --git a/src/utils/nuxt.ts b/src/utils/nuxt.ts index c0b5e29a..6d0d5822 100644 --- a/src/utils/nuxt.ts +++ b/src/utils/nuxt.ts @@ -18,18 +18,24 @@ interface NuxtProjectManifest { } } -export async function cleanupNuxtDirs(rootDir: string, buildDir: string) { +export async function cleanupNuxtDirs( + rootDir: string, + buildDir: string, + customDirs?: string[] +) { logger.info('Cleaning up generated Nuxt files and caches...') - await rmRecursive( - [ - buildDir, - '.output', - 'dist', - 'node_modules/.vite', - 'node_modules/.cache', - ].map(dir => resolve(rootDir, dir)), - ) + const defaultDirs = [ + buildDir, + '.output', + 'dist', + 'node_modules/.vite', + 'node_modules/.cache', + ].map((dir) => resolve(rootDir, dir)) + + const dirsToClean = [...defaultDirs, ...(customDirs || [])] + + await rmRecursive(dirsToClean) } export function nuxtVersionToGitIdentifier(version: string) { @@ -57,7 +63,7 @@ function resolveNuxtManifest(nuxt: Nuxt): NuxtProjectManifest { } export async function writeNuxtManifest( - nuxt: Nuxt, + nuxt: Nuxt ): Promise { const manifest = resolveNuxtManifest(nuxt) const manifestPath = resolve(nuxt.options.buildDir, 'nuxt.json') @@ -67,12 +73,12 @@ export async function writeNuxtManifest( } export async function loadNuxtManifest( - buildDir: string, + buildDir: string ): Promise { const manifestPath = resolve(buildDir, 'nuxt.json') const manifest: NuxtProjectManifest | null = await fsp .readFile(manifestPath, 'utf-8') - .then(data => JSON.parse(data) as NuxtProjectManifest) + .then((data) => JSON.parse(data) as NuxtProjectManifest) .catch(() => null) return manifest }