copy.js 584 B

123456789101112131415161718192021222324252627
  1. var co = require('co');
  2. var fs = require('..');
  3. copy('.', 'examples/dest', function(err){
  4. if (err) throw err;
  5. console.log('done');
  6. });
  7. function copy(src, dst, fn) {
  8. co(function *(){
  9. var files = yield fs.readdir(src);
  10. yield fs.mkdir(dst);
  11. for (var i = 0; i < files.length; i++) {
  12. var file = files[i];
  13. var stat = yield fs.stat(file);
  14. if (!stat.isFile()) continue;
  15. var buf = yield fs.readFile(file);
  16. console.log('copy %s -> %s', src + '/' + file, dst + '/' + file);
  17. yield fs.writeFile(dst + '/' + file, buf);
  18. }
  19. })(fn);
  20. }