copy-dir
Easy used 'copy-dir' lib, even use a filter, copy a file or directory to another path, when target path or parent target path not exists, it will create the directory automatically.
install
npm install copy-dir
grammar
Sync Mode:
copydir.sync(from, to[, options]);
Async Mode:
copydir(from, to, [options, ]callback);
[options]:
utimes: false,
mode: false,
cover: true,
filter: true,
filter is a function that you want to filter the path, then return true or false.
It can use three arguments named state, filepath, filename
- state: String, 'file' / 'directory' / 'symbolicLink', marked as the file or path type
- filepath: String, the file path
- filename: String, the file name
usage
Sync Mode:
var copydir = require('copy-dir');
copydir.sync('/my/from/path', '/my/target/path', {
utimes: true,
mode: true,
cover: true
});
Async Mode:
var copydir = require('copy-dir');
copydir('/my/from/path', '/my/target/path', {
utimes: true,
mode: true,
cover: true
}, function(err){
if(err) throw err;
console.log('done');
});
add a filter
When you want to copy a directory, but some file or sub directory is not you want, you can do like this:
Sync Mode:
var path = require('path');
var copydir = require('copy-dir');
copydir.sync('/my/from/path', '/my/target/path', {
filter: function(stat, filepath, filename){
if(stat === 'file' && path.extname(filepath) === '.html') {
return false;
}
if (stat === 'directory' && filename === '.svn') {
return false;
}
if (stat === 'symbolicLink') {
return false;
}
return true;
}
});
console.log('done');
Async Mode:
var path = require('path');
var copydir = require('copy-dir');
copydir('/a/b/c', '/a/b/e', {
filter: function(stat, filepath, filename) {
return true;
}
}, function(err) {
});
Update Logs
1.3.0
Bug fix: filter function arguments incorrect, delete the third argument: dirname
Questions?
If you have any questions, please feel free to ask through New Issue.
License
copy-dir is available under the terms of the MIT License.