Skip to content

Commit 728c8ee

Browse files
fix: revert "refactor: use fs.cpSync (#21019)" (#21081)
Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com>
1 parent d43dbd7 commit 728c8ee

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

packages/create-vite/src/index.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -644,11 +644,7 @@ async function init() {
644644
)
645645
fs.writeFileSync(targetPath, updatedContent)
646646
} else {
647-
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- it is not experimental in Node 22.3+
648-
fs.cpSync(path.join(templateDir, file), targetPath, {
649-
recursive: true,
650-
mode: fs.constants.COPYFILE_FICLONE,
651-
})
647+
copy(path.join(templateDir, file), targetPath)
652648
}
653649
}
654650

@@ -714,6 +710,15 @@ function formatTargetDir(targetDir: string) {
714710
return targetDir.trim().replace(/\/+$/g, '')
715711
}
716712

713+
function copy(src: string, dest: string) {
714+
const stat = fs.statSync(src)
715+
if (stat.isDirectory()) {
716+
copyDir(src, dest)
717+
} else {
718+
fs.copyFileSync(src, dest)
719+
}
720+
}
721+
717722
function isValidPackageName(projectName: string) {
718723
return /^(?:@[a-z\d\-*~][a-z\d\-*._~]*\/)?[a-z\d\-~][a-z\d\-._~]*$/.test(
719724
projectName,
@@ -729,6 +734,15 @@ function toValidPackageName(projectName: string) {
729734
.replace(/[^a-z\d\-~]+/g, '-')
730735
}
731736

737+
function copyDir(srcDir: string, destDir: string) {
738+
fs.mkdirSync(destDir, { recursive: true })
739+
for (const file of fs.readdirSync(srcDir)) {
740+
const srcFile = path.resolve(srcDir, file)
741+
const destFile = path.resolve(destDir, file)
742+
copy(srcFile, destFile)
743+
}
744+
}
745+
732746
function isEmpty(path: string) {
733747
const files = fs.readdirSync(path)
734748
return files.length === 0 || (files.length === 1 && files[0] === '.git')

packages/vite/src/node/plugins/prepareOutDir.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import colors from 'picocolors'
44
import type { Plugin } from '../plugin'
55
import { getResolvedOutDirs, resolveEmptyOutDir } from '../watch'
66
import type { Environment } from '../environment'
7-
import { emptyDir, normalizePath } from '../utils'
7+
import { copyDir, emptyDir, normalizePath } from '../utils'
88
import { withTrailingSlash } from '../../shared/utils'
99

1010
export function prepareOutDirPlugin(): Plugin {
@@ -86,11 +86,7 @@ function prepareOutDir(
8686
),
8787
)
8888
}
89-
// eslint-disable-next-line n/no-unsupported-features/node-builtins -- it is not experimental in Node 22.3+
90-
fs.cpSync(publicDir, outDir, {
91-
recursive: true,
92-
mode: fs.constants.COPYFILE_FICLONE,
93-
})
89+
copyDir(publicDir, outDir)
9490
}
9591
}
9692
}

packages/vite/src/node/utils.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,25 @@ export function emptyDir(dir: string, skip?: string[]): void {
624624
}
625625
}
626626

627+
// NOTE: we cannot use `fs.cpSync` because of a bug in Node.js (https://github.com/nodejs/node/issues/58768, https://github.com/nodejs/node/issues/59168)
628+
// also note that we should set `dereference: true` when we use `fs.cpSync`
629+
export function copyDir(srcDir: string, destDir: string): void {
630+
fs.mkdirSync(destDir, { recursive: true })
631+
for (const file of fs.readdirSync(srcDir)) {
632+
const srcFile = path.resolve(srcDir, file)
633+
if (srcFile === destDir) {
634+
continue
635+
}
636+
const destFile = path.resolve(destDir, file)
637+
const stat = fs.statSync(srcFile)
638+
if (stat.isDirectory()) {
639+
copyDir(srcFile, destFile)
640+
} else {
641+
fs.copyFileSync(srcFile, destFile)
642+
}
643+
}
644+
}
645+
627646
export const ERR_SYMLINK_IN_RECURSIVE_READDIR =
628647
'ERR_SYMLINK_IN_RECURSIVE_READDIR'
629648
export async function recursiveReaddir(dir: string): Promise<string[]> {

0 commit comments

Comments
 (0)