From 66137d6d32022914e4b87fb998ff1925f608e82e Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 16 Aug 2022 13:04:14 +0200 Subject: [PATCH 1/4] feat(nuxi): auto cleanup with project manifest --- packages/nuxi/src/commands/cleanup.ts | 2 +- packages/nuxi/src/commands/dev.ts | 8 ++++ packages/nuxi/src/commands/upgrade.ts | 3 +- packages/nuxi/src/utils/fs.ts | 14 +------ packages/nuxi/src/utils/nuxt.ts | 59 +++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 15 deletions(-) create mode 100644 packages/nuxi/src/utils/nuxt.ts diff --git a/packages/nuxi/src/commands/cleanup.ts b/packages/nuxi/src/commands/cleanup.ts index 83018e5191a..46a257aac3e 100644 --- a/packages/nuxi/src/commands/cleanup.ts +++ b/packages/nuxi/src/commands/cleanup.ts @@ -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({ diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 4fa81e740ce..8002b8ec10f 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -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({ @@ -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) + } + await currentNuxt.ready() await currentNuxt.hooks.callHook('listen', listener.server, listener) diff --git a/packages/nuxi/src/commands/upgrade.ts b/packages/nuxi/src/commands/upgrade.ts index a10dd3812eb..796d9261fd5 100644 --- a/packages/nuxi/src/commands/upgrade.ts +++ b/packages/nuxi/src/commands/upgrade.ts @@ -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 { diff --git a/packages/nuxi/src/utils/fs.ts b/packages/nuxi/src/utils/fs.ts index 5074897f186..47b10555084 100644 --- a/packages/nuxi/src/utils/fs.ts +++ b/packages/nuxi/src/utils/fs.ts @@ -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 @@ -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 (rootDir: string, fn: (dir: string) => T | undefined): T | null { let dir = rootDir while (dir !== dirname(dir)) { diff --git a/packages/nuxi/src/utils/nuxt.ts b/packages/nuxi/src/utils/nuxt.ts new file mode 100644 index 00000000000..a5a2194eada --- /dev/null +++ b/packages/nuxi/src/utils/nuxt.ts @@ -0,0 +1,59 @@ +import { promises as fsp } from 'node:fs' +import { dirname } from 'node:path' +import { resolve } 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 { + 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 { + 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 +} From f24edca7f4124a8c69fcdb2b80cb2cc4a258d340 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 16 Aug 2022 13:11:42 +0200 Subject: [PATCH 2/4] refactor: use pathe --- packages/nuxi/src/utils/nuxt.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/nuxi/src/utils/nuxt.ts b/packages/nuxi/src/utils/nuxt.ts index a5a2194eada..12f7c452c0f 100644 --- a/packages/nuxi/src/utils/nuxt.ts +++ b/packages/nuxi/src/utils/nuxt.ts @@ -1,6 +1,5 @@ import { promises as fsp } from 'node:fs' -import { dirname } from 'node:path' -import { resolve } from 'pathe' +import { resolve, dirname } from 'pathe' import consola from 'consola' import { hash } from 'ohash' import type { Nuxt } from '@nuxt/schema' From b1e86568897748f17d462faa76f02673c96b0c64 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 16 Aug 2022 14:32:54 +0200 Subject: [PATCH 3/4] cleanup only once --- packages/nuxi/src/commands/dev.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 8002b8ec10f..85bab555ccb 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -77,10 +77,12 @@ export default defineNuxtCommand({ } // 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) + if (!isRestart) { + const previousManifest = await loadNuxtManifest(currentNuxt.options.buildDir) + const newManifest = await writeNuxtManifest(currentNuxt) + if (previousManifest && newManifest && previousManifest._hash !== newManifest._hash) { + await cleanupNuxtDirs(currentNuxt.options.rootDir) + } } await currentNuxt.ready() From 78b3c00083dd422fb83fb59289b3f6e11719a03d Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Tue, 16 Aug 2022 14:33:33 +0200 Subject: [PATCH 4/4] make comment better --- packages/nuxi/src/commands/dev.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nuxi/src/commands/dev.ts b/packages/nuxi/src/commands/dev.ts index 85bab555ccb..b7dc1767fed 100644 --- a/packages/nuxi/src/commands/dev.ts +++ b/packages/nuxi/src/commands/dev.ts @@ -76,7 +76,7 @@ export default defineNuxtCommand({ showURL() } - // Check if we need cache invalidation + // Write manifest and also check if we need cache invalidation if (!isRestart) { const previousManifest = await loadNuxtManifest(currentNuxt.options.buildDir) const newManifest = await writeNuxtManifest(currentNuxt)