Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxi): auto cleanup with project manifest changes #6672

Merged
merged 4 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/nuxi/src/commands/cleanup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { resolve } from 'pathe'
import { cleanupNuxtDirs } from '../utils/fs'
import { cleanupNuxtDirs } from '../utils/nuxt'
import { defineNuxtCommand } from './index'

export default defineNuxtCommand({
Expand Down
8 changes: 8 additions & 0 deletions packages/nuxi/src/commands/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { writeTypes } from '../utils/prepare'
import { loadKit } from '../utils/kit'
import { importModule } from '../utils/cjs'
import { overrideEnv } from '../utils/env'
import { writeNuxtManifest, loadNuxtManifest, cleanupNuxtDirs } from '../utils/nuxt'
import { defineNuxtCommand } from './index'

export default defineNuxtCommand({
Expand Down Expand Up @@ -75,6 +76,13 @@ export default defineNuxtCommand({
showURL()
}

// Check if we need cache invalidation
const previousManifest = await loadNuxtManifest(currentNuxt.options.buildDir)
const newManifest = await writeNuxtManifest(currentNuxt)
if (previousManifest && newManifest && previousManifest._hash !== newManifest._hash) {
await cleanupNuxtDirs(currentNuxt.options.rootDir)
}
pi0 marked this conversation as resolved.
Show resolved Hide resolved

await currentNuxt.ready()

await currentNuxt.hooks.callHook('listen', listener.server, listener)
Expand Down
3 changes: 2 additions & 1 deletion packages/nuxi/src/commands/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import consola from 'consola'
import { resolve } from 'pathe'
import { resolveModule } from '../utils/cjs'
import { getPackageManager, packageManagerLocks } from '../utils/packageManagers'
import { cleanupNuxtDirs, rmRecursive, touchFile } from '../utils/fs'
import { rmRecursive, touchFile } from '../utils/fs'
import { cleanupNuxtDirs } from '../utils/nuxt'
import { defineNuxtCommand } from './index'

async function getNuxtVersion (paths: string | string[]): Promise<string|null> {
Expand Down
14 changes: 1 addition & 13 deletions packages/nuxi/src/utils/fs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { promises as fsp } from 'node:fs'
import { dirname, resolve } from 'pathe'
import { dirname } from 'pathe'
import consola from 'consola'

// Check if a file exists
Expand Down Expand Up @@ -29,18 +29,6 @@ export async function touchFile (path: string) {
await fsp.utimes(path, time, time).catch(() => {})
}

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)) {
Expand Down
58 changes: 58 additions & 0 deletions packages/nuxi/src/utils/nuxt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { promises as fsp } from 'node:fs'
import { resolve, dirname } from 'pathe'
import consola from 'consola'
import { hash } from 'ohash'
import type { Nuxt } from '@nuxt/schema'
import { rmRecursive } from './fs'

export interface NuxtProjectManifest {
_hash: string
project: {
rootDir: string
},
versions: {
nuxt: string
}
}

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 resolveNuxtManifest (nuxt: Nuxt): NuxtProjectManifest {
const manifest: NuxtProjectManifest = {
_hash: null,
project: {
rootDir: nuxt.options.rootDir
},
versions: {
nuxt: nuxt._version
}
}
manifest._hash = hash(manifest)
return manifest
}

export async function writeNuxtManifest (nuxt: Nuxt): Promise<NuxtProjectManifest> {
const manifest = resolveNuxtManifest(nuxt)
const manifestPath = resolve(nuxt.options.buildDir, 'nuxt.json')
await fsp.mkdir(dirname(manifestPath), { recursive: true })
await fsp.writeFile(manifestPath, JSON.stringify(manifest, null, 2), 'utf-8')
return manifest
}

export async function loadNuxtManifest (buildDir: string): Promise<NuxtProjectManifest | null> {
const manifestPath = resolve(buildDir, 'nuxt.json')
const manifest: NuxtProjectManifest | null = await fsp.readFile(manifestPath, 'utf-8')
.then(data => JSON.parse(data) as NuxtProjectManifest)
.catch(() => null)
return manifest
}
pi0 marked this conversation as resolved.
Show resolved Hide resolved