From 27dcd26c4376f07f9d07ccad1e2b6dbe86ccddc3 Mon Sep 17 00:00:00 2001 From: Sukka Date: Wed, 15 Nov 2023 21:34:18 +0800 Subject: [PATCH] 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 --- scripts/install-native.mjs | 10 +++++++--- scripts/publish-native.js | 5 ++--- scripts/setup-wasm.mjs | 7 +++---- scripts/trace-next-server.js | 5 +++-- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/scripts/install-native.mjs b/scripts/install-native.mjs index 9ea02664f1..28c881b681 100644 --- a/scripts/install-native.mjs +++ b/scripts/install-native.mjs @@ -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 }) diff --git a/scripts/publish-native.js b/scripts/publish-native.js index 6bd9e7b2dc..950117e26c 100755 --- a/scripts/publish-native.js +++ b/scripts/publish-native.js @@ -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) ) diff --git a/scripts/setup-wasm.mjs b/scripts/setup-wasm.mjs index 5ecb411319..53094822f1 100644 --- a/scripts/setup-wasm.mjs +++ b/scripts/setup-wasm.mjs @@ -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) diff --git a/scripts/trace-next-server.js b/scripts/trace-next-server.js index a1fd6d2c9b..422cec07cb 100644 --- a/scripts/trace-next-server.js +++ b/scripts/trace-next-server.js @@ -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)