Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: cleanup now that we've dropped Node 12 #8239

Merged
merged 4 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
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