From 29659a0cbb58e3d7a7fcb951ee50dd64ce24be49 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Mon, 23 May 2022 11:23:38 -0700 Subject: [PATCH] chore: cleanup now that we've dropped Node 12 (#8239) Co-authored-by: patak-dev --- packages/create-vite/index.js | 9 +------- packages/vite/src/node/optimizer/index.ts | 5 ++--- packages/vite/src/node/utils.ts | 25 ++++++----------------- 3 files changed, 9 insertions(+), 30 deletions(-) diff --git a/packages/create-vite/index.js b/packages/create-vite/index.js index 0efdbe6154824b..93ddb3c3f027a1 100755 --- a/packages/create-vite/index.js +++ b/packages/create-vite/index.js @@ -348,14 +348,7 @@ function emptyDir(dir) { return } for (const file of fs.readdirSync(dir)) { - const abs = path.resolve(dir, file) - // baseline is Node 12 so can't use rmSync :( - if (fs.lstatSync(abs).isDirectory()) { - emptyDir(abs) - fs.rmdirSync(abs) - } else { - fs.unlinkSync(abs) - } + fs.rmSync(path.resolve(dir, file), { recursive: true, force: true }) } } diff --git a/packages/vite/src/node/optimizer/index.ts b/packages/vite/src/node/optimizer/index.ts index b31cd7a1b595bf..0e71ba299e6727 100644 --- a/packages/vite/src/node/optimizer/index.ts +++ b/packages/vite/src/node/optimizer/index.ts @@ -16,7 +16,6 @@ import { normalizeId, normalizePath, removeDir, - removeDirSync, renameDir, writeFile } from '../utils' @@ -262,7 +261,7 @@ export function loadCachedDepOptimizationMetadata( } // Start with a fresh cache - removeDirSync(depsCacheDir) + fs.rmSync(depsCacheDir, { recursive: true, force: true }) } /** @@ -540,7 +539,7 @@ export async function runOptimizeDeps( } function cancel() { - removeDirSync(processingCacheDir) + fs.rmSync(processingCacheDir, { recursive: true, force: true }) } } diff --git a/packages/vite/src/node/utils.ts b/packages/vite/src/node/utils.ts index a3573d2a6dc1de..258551b838f271 100644 --- a/packages/vite/src/node/utils.ts +++ b/packages/vite/src/node/utils.ts @@ -504,14 +504,7 @@ export function emptyDir(dir: string, skip?: string[]): void { if (skip?.includes(file)) { continue } - const abs = path.resolve(dir, file) - // baseline is Node 12 so can't use rmSync :( - if (fs.lstatSync(abs).isDirectory()) { - emptyDir(abs) - fs.rmdirSync(abs) - } else { - fs.unlinkSync(abs) - } + fs.rmSync(path.resolve(dir, file), { recursive: true, force: true }) } } @@ -532,16 +525,11 @@ export function copyDir(srcDir: string, destDir: string): void { } } -export function removeDirSync(dir: string) { - if (fs.existsSync(dir)) { - const rmSync = fs.rmSync ?? fs.rmdirSync // TODO: Remove after support for Node 12 is dropped - rmSync(dir, { recursive: true }) - } -} - export const removeDir = isWindows ? promisify(gracefulRemoveDir) - : removeDirSync + : function removeDirSync(dir: string) { + fs.rmSync(dir, { recursive: true, force: true }) + } export const renameDir = isWindows ? promisify(gracefulRename) : fs.renameSync export function ensureWatchedFile( @@ -842,10 +830,9 @@ function gracefulRemoveDir( dir: string, cb: (error: NodeJS.ErrnoException | null) => void ) { - const rmdir = fs.rm ?? fs.rmdir // TODO: Remove after support for Node 12 is dropped const start = Date.now() let backoff = 0 - rmdir(dir, { recursive: true }, function CB(er) { + fs.rm(dir, { recursive: true }, function CB(er) { if (er) { if ( (er.code === 'ENOTEMPTY' || @@ -854,7 +841,7 @@ function gracefulRemoveDir( Date.now() - start < GRACEFUL_REMOVE_DIR_TIMEOUT ) { setTimeout(function () { - rmdir(dir, { recursive: true }, CB) + fs.rm(dir, { recursive: true }, CB) }, backoff) if (backoff < 100) backoff += 10 return