question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support for `dest` being a directory when using `copy*()`?

See original GitHub issue

When using copySync() (and, I assume, copy() too): if src is a file path and dest is a directory path, I would expect that the source file would be copied into the destination directory, with the same name that it had before (i.e. the last part at the end of the src path string).

However, it seems that, instead, copy() attempts to replace the dest directory with the src file.

I have tried {clobber: false} but this simply seems to stop the copy operation from taking place [as per your explanation in #321 I now understand why].

To get it to behave as desired, I have to include the destination filename at the end of the dest path. This means duplicating the destination filename. Sometimes, my code that calls copy() does not know the filename (only the full src path), so it is necessary to make an extra call, to path.basename(), first.

I assume that this behaviour is by design to fit in with how (standard) fs works? However I wonder if you could add a further copy()-like function (or option on the fs-extra version of copy() to support this behaviour?

Thanks for this very helpful module (only just started using it, but it’s already saved me lots of time) 😃.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

6reactions
jprichardsoncommented, Dec 3, 2016

When using copySync() (and, I assume, copy() too): if src is a file path and dest is a directory path, I would expect that the source file would be copied into the destination directory, with the same name that it had before (i.e. the last part at the end of the src path string).

I understand the perspective here. A lot of people have requested similar functionality. It’s my belief that we as programmers should be explicit where possible. As reading code is harder than writing code. fs-extra has chosen to encourage this by making programmer intent obvious to those who read the code.

I think where this functionality makes sense is in the case of shell programs where terse expressiveness is valued given the longevity of shell scripts.

Hope this makes sense.

Thanks for this very helpful module (only just started using it, but it’s already saved me lots of time) 😃.

You’re welcome. @RyanZim and I have a lot of great things planned for this module! 😃

1reaction
just-boriscommented, Dec 28, 2017

@RyanZim well this is another option that is preferable sometimes. But my real example is slightly more complicated than I have shared before

copyFileSync(`${srcRoot}/file-one.js`, `${destRoot}/app-one`)
copyFileSync(`${srcRoot}/file-two.js`, `${destRoot}/app-two`)
copyFileSync(`${srcRoot}/file-three.js`, `${destRoot}/app-two`)

There I would go with three copyFileSync calls instad of creating {src, dest} object pairs and iterating over them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Linux: copy and create destination dir if it does not exist
I guess this is the solution assuming you want the destination parent directories to be the same as the origin parent directories, which...
Read more >
xcopy | Microsoft Learn
Displays source and destination file names while copying. /l, Generates a list of files that are to be copied, but does not actively...
Read more >
The Ultimate Guide to Copying Files and Folders Using Xcopy
With Xcopy, you can copy files and create the destination directory on the fly. To do so, you need to append the /I...
Read more >
Python Copy Files and Directories [10 Ways] - PYnative
The shutil.copyfileobj() method · fsrc : The source file location (path) whose contents need to be copied. · fdst : The destination file...
Read more >
Copying a File or Directory - The Java™ Tutorials
COPY_ATTRIBUTES – Copies the file attributes associated with the file to the target file. The exact file attributes supported are file system and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found