jonschlinkert / copy

Copy files using glob patterns. Sync, async, promise or streams. (node.js utility)
MIT License
94 stars 125 forks source link

wrong behavior on windows when using glob "*" #23

Open oupala opened 7 years ago

oupala commented 7 years ago

I've just noticed a wrong behavior on windows when using glob "*".

C:\test
└───source
    └───path
        └───to
            └───a
                └───file
└───dest1
└───dest2

The main objective is to copy the file file and its tree to the destination directory (dest1 or dest2).

When using a full path to specify which files to copy, everything goes well.

λ node_modules\.bin\copy source/path/to/a/file dest1/
√ dest1\source\path\to\a\file

The result is that the file file is copied with is whole tree.

C:\test
└───source
    └───path
        └───to
            └───a
                └───file
└───dest1
    └───source
        └───path
            └───to
                └───a
                    └───file
└───dest2

When using a glob pattern *""** to specify multiple files to copy, things get worse.

node_modules\.bin\copy source/path/to/a/* dest2/
√ ..\..\..\..\dest2\file

The result is that the file file is copied but without its tree.

C:\test
└───source
    └───path
        └───to
            └───a
                └───file
└───dest1
    └───source
        └───path
            └───to
                └───a
                    └───file
└───dest2
    └───file

This bug does not happen on linux.

ghost commented 6 years ago

Any update ?

DJDaveMark commented 6 years ago

I think this bug comes from the fact that the flatten option is inconsistent if the glob matches a single file or multiple files. If you always specify the flatten option, you'll get the expected results:

No flatten option (commented out)

copy('path/to/a/file', 'dest', /*{flatten: false},*/ function (err, files) {
    // dest/path/to/a/file
});
copy('path/to/a/file*', 'dest', /*{flatten: true},*/ function (err, files) {
    // dest/file
});

Flatten option (inversed from above)

copy('path/to/a/file', 'dest', {flatten: true}, function (err, files) {
    // dest/file
});
copy('path/to/a/file*', 'dest', {flatten: false}, function (err, files) {
    // dest/path/to/a/file
});