333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**
 * Module dependencies.
 */
 
var fs = require('fs');
var path = require('path');
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var rl = require("readline").createInterface(process.stdin, process.stdout);
var ndir = require('../');
 
 
if (process.argv.length < 4) {
  console.log('Usage: node copy-dir.js <fromdir> <todir>');
  process.exit(1);
}
 
function CopyDir(fromdir, todir) {
  this.tasks = [];
  this.walkEnd = false;
  this.copyfileCount = 0;
};
util.inherits(CopyDir, EventEmitter);
 
CopyDir.prototype.start = function() {
  var self = this;
  self.emit('start');
  var walker = ndir.walk(fromdir);
  walker.on('dir', function(dirpath, files) {
    var doNext = self.tasks.length === 0;
    self.tasks.push([dirpath, true]);
    for (var i = 0, l = files.length; i < l; i++) {
      var info = files[i];
      self.tasks.push([info[0], info[1].isDirectory()]);
    }
    if (doNext) {
      process.nextTick(function() {
        self.next();
      });
    }
  });
  walker.on('end', function() {
    self.walkEnd = true;
  });
};
 
CopyDir.prototype.next = function() {
  var task = this.tasks.shift();
  if (!task) {
    if (this.walkEnd) {
      this.emit('end');
    }
    return;
  }
  var self = this;
  var f = task[0];
  var t = f.replace(fromdir, '');
  if (t[0] === '/') {
    t = t.substring(1);
  }
  t = path.join(todir, t);
  var isdir = task[1];
  if (isdir) {
    ndir.mkdir(t, function(err) {
      self.next();
    });
    return;
  }
  self.copyfile(f, t, function() {
    self.next();
  });
};
 
 
CopyDir.prototype._copyfile = function _copyfile(fromfile, tofile, callback) {
  var self = this;
  self.emit('startCopyfile', fromfile, tofile);
  ndir.copyfile(fromfile, tofile, function(err) {
    self.emit('endCopyfile', err, fromfile, tofile);
    if (!err) {
      self.copyfileCount++;
    }
    callback(err);
  });
}
 
CopyDir.prototype.copyfile = function copyfile(fromfile, tofile, callback) {
  var needCopy = true;
  var self = this;
  path.exists(tofile, function(exists) {
    if (exists) {
      self.emit('fileExists', tofile, function(confirm) {
        if (confirm) {
          return self._copyfile(fromfile, tofile, callback);
        }
        callback();
      })
      return;
    }
    self._copyfile(fromfile, tofile, callback);
  });
};
 
var fromdir = path.resolve(process.argv[2]);
var todir = path.resolve(process.argv[3]);
 
var copyworker = new CopyDir(fromdir, todir);
copyworker.on('start', function() {
  console.log('Start copy %s to %s', fromdir, todir);
});
copyworker.on('fileExists', function(tofile, confirmCallback) {
  rl.question('File "' + tofile + '" exists, overwrite? > ', function (answer) {
    confirmCallback(answer === 'yes' || answer === 'y');
  });
});
copyworker.on('startCopyfile', function(fromfile, tofile) {
  util.print(util.format('Copying "%s" to "%s" ... ', fromfile, tofile));
});
copyworker.on('endCopyfile', function(err, fromfile, tofile) {
  util.print((err ? 'Error!!!' : 'done.') + '\n');
});
 
function exit() {
  console.log('\nTotal copy %d files.', copyworker.copyfileCount);
  process.exit(0);
};
copyworker.on('end', exit);
rl.on('close', exit);
 
copyworker.start();