-
Notifications
You must be signed in to change notification settings - Fork 772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for dest
being a directory when using copy*()
?
#323
Comments
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. 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.
You're welcome. @RyanZim and I have a lot of great things planned for this module! :) |
@jprichardson BTW, we should document this fact clearly somewhere, since a lot of people seem to be asking about this. |
@jprichardson: thanks for the thoughtful explanation. I agree with @RyanZim that a note in the docs would help clarify how it works. Thanks again, both (and for the very quick replies). |
IMO, the devs should avoid copy-pasting parts as they cause typos and bugs. For example, I want to copy bunch of files copySync(`${srcRoot}/angular.js`, `${destRoot}/angular.js`)
copySync(`${srcRoot}/react.js`, `${destRoot}/react.js`)
copySync(`${srcRoot}/vue.js`, `${destRoot}/vue.js`) Repeating the same file name twice will not help anyone reading my code to understand it better. But there are more chances to make a typo, and then it will be very hard to track down, why file is named wrong at the other end. That's why I have created 2-line helper that appends file name for me: const path = require('path')
const { copySync } = require('fs-extra')
function copyFileSync(filePath, destDir) {
const filename = path.basename(filePath)
return copySync(filePath, path.join(destDir, filename))
} Works perfectly! Code is cleaner and there are less places where dev can make a spelling mistake. copyFileSync(`${srcRoot}/angular.js`, destRoot)
copyFileSync(`${srcRoot}/react.js`, destRoot)
copyFileSync(`${srcRoot}/vue.js`, destRoot) |
@just-boris you need to rethink how you're writing code. You have 3 lines that are almost the same. Consider something like this instead: ;['angular', 'react', 'vue']
.forEach(name => fs.copySync(`${srcRoot}/${name}.js`, `${destRoot}/${name}.js`)) |
@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 |
To add another case where it'd be nice to support this: I use a glob to find a bunch of files in different directories, and I'd like to copy them all to the same directory. So I've got something like: // Not really hardcoded, but returned from a glob query
const input = [
'absolute/path/to/some/file.img',
'absolute/path/to/some/other/file.img',
'absolute/path/to/somewhere/else.img'
]; What I'd like to do is: input.map(path => fs.copy(filePath, 'ouputDir')) But instead I've got to do: input.map(path => fs.copy(filePath, path.join('outputDir', path.basename(filePath)) It's not a showstopper, but I find the first hypothetical snippet a lot more readable. |
When using
copySync()
(and, I assume,copy()
too): ifsrc
is a file path anddest
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 thesrc
path string).However, it seems that, instead,
copy()
attempts to replace thedest
directory with thesrc
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 callscopy()
does not know the filename (only the fullsrc
path), so it is necessary to make an extra call, topath.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 furthercopy()
-like function (or option on the fs-extra version ofcopy()
to support this behaviour?Thanks for this very helpful module (only just started using it, but it’s already saved me lots of time) :-).
The text was updated successfully, but these errors were encountered: