davglass / cpr Goto Github PK
View Code? Open in Web Editor NEWNodejs cp -R
License: Other
Nodejs cp -R
License: Other
Not sure what the defaults are for the options even after skimming the code. Could you please list the defaults?
I was having a difficult time filtering out files with node ncp
from npm, because negating a regex in JS is generally hard, but with cpr
filtering excludes files instead of including them, which is a nice default.
Funny logo. :)
to exclude junk files (such as .DS_Store). This could be made available to the CLI and would reduce project dependencies.
cpr('./mockData/', './results/', {
confirm: true
}, function (err, files) {
if (err) {
return console.error('can not do it', err);
}
files.forEach(function (file) {
console.log('copied: ', file);
});
});
//No output, files is an empty array
How to reproduce:
mkdir -p src/bar # create nested folders
touch src/bar/test.txt # create file
chmod -w -R src # revoke write permissions
src
folder with cpr:const cpr = require('cpr')
cpr('src', 'dest', (err, res) => { if (err) throw err; console.log(res) })
It will produce this error:
Uncaught [Error: Unable to copy directory entirely] {
list: [
[Error: EACCES: permission denied, mkdir '/path/to/dest/bar'] {
errno: -13,
code: 'EACCES',
syscall: 'mkdir',
path: '/path/to/dest/bar'
},
[Error: ENOENT: no such file or directory, open 'dest/bar/test.txt'] {
errno: -2,
code: 'ENOENT',
syscall: 'open',
path: 'dest/bar/test.txt'
}
]
}
I expect behaviour as original cp
command has:
cp -R src dest # success
ls -l dest
total 0
dr-xr-xr-x 3 isqua isqua 102 Oct 30 22:53 ./
drwxr-xr-x 19 isqua isqua 646 Oct 30 22:53 ../
dr-xr-xr-x 3 isqua isqua 102 Oct 30 22:53 bar/
ll dest/bar
total 0
dr-xr-xr-x 3 isqua isqua 102 Oct 30 22:53 ./
dr-xr-xr-x 3 isqua isqua 102 Oct 30 22:53 ../
-r--r--r-- 1 isqua isqua 0 Oct 30 22:53 test.txt
NodeJS version: v12.18.1
cpr version: 3.0.1, 2.2.0
Hi,
this issue appeared after update from 5.0.12 to 5.0.13 (as we actually trusted in semantic versioning). We are relying on that configuration option to set a different color for plotOptions/column/states/hover
to show a semi transparent color on top of the actual gradient column fill color.
Since v5.0.13 we experienced a black fill color with a style value set to rgb(NaN, NaN, NaN)
in DOM explorer when hovering.
I prepared a simplified demo here: https://jsfiddle.net/zt4xm146/ If you enable the showBug
option in the 1st line you can simulate the issue by hovering the column bars.
The problem is - as far as we figured out: when using rgb()
or rgba()
string values for this option it results in that black filling.
Furthermore, try to replace the <script>
tag in the HTML section to use 5.0.12 version of highcharts:
<script src="https://code.highcharts.com/5.0.12/highcharts.js"></script>
Now the bug toggle variable shows no effect in terms of black filling on hover.
I think it may be related to this new "Added color animation on state change for non-marker based series like columns and tree maps" feature I saw in the changelog for v5.0.13.
I wonder why such features show up in a patch version? ;-)
Files starting with dot (.) are not copied
Tested in linux
preserve links and attributes
Not particularly useful to me, at least right now, but it might be a good goal to reach.
I am unable to find a change log for this project anywhere. It would be good to know what changed between all those releases and the major ones with potentially breaking changes in particular.
Hey,
cpr
removes dirs if they already exist, if overwrite:true
is specified in options (which I assumed was just the same as -f
).
So for a test.js
which contains this:
var cpr = require('cpr');
cpr('build/', 'deployments/', {
deleteFirst: false,
overwrite: true,
confirm: false
}, function(err, files) {
if(err) {
console.error('ERROR', err);
return;
}
console.log('SUCCESS');
});
I see this behavior:
% find build
build
build/assets
build/assets/2
% find deployments
deployments
deployments/assets
deployments/assets/1
% node test.js
SUCCESS
% find deployments/
deployments/
deployments/assets
deployments/assets/2
whereas cp -R -f
does the following:
% find build
build
build/assets
build/assets/2
% find deployments
deployments
deployments/assets
deployments/assets/1
% cp -R build/* deployments
% find deployments
deployments
deployments/assets
deployments/assets/1
deployments/assets/2
and without overwrite: true
it fails on the already existing assets/
dir (and possibly other already existing files). So right now there seems to be no way to merge to directory trees with cpr
. ... or am I overlooking anything?
AFAICT this is the code causing this behavior: https://github.com/davglass/cpr/blob/master/lib/index.js#L113
Why was that code added in the first place?
BR,
Domme
It would be great if cpr included a CLI. This would make it super easy to write scripts that work across Linux, Mac, and Windows. Something like this:
cpr [source_dir] [destination_dir]
And if you want to be really awesome, you could support multiple source files and/or globs:
cpr [source_file1] [source_file2] [...source_fileN] [destination_dir]
cpr [source_glob] [destination_dir]
Hi,
this patch looks enough to update mkdirp dependency to ^1.0.3:
--- a/lib/index.js
+++ b/lib/index.js
@@ -121,12 +121,12 @@
err.errno = 27;
options.errors.push(err);
} else {
- mkdirp(to, stat.mode, stack.add(function(err) {
+ mkdirp(to, stat.mode).catch((err) => {stack.add(function(err) {
/*istanbul ignore next*/
if (err) {
options.errors.push(err);
}
- }));
+ })});
}
}));
}
@@ -139,7 +139,7 @@
var copyFile = function(from, to, options, callback) {
var dir = path.dirname(to);
- mkdirp(dir, function() {
+ mkdirp(dir).then( () => {
fs.stat(to, function(statError) {
var err;
if(!statError && options.overwrite !== true) {
Does it skip over that particular file and keep going or does it stop where it was and return the error?
Hi,
I had assumed that this module would behave like the command it takes its name from, but it appears it does not do so when copying single files.
Say you have this file structure:
foo/
`-- bar.jpg
baz/
and you want to copy foo/bar.jpg
to baz/qux.jpg
.
When you execute cp -R foo/bar.jpg baz/qux.jpg
in command line, it creates a renamed copy to the target path. You get:
foo/
`-- bar.jpg
baz/
`-- qux.jpg
When using this project, on the other hand, you get:
foo/
`-- bar.jpg
baz/
`-- qux.jpg/
`-- bar.jpg
I was relying on this behavior to have a generic purpose file copy module.
cpr("/path/to/dir/", "/dest/")
//-> copies contents of /path/to/dir/ into /dest/
cpr("/path/to/dir/**/*", "/dest/")
//-> same as above
cpr("/path/**/dir/", "/dest/")
//-> same, and possibly more if there're other nested dirs named "dir"
When/if glob
expands directories, we can use glob.Glob
to copy each file as it is expanded from the glob. This is not only better because it helps avoid race conditions by copying the file immediately after it's expanded, but it may also have better performance because the hard drive has already sought to that track/sector.
Since a glob will likely resolve to many files, we might as well implement #41 along with this. An instance of glob.Glob
per glob and file paths memoized to avoid redundancy.
cpr(["/path/to/dir/", "/path/to/dir/**/*", "/path/**/dir/"], "/dest/")
//-> same as above, with no file copied more than once
This doesn't seem to support the *NIX cp
commands base functionality cp foo bar baz qux
which would copy all files into qux
to avoid this.
Trying to filter the list of items to not include anything that is hidden ex: ._helloFile or ._helloWorld.jpg
Seems to not be working correctly AND the files array that is passed to the callback still returns everything in the source folder rather than my filtered results. What am I doing wrong?
cpr(source, destination, {
//deleteFirst: true, //Delete "to" before
overwrite: true, //If the file exists, overwrite it
confirm: true, //After the copy, stat all the copied files to make sure they are there
filter:/^(^[_].*)*$/gmi
}, function(err, files) {
if(err){
console.log(err)
_self.logs = err;
}else{
console.log(files)
}
});
It is possible to pass the API options as arguments to the CLI interface? E.g. cpr --overwrite foo bar
From what I read from the source code, the errors
can actually be a string or an array of strings, which I found very odd from a consumer perspective.
I would suggest to normalize this to always be an array of Error
instances. Unlike strings, error instances gives more information about the actual error. For instance, we can get a stack trace from it if necessary.
If you guys agree with this, I will make a PR with these changes. This is an API change which translates in a major release bump.
Seems this module doesn't handle empty directories very well. Given a file tree
parent
child-dir
child.file
If you cpr
the parent somewhere, there's an inevitable stat
error on the destination child-dir
path. Similary, just running cpr
on an empty directory also fails. I'm working around this now with
fs.readdir(src, function (err, files) {
if (files.length) {
cpr(src, dst, function (errs) {
if (!errs) return;
errs.forEach(function (err) {
if (err.code === "ENOENT" && err.syscall === "stat") {
mkdirp(err.path);
}
});
});
} else {
mkdirp(dst);
}
});
but it'd be nice to see this fixed :)
To suppress errors if the source did not exist (ENOENT
). Errors should still be thrown if the user's permissions were insufficient.
You can observe this by adding the following test to tests/full.js
:
"should fail without write permissions": {
topic: function() {
var self = this;
var mkdirp = require('mkdirp');
var baddir = path.join(to, 'readonly');
mkdirp.sync(baddir);
fs.chmodSync(baddir, '555');
cpr(from, baddir, function(err,status) {
self.callback(null, {
err: err,
status: status
});
});
},
"should return an error in the callback": function(topic) {
assert.isUndefined(topic.status);
assert(topic.err instanceof Error);
//assert.equal('From should be a file or directory', topic.err.message);
}
},
npm test
outputs:
✗ Errored » callback not fired
in should fail without write permissions
in CPR Tests
in tests/full.js
✗ Errored » 38 honored ∙ 1 errored ∙ 1 dropped
npm ERR! Test failed. See above for more details.
cpr("source", "destination", function(error) {
console.log(error.code);
//-> undefined
});
I was expecting something like "EEXIST".
Using cpr v1.0.0, I get the error Unable to copy directory entirely
when i try to use CPR to copy directories in a linux centos environment. Things were working fine when I was using 0.4.2, so I'm just going to switch back to that.
Is there an option to preserve timestamps when copying? Like fs-extra's copy. If not it'd be really nice to have.
instead of borking when provided with a non-directory, just copy a file like cp
does
Add native promise support.
Hey, I'm curious if interest exists in introducing some throttling on IO, this would be to prevent overwhelming the system when copying large trees.
Or maybe a recommendation for preventing this. I realize the synchronize variant (not yet merged) would likely not have this issue.
…with AppVeyor. If you set up the [free] account, I can upload the config file.
Would be nice to have an option to pass the filter with the CLI
If I'm copying dirA to parent, then dirA/test.png doesn't seem to overwrite parent/dirA/test.png with the overwrite flag set to true. Is this not supported?
Thanks!
is it bsd or ... ?
Given a file named "changed.txt" with the contents:
changed-v1
And with the following code added to the copyFile
function:
fromFile.on('error', onError);
toFile.on('error', onError);
fromFile.once('end', function() {
cb(err);
if (to === 'changed.txt') {
console.error(to);
console.error(fs.readFileSync(from, 'utf8'));
console.error(fs.readFileSync(to, 'utf8'));
}
});
fromFile.pipe(toFile);
About 50% of the time I get in the console:
changed.txt
changed-v1
changed-v1
And the other 50% I get:
changed.txt
changed-v1
It seems like there may be a race condition in the piping and "end" event. I should also note that there are other files in the operation, and this is the only one affected. For instance using fixturify:
{ 'changed.txt': '',
'missing-changed.txt': 'changed-v1\n',
'missing-unchanged.txt': 'unchanged\n',
'removed-changed.txt': 'changed-v1\n',
'removed-unchanged.txt': 'unchanged\n',
'unchanged.txt': 'unchanged\n' }
Hello!
Thank you for taking your time to implement this useful module.
However, does it support symlink resolving as ncp does? I can't find it in the documentation.
Thank you!
Hello!
I have a project with extremely big directories. It would be a tremendous improvement if cpr would be able to compare files from source and target directory using timestamp and/or filesize.
Thanks!
Greetings! I have the following code that is trying to ignore CVS directories, but the last 5 variations of regex I tried do not appear to work as expected, but I've tested the regex online and in other code, so the format should be ok. Any tips on what I might be doing wrong are appreciated.
` code snippet
cpr('./public', 'build/public/', {
deleteFirst: true,
overwrite: true,
confirm: true,
filter: '^((?!CVS).)*$'
}, function (err) {
if (err) {
console.log(colors.red.bold('Something went wrong during copy: '), err.stack);
return console.error(err);
}
console.log(colors.blue.bold('...copied ./public to build directory!'));
});
`
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.