@@ -76,14 +76,6 @@ const expandPatternsWithBraceExpansion = patterns => patterns.flatMap(pattern =>
7676*/
7777const isSelfCopy = ( from , to ) => path . resolve ( to ) === path . resolve ( from ) ;
7878
79- const ensureNotSelfCopy = ( from , to ) => {
80- if ( isSelfCopy ( from , to ) ) {
81- throw new CpyError ( `Refusing to copy to itself: \`${ from } \`` ) ;
82- }
83-
84- return to ;
85- } ;
86-
8779const relativizeWithin = ( base , file ) => {
8880 const relativePath = path . relative ( base , file ) ;
8981 if ( relativePath . startsWith ( '..' ) || path . isAbsolute ( relativePath ) ) {
@@ -125,7 +117,7 @@ const computeToForGlob = ({entry, destination, options}) => {
125117
126118const computeToForNonGlob = ( { entry, destination, options} ) => {
127119 if ( path . isAbsolute ( destination ) ) {
128- return ensureNotSelfCopy ( entry . path , path . join ( destination , entry . name ) ) ;
120+ return path . join ( destination , entry . name ) ;
129121 }
130122
131123 const insideCwd = ! path . relative ( options . cwd , entry . path ) . startsWith ( '..' ) ;
@@ -145,10 +137,10 @@ const computeToForNonGlob = ({entry, destination, options}) => {
145137 }
146138
147139 if ( ! entry . pattern . isDirectory && ! insideCwd ) {
148- return ensureNotSelfCopy ( entry . path , path . join ( path . resolve ( options . cwd , destination ) , entry . name ) ) ;
140+ return path . join ( path . resolve ( options . cwd , destination ) , entry . name ) ;
149141 }
150142
151- return ensureNotSelfCopy ( entry . path , path . join ( options . cwd , destination , path . relative ( options . cwd , entry . path ) ) ) ;
143+ return path . join ( options . cwd , destination , path . relative ( options . cwd , entry . path ) ) ;
152144} ;
153145
154146const preprocessDestinationPath = ( { entry, destination, options} ) => (
@@ -334,14 +326,19 @@ export default function cpy(
334326 return pMap (
335327 entries ,
336328 async entry => {
337- const to = renameFile (
338- preprocessDestinationPath ( {
339- entry,
340- destination,
341- options,
342- } ) ,
343- options . rename ,
344- ) ;
329+ let to = preprocessDestinationPath ( {
330+ entry,
331+ destination,
332+ options,
333+ } ) ;
334+
335+ // Apply rename after computing the base destination path
336+ to = renameFile ( to , options . rename ) ;
337+
338+ // Check for self-copy after rename has been applied
339+ if ( isSelfCopy ( entry . path , to ) ) {
340+ throw new CpyError ( `Refusing to copy to itself: \`${ entry . path } \`` ) ;
341+ }
345342
346343 try {
347344 await copyFile ( entry . path , to , { ...options , onProgress : fileProgressHandler } ) ;
0 commit comments