Skip to content
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

fs.copy is different from 'cp -r' #537

Closed
ChenCody opened this issue Jan 11, 2018 · 3 comments
Closed

fs.copy is different from 'cp -r' #537

ChenCody opened this issue Jan 11, 2018 · 3 comments

Comments

@ChenCody
Copy link

  • Operating System: Mac OS 10.13.2
  • Node.js version: 8.2.1
  • fs-extra version: 5.0.0

The document in 'docs/copy.md' says

'Copy a file or directory. The directory can have contents. Like cp -r.'
but in fact it is not.

Try this:
There is two folder called 'A' and 'B' like:

A--
 |-a1
 |-a2
B--
 |-b1
 |-b2

If I try cp -r ./a ./b, the folder 'B' will be like this:

B--
 |-b1
 |-b2
 |-A
   |-a1
   |-a2

And if i try fs.copy('./a', './b'), B will be like:

B--
 |-b1
 |-b2
 |-a1
 |-a2

These two command get different result. So i think the description

'Copy a file or directory. The directory can have contents. Like cp -r.'

in the document 'copy.md' is misleading.

I wish you could modify this document, and add some option that could make fs.copy achieve the same effect as 'cp -r'.

PS: Sorry for my poor English. : )

@RyanZim
Copy link
Collaborator

RyanZim commented Jan 11, 2018

Dupe of #323; PR welcome to clarify docs.

@RyanZim RyanZim closed this as completed Jan 11, 2018
wparad added a commit to Authress-Engineering/aws-architect.js that referenced this issue Jan 15, 2018
@ahuggins-nhs
Copy link

Recursive function which handily and easily solves this problem. No error handling, use at your risk as a starting point. Hope this helps others who come across this library, as it is fantastic and this is a really small sticking point.

const copyRecursiveSync = function copyRecursiveSync (src, dest) {
    fs.copySync(src, dest);

    fs.readdirSync(src)
        .map((name) => name)
        .filter((dir) => fs.lstatSync(path.join(src, dir)).isDirectory())
        .forEach((dir) => {
            copyRecursiveSync(path.join(src, dir), path.join(dest, dir));
            console.log(path.join(src, dir), path.join(dest, dir));
        });
};

@PawelWesolowski
Copy link

another workaround

const fs = require('fs-extra');
const path = require('path');

const src = './from/here';
const dst = './to'; // will be copied to ./to/here

let newPath = path.join(dst, path.basename(src));
fs.emptyDirSync(newPath);
fs.copySync(src,newPath);

Repository owner locked as resolved and limited conversation to collaborators Sep 1, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants