feat(nuxi): nuxi cleanup command (#6125)

This commit is contained in:
pooya parsa 2022-07-25 17:19:17 +02:00 committed by GitHub
parent 133723ff76
commit 25ac652652
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 60 additions and 7 deletions

View File

@ -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.

View File

@ -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)
}
})

View File

@ -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),

View File

@ -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' })
}

View File

@ -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)) {