Skip to content

Commit

Permalink
feat(nuxi): nuxi cleanup command (nuxt#6125)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 authored Jul 25, 2022
1 parent 133723f commit 25ac652
Showing 5 changed files with 60 additions and 7 deletions.
16 changes: 16 additions & 0 deletions docs/content/3.api/5.commands/cleanup.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 15 additions & 0 deletions packages/nuxi/src/commands/cleanup.ts
Original file line number Diff line number Diff line change
@@ -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)
}
})
2 changes: 2 additions & 0 deletions packages/nuxi/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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),
13 changes: 7 additions & 6 deletions packages/nuxi/src/commands/upgrade.ts
Original file line number Diff line number Diff line change
@@ -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' })
}

21 changes: 20 additions & 1 deletion packages/nuxi/src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -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<T> (rootDir: string, fn: (dir: string) => T | undefined): T | null {
let dir = rootDir
while (dir !== dirname(dir)) {

0 comments on commit 25ac652

Please sign in to comment.