Skip to content

Commit

Permalink
fixed #11 - empty directory handling
Browse files Browse the repository at this point in the history
  • Loading branch information
davglass committed Feb 15, 2015
1 parent 76b0f84 commit 294c282
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 17 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ var getTree = function(from, options, callback) {
options.toHash = options.toHash || {};

fs.readdir(from, stack.add(function(err, dirs) {
if (!dirs.length) {
results[from] = true;
fs.stat(from, stack.add(function(err, stat) {
if (err) {
return errors.push(err);
}
options.stats[from] = stat;
options.toHash[from] = path.join(options.to, path.relative(options.from, from));
}));
}
dirs.forEach(function (dir) {
var base = path.join(from, dir);
fs.stat(base, stack.add(function(err, stat) {
Expand Down Expand Up @@ -71,9 +81,13 @@ var splitTree = function (tree, options, callback) {

tree.forEach(function(item) {
var to = options.toHash[item];
dirs[path.dirname(item)] = true;
options.stats[path.dirname(item)] = fs.statSync(path.dirname(item));
options.toHash[path.dirname(item)] = path.dirname(to);
if (options.stats[item] && options.stats[item].isDirectory()) {
dirs[item] = true;
} else {
dirs[path.dirname(item)] = true;
options.stats[path.dirname(item)] = fs.statSync(path.dirname(item));
options.toHash[path.dirname(item)] = path.dirname(to);
}
});

tree.forEach(function(item) {
Expand Down
11 changes: 11 additions & 0 deletions tests/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,17 @@ var tests = {
assert.equal('From should be a file or directory', topic.err.message);
}
},
"should copy empty directory": {
topic: function() {
var mkdirp = require('mkdirp');
mkdirp.sync(path.join(to, 'empty-src'));
cpr(path.join(to, 'empty-src'), path.join(to, 'empty-dest'), this.callback);
},
'has ./out/empty-dest': function(topic) {
var stat = fs.statSync(path.join(to, 'empty-dest'));
assert.ok(stat.isDirectory());
},
},
"should copy one file": {
topic: function() {
cpr(__filename, path.join(to, 'one-file-test/'), this.callback);
Expand Down

0 comments on commit 294c282

Please sign in to comment.