Home | 簡體中文 | 繁體中文 | 雜文 | 打賞(Donations) | ITEYE 博客 | OSChina 博客 | Facebook | Linkedin | 知乎專欄 | Search | About

4.18. copy-dir

install

		
npm install copy-dir		
		
		

usage

		
Sync:

var copydir = require('copy-dir');
 
copydir.sync('/my/from/path', '/my/target/path');
Async:

var copydir = require('copy-dir');
 
copydir('/my/from/path', '/my/target/path', function(err){
  if(err){
    console.log(err);
  } else {
    console.log('ok');
  }
});
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:

var path = require('path');
var copydir = require('copy-dir');
 
copydir.sync('/my/from/path', '/my/target/path', function(stat, filepath, filename){
  if(stat === 'file' && path.extname(filepath) === '.html') {
    return false;
  }
  if (stat === 'directory' && filename === '.svn') {
    return false;
  }
  return true;
}, function(err){
  console.log('ok');
});
Async:

var path = require('path');
var copydir = require('copy-dir');
 
copydir('/a/b/c', '/a/b/e', function(stat, filepath, filename){
  //... 
}, function(err) {
  //... 
});