Skip to content

Commit 27dcd26

Browse files
authored
chore: replace fs-extra usage in scripts/ (#57215)
The PR is the continuation of #56917 and #57030. The PR replaces `fs-extra#copy` with Node.js built-in `fs.cp` API (which is almost identical to `fs-extra#copy`, and has been available since Node.js 16). The PR also provides a workaround for flaky Windows `fs.rename` operation (#57030). cc @styfle @wbinnssmith
1 parent 7281cd3 commit 27dcd26

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

scripts/install-native.mjs

+7-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import os from 'os'
22
import path from 'path'
33
import execa from 'execa'
44
import fs from 'fs'
5-
import { move } from 'fs-extra'
5+
import fsp from 'fs/promises'
66
;(async function () {
77
if (process.env.NEXT_SKIP_NATIVE_POSTINSTALL) {
88
console.log(
@@ -70,8 +70,12 @@ import { move } from 'fs-extra'
7070
pkgs.map(async (pkg) => {
7171
const from = path.join(tmpdir, 'node_modules/@next', pkg)
7272
const to = path.join(cwd, 'node_modules/@next', pkg)
73-
// overwriting by removing the target first
74-
return move(from, to, { overwrite: true })
73+
// The directory from pnpm store is a symlink, which can not be overwritten,
74+
// so we remove the existing directory before copying
75+
await fsp.rm(to, { recursive: true, force: true })
76+
// Renaming is flaky on Windows, and the tmpdir is going to be deleted anyway,
77+
// so we use copy the directory instead
78+
return fsp.cp(from, to, { force: true, recursive: true })
7579
})
7680
)
7781
fs.rmSync(tmpdir, { recursive: true, force: true })

scripts/publish-native.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
const path = require('path')
44
const execa = require('execa')
5-
const { copy } = require('fs-extra')
65
const { Sema } = require('async-sema')
7-
const { readFile, readdir, writeFile } = require('fs/promises')
6+
const { readFile, readdir, writeFile, cp } = require('fs/promises')
87

98
const cwd = process.cwd()
109

@@ -28,7 +27,7 @@ const cwd = process.cwd()
2827

2928
try {
3029
let binaryName = `next-swc.${platform}.node`
31-
await copy(
30+
await cp(
3231
path.join(cwd, 'packages/next-swc/native', binaryName),
3332
path.join(nativePackagesDir, platform, binaryName)
3433
)

scripts/setup-wasm.mjs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import path from 'path'
22
import fs from 'fs'
3-
import { copy } from 'fs-extra'
43
;(async function () {
54
try {
65
let wasmDir = path.join(process.cwd(), 'packages/next-swc/crates/wasm')
@@ -22,10 +21,10 @@ import { copy } from 'fs-extra'
2221
JSON.stringify(wasmPkg, null, 2)
2322
)
2423

25-
await copy(
26-
path.join(wasmDir, `${folderName}`),
24+
fs.cpSync(
25+
path.join(wasmDir, folderName),
2726
path.join(process.cwd(), `node_modules/@next/swc-wasm-${wasmTarget}`),
28-
{ overwrite: true }
27+
{ force: true, recursive: true }
2928
)
3029
} catch (e) {
3130
console.error(e)

scripts/trace-next-server.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ const os = require('os')
22
const path = require('path')
33
const execa = require('execa')
44
const fsp = require('fs/promises')
5-
const { copy } = require('fs-extra')
65
const prettyBytes = require('pretty-bytes')
76
const gzipSize = require('next/dist/compiled/gzip-size')
87
const { nodeFileTrace } = require('next/dist/compiled/@vercel/nft')
@@ -25,14 +24,16 @@ async function main() {
2524
const origTestDir = path.join(origRepoDir, 'test')
2625
const dotDir = path.join(origRepoDir, './') + '.'
2726

28-
await copy(origRepoDir, repoDir, {
27+
await fsp.cp(origRepoDir, repoDir, {
2928
filter: (item) => {
3029
return (
3130
!item.startsWith(origTestDir) &&
3231
!item.startsWith(dotDir) &&
3332
!item.includes('node_modules')
3433
)
3534
},
35+
force: true,
36+
recursive: true,
3637
})
3738

3839
console.log('using workdir', workDir)

0 commit comments

Comments
 (0)