Skip to content

Commit

Permalink
chore: refactor to use fs.promises.rm() (#54076)
Browse files Browse the repository at this point in the history
Since Next.js already requires [`node@16.8.0`](https://github.com/vercel/next.js/blob/88b8d15f41f894011cf33af0b1f4a3fb24aed46b/packages/next/package.json#L319) or newer, we can drop old techniques for removing a directory recursively and instead rely on the native [`fs.promises.rm()`](https://nodejs.org/api/fs.html#fspromisesrmpath-options) function (available since `node@14.14.0`).

x-ref: #34210
  • Loading branch information
styfle committed Aug 16, 2023
1 parent 9f3fd5d commit f944e98
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 18 deletions.
3 changes: 1 addition & 2 deletions packages/next/src/build/index.ts
Expand Up @@ -2989,8 +2989,7 @@ export default async function build(
}

// remove temporary export folder
await recursiveDelete(exportOptions.outdir)
await fs.rmdir(exportOptions.outdir)
await fs.rm(exportOptions.outdir, { recursive: true, force: true })
await fs.writeFile(
manifestPath,
JSON.stringify(pagesManifest, null, 2),
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/build/utils.ts
Expand Up @@ -53,7 +53,6 @@ import {
} from '../server/load-components'
import { trace } from '../trace'
import { setHttpClientAndAgentOptions } from '../server/setup-http-agent-env'
import { recursiveDelete } from '../lib/recursive-delete'
import { Sema } from 'next/dist/compiled/async-sema'
import { denormalizePagePath } from '../shared/lib/page-path/denormalize-page-path'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
Expand Down Expand Up @@ -1809,7 +1808,7 @@ export async function copyTracedFiles(
moduleType = packageJson.type === 'module'
} catch {}
const copiedFiles = new Set()
await recursiveDelete(outputPath)
await fs.rm(outputPath, { recursive: true, force: true })

async function handleTraceFiles(traceFilePath: string) {
const traceData = JSON.parse(await fs.readFile(traceFilePath, 'utf8')) as {
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/export/index.ts
Expand Up @@ -17,7 +17,6 @@ import * as Log from '../build/output/log'
import createSpinner from '../build/spinner'
import { SSG_FALLBACK_EXPORT_ERROR } from '../lib/constants'
import { recursiveCopy } from '../lib/recursive-copy'
import { recursiveDelete } from '../lib/recursive-delete'
import {
BUILD_ID_FILE,
CLIENT_PUBLIC_FILES_PATH,
Expand Down Expand Up @@ -354,7 +353,7 @@ export default async function exportApp(
)
}

await recursiveDelete(join(outDir))
await promises.rm(outDir, { recursive: true, force: true })
await promises.mkdir(join(outDir, '_next', buildId), { recursive: true })

writeFileSync(
Expand Down
4 changes: 1 addition & 3 deletions packages/next/src/lib/verify-partytown-setup.ts
Expand Up @@ -8,7 +8,6 @@ import {
} from './has-necessary-dependencies'
import { fileExists, FileType } from './file-exists'
import { FatalError } from './fatal-error'
import { recursiveDelete } from './recursive-delete'
import * as Log from '../build/output/log'
import { getPkgManager } from './helpers/get-pkg-manager'

Expand Down Expand Up @@ -50,8 +49,7 @@ async function copyPartytownStaticFiles(
)

if (hasPartytownLibDir) {
await recursiveDelete(partytownLibDir)
await promises.rmdir(partytownLibDir)
await promises.rm(partytownLibDir, { recursive: true, force: true })
}

const { copyLibFiles } = await Promise.resolve(
Expand Down
11 changes: 2 additions & 9 deletions packages/next/src/server/image-optimizer.ts
Expand Up @@ -99,15 +99,8 @@ async function writeToCacheDir(
) {
const filename = join(dir, `${maxAge}.${expireAt}.${etag}.${extension}`)

// Added in: v14.14.0 https://nodejs.org/api/fs.html#fspromisesrmpath-options
// attempt cleaning up existing stale cache
if ((promises as any).rm) {
await (promises as any)
.rm(dir, { force: true, recursive: true })
.catch(() => {})
} else {
await promises.rmdir(dir, { recursive: true }).catch(() => {})
}
await promises.rm(dir, { recursive: true, force: true }).catch(() => {})

await promises.mkdir(dir, { recursive: true })
await promises.writeFile(filename, buffer)
}
Expand Down

0 comments on commit f944e98

Please sign in to comment.