Skip to content

Commit

Permalink
chore: cleanup now that we've dropped Node 12 (#8239)
Browse files Browse the repository at this point in the history
Co-authored-by: patak-dev <matias.capeletto@gmail.com>
  • Loading branch information
benmccann and patak-dev committed May 23, 2022
1 parent c1e0132 commit 29659a0
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 30 deletions.
9 changes: 1 addition & 8 deletions packages/create-vite/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
normalizeId,
normalizePath,
removeDir,
removeDirSync,
renameDir,
writeFile
} from '../utils'
Expand Down Expand Up @@ -262,7 +261,7 @@ export function loadCachedDepOptimizationMetadata(
}

// Start with a fresh cache
removeDirSync(depsCacheDir)
fs.rmSync(depsCacheDir, { recursive: true, force: true })
}

/**
Expand Down Expand Up @@ -540,7 +539,7 @@ export async function runOptimizeDeps(
}

function cancel() {
removeDirSync(processingCacheDir)
fs.rmSync(processingCacheDir, { recursive: true, force: true })
}
}

Expand Down
25 changes: 6 additions & 19 deletions packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}

Expand All @@ -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(
Expand Down Expand Up @@ -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' ||
Expand All @@ -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
Expand Down

0 comments on commit 29659a0

Please sign in to comment.