From b6ab87f6256dfeb40a9030a77654e7bffd06c73a Mon Sep 17 00:00:00 2001 From: SeanCassiere <33615041+SeanCassiere@users.noreply.github.com> Date: Sat, 7 Sep 2024 19:37:17 +1200 Subject: [PATCH] fix: remove the directories if they are empty --- packages/vinxi/lib/build.js | 52 ++++++++++++++++++++++++++++--------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/packages/vinxi/lib/build.js b/packages/vinxi/lib/build.js index 75839184..a4d854d9 100644 --- a/packages/vinxi/lib/build.js +++ b/packages/vinxi/lib/build.js @@ -2,7 +2,7 @@ import { mkdir, rm } from "fs/promises"; import { createRequire } from "module"; import { build, copyPublicAssets, createNitro, prerender } from "nitropack"; -import { readdirSync, writeFileSync } from "node:fs"; +import { existsSync, readdirSync, writeFileSync } from "node:fs"; import { pathToFileURL } from "node:url"; import { H3Event, createApp } from "../runtime/server.js"; @@ -322,24 +322,32 @@ export async function createBuild(app, buildConfig) { for (const router of app.config.routers.filter( (r) => r.type === "http" && r.target === "server", )) { - const assetsDir = join( - nitro.options.output.publicDir, - router.base, - "assets", - ); + const routerDir = join(nitro.options.output.publicDir, router.base); + const assetsDir = join(routerDir, "assets"); if (!existsSync(assetsDir)) { continue; } - const files = readdirSync(assetsDir); - for (const file of files) { + + let hasFilesDeleted = false; + const assetFiles = readdirSync(assetsDir); + for (const assetName of assetFiles) { if ( - file.endsWith(".js") || - file.endsWith(".mjs") || - file.endsWith(".cjs") + assetName.endsWith(".js") || + assetName.endsWith(".mjs") || + assetName.endsWith(".cjs") ) { - await rm(join(assetsDir, file)); + if (!hasFilesDeleted) { + hasFilesDeleted = true; + } + await rm(join(assetsDir, assetName)); } } + + // if the router dir is empty (including its subdirectories), remove it + // if the subdirectories are empty, they will be removed recursively + if (hasFilesDeleted) { + await deleteEmptyDirs(routerDir); + } } await app.hooks.callHook("app:build:nitro:assets:copy:end", { app, nitro }); @@ -792,3 +800,23 @@ function browserBuild() { }, }; } + +/** + * Recursively deletes empty directories starting from the given directory + * + * @param {string} dir + * @returns void + */ +async function deleteEmptyDirs(dir) { + const files = readdirSync(dir); + if (files.length === 0) { + await rm(dir); + return; + } + for (const file of files) { + const filePath = join(dir, file); + if (existsSync(filePath) && !file.startsWith(".")) { + deleteEmptyDirs(filePath); + } + } +}