diff --git a/docs/content/3.api/5.commands/cleanup.md b/docs/content/3.api/5.commands/cleanup.md new file mode 100644 index 0000000000..2c06a4f7b3 --- /dev/null +++ b/docs/content/3.api/5.commands/cleanup.md @@ -0,0 +1,16 @@ +# `nuxi cleanup` + +```{bash} +npx nuxi clean|cleanup [rootDir] +``` + +The `cleanup` command, removes common generated nuxt files and caches. Including: + +- `.nuxt` +- `.output` +- `node_modules/.vite` +- `node_modules/.cache` + +Option | Default | Description +-------------------------|-----------------|------------------ +`rootDir` | `.` | The root directory of the project. diff --git a/packages/nuxi/src/commands/cleanup.ts b/packages/nuxi/src/commands/cleanup.ts new file mode 100644 index 0000000000..83018e5191 --- /dev/null +++ b/packages/nuxi/src/commands/cleanup.ts @@ -0,0 +1,15 @@ +import { resolve } from 'pathe' +import { cleanupNuxtDirs } from '../utils/fs' +import { defineNuxtCommand } from './index' + +export default defineNuxtCommand({ + meta: { + name: 'cleanup', + usage: 'npx nuxi clean|cleanup', + description: 'Cleanup generated nuxt files and caches' + }, + async invoke (args) { + const rootDir = resolve(args._[0] || '.') + await cleanupNuxtDirs(rootDir) + } +}) diff --git a/packages/nuxi/src/commands/index.ts b/packages/nuxi/src/commands/index.ts index 1440b5bf54..b2469131f0 100644 --- a/packages/nuxi/src/commands/index.ts +++ b/packages/nuxi/src/commands/index.ts @@ -5,6 +5,8 @@ const _rDefault = r => r.default || r export const commands = { dev: () => import('./dev').then(_rDefault), build: () => import('./build').then(_rDefault), + cleanup: () => import('./cleanup').then(_rDefault), + clean: () => import('./cleanup').then(_rDefault), preview: () => import('./preview').then(_rDefault), start: () => import('./preview').then(_rDefault), analyze: () => import('./analyze').then(_rDefault), diff --git a/packages/nuxi/src/commands/upgrade.ts b/packages/nuxi/src/commands/upgrade.ts index ecf1576af6..4b62ffc5b0 100644 --- a/packages/nuxi/src/commands/upgrade.ts +++ b/packages/nuxi/src/commands/upgrade.ts @@ -1,9 +1,10 @@ import { execSync } from 'node:child_process' -import { promises as fsp, existsSync } from 'node:fs' +import { promises as fsp } from 'node:fs' import consola from 'consola' import { resolve } from 'pathe' import { resolveModule } from '../utils/cjs' import { getPackageManager, packageManagerLocks } from '../utils/packageManagers' +import { cleanupNuxtDirs, rmRecursive } from '../utils/fs' import { defineNuxtCommand } from './index' async function getNuxtVersion (paths: string | string[]) { @@ -37,16 +38,16 @@ export default defineNuxtCommand({ if (args.force || args.f) { consola.info('Removing lock-file and node_modules...') - await Promise.all([ - fsp.rm(packageManagerLocks[packageManager]), + await rmRecursive([ + packageManagerLocks[packageManager], fsp.rm('node_modules', { recursive: true }) ]) + await cleanupNuxtDirs(rootDir) + consola.info('Installing nuxt...') execSync(`${packageManager} install`, { stdio: 'inherit' }) } else { + await cleanupNuxtDirs(rootDir) consola.info('Upgrading nuxt...') - await Promise.all(['node_modules/.cache', resolve(rootDir, '.nuxt'), 'node_modules/.vite'].map((path) => { - return existsSync(path) ? fsp.rm(path, { recursive: true }) : undefined - })) execSync(`${packageManager} ${packageManager === 'yarn' ? 'add' : 'install'} -D nuxt@rc`, { stdio: 'inherit' }) } diff --git a/packages/nuxi/src/utils/fs.ts b/packages/nuxi/src/utils/fs.ts index b4ec085bc3..17832932d4 100644 --- a/packages/nuxi/src/utils/fs.ts +++ b/packages/nuxi/src/utils/fs.ts @@ -1,5 +1,6 @@ import { promises as fsp } from 'node:fs' -import { dirname } from 'pathe' +import { dirname, resolve } from 'pathe' +import consola from 'consola' // Check if a file exists export async function exists (path: string) { @@ -16,6 +17,24 @@ export async function clearDir (path: string) { await fsp.mkdir(path, { recursive: true }) } +export async function rmRecursive (paths: string[]) { + await Promise.all(paths.map(async (path) => { + await fsp.rm(path, { recursive: true, force: true }) + })) +} + +export async function cleanupNuxtDirs (rootDir: string) { + consola.info('Cleaning up generated nuxt files and caches...') + + await rmRecursive([ + '.nuxt', + '.output', + 'dist', + 'node_modules/.vite', + 'node_modules/.cache' + ].map(dir => resolve(rootDir, dir))) +} + export function findup (rootDir: string, fn: (dir: string) => T | undefined): T | null { let dir = rootDir while (dir !== dirname(dir)) {