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
This commit is contained in:
Sukka 2023-11-15 21:34:18 +08:00 committed by GitHub
parent 7281cd3fc4
commit 27dcd26c43
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 15 additions and 12 deletions

View file

@ -2,7 +2,7 @@ import os from 'os'
import path from 'path'
import execa from 'execa'
import fs from 'fs'
import { move } from 'fs-extra'
import fsp from 'fs/promises'
;(async function () {
if (process.env.NEXT_SKIP_NATIVE_POSTINSTALL) {
console.log(
@ -70,8 +70,12 @@ import { move } from 'fs-extra'
pkgs.map(async (pkg) => {
const from = path.join(tmpdir, 'node_modules/@next', pkg)
const to = path.join(cwd, 'node_modules/@next', pkg)
// overwriting by removing the target first
return move(from, to, { overwrite: true })
// The directory from pnpm store is a symlink, which can not be overwritten,
// so we remove the existing directory before copying
await fsp.rm(to, { recursive: true, force: true })
// Renaming is flaky on Windows, and the tmpdir is going to be deleted anyway,
// so we use copy the directory instead
return fsp.cp(from, to, { force: true, recursive: true })
})
)
fs.rmSync(tmpdir, { recursive: true, force: true })

View file

@ -2,9 +2,8 @@
const path = require('path')
const execa = require('execa')
const { copy } = require('fs-extra')
const { Sema } = require('async-sema')
const { readFile, readdir, writeFile } = require('fs/promises')
const { readFile, readdir, writeFile, cp } = require('fs/promises')
const cwd = process.cwd()
@ -28,7 +27,7 @@ const cwd = process.cwd()
try {
let binaryName = `next-swc.${platform}.node`
await copy(
await cp(
path.join(cwd, 'packages/next-swc/native', binaryName),
path.join(nativePackagesDir, platform, binaryName)
)

View file

@ -1,6 +1,5 @@
import path from 'path'
import fs from 'fs'
import { copy } from 'fs-extra'
;(async function () {
try {
let wasmDir = path.join(process.cwd(), 'packages/next-swc/crates/wasm')
@ -22,10 +21,10 @@ import { copy } from 'fs-extra'
JSON.stringify(wasmPkg, null, 2)
)
await copy(
path.join(wasmDir, `${folderName}`),
fs.cpSync(
path.join(wasmDir, folderName),
path.join(process.cwd(), `node_modules/@next/swc-wasm-${wasmTarget}`),
{ overwrite: true }
{ force: true, recursive: true }
)
} catch (e) {
console.error(e)

View file

@ -2,7 +2,6 @@ const os = require('os')
const path = require('path')
const execa = require('execa')
const fsp = require('fs/promises')
const { copy } = require('fs-extra')
const prettyBytes = require('pretty-bytes')
const gzipSize = require('next/dist/compiled/gzip-size')
const { nodeFileTrace } = require('next/dist/compiled/@vercel/nft')
@ -25,7 +24,7 @@ async function main() {
const origTestDir = path.join(origRepoDir, 'test')
const dotDir = path.join(origRepoDir, './') + '.'
await copy(origRepoDir, repoDir, {
await fsp.cp(origRepoDir, repoDir, {
filter: (item) => {
return (
!item.startsWith(origTestDir) &&
@ -33,6 +32,8 @@ async function main() {
!item.includes('node_modules')
)
},
force: true,
recursive: true,
})
console.log('using workdir', workDir)