Skip to content

Commit fe78d6f

Browse files
committed
checks isDirectory
- adds checks to ensure we aren't trying to read a directory as a file. - closes #24 - thanks @jnthnjns
1 parent 612b170 commit fe78d6f

File tree

4 files changed

+47
-8
lines changed

4 files changed

+47
-8
lines changed

index.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,18 @@ function copyEach(files, dir, options, cb) {
9797

9898
utils.each(files, function(filename, next) {
9999
var filepath = path.resolve(opts.cwd, filename);
100+
if (utils.isDirectory(filepath)) {
101+
next()
102+
return;
103+
}
100104
copyOne(filepath, dir, opts, next);
101-
}, cb);
105+
}, function(err, arr) {
106+
if (err) {
107+
cb(err);
108+
return;
109+
}
110+
cb(null, arr.filter(Boolean));
111+
});
102112
}
103113

104114
/**

lib/utils.js

+26
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,39 @@ require('resolve-dir', 'resolve');
2727
require('to-file');
2828
require = fn;
2929

30+
/**
31+
* Gets the file stats for a File object.
32+
*
33+
* @param {Object} `file` File object that has a `path` property.
34+
* @return {Object} `fs.stat` object if successful. Otherwise an empty object.
35+
*/
36+
3037
utils.stat = function(file) {
3138
try {
3239
return fs.lstatSync(file.path);
3340
} catch (err) {}
3441
return {};
3542
};
3643

44+
/**
45+
* Checks if the file is a directory using `fs.lstatSync`.
46+
*
47+
* @param {String|Object} `file` filepath as a string or a file object with a `path` property.
48+
* @return {Boolean} Returns `true` when the filepath is a directory.
49+
*/
50+
51+
utils.isDirectory = function(file) {
52+
if (typeof file === 'string') {
53+
file = {path: file}
54+
}
55+
56+
var stat = utils.stat(file);
57+
if (stat.isDirectory) {
58+
return stat.isDirectory();
59+
}
60+
return false;
61+
};
62+
3763
/**
3864
* Get the base filepath from a glob.
3965
*

test/copy.js

+9-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var assert = require('assert');
77
require('assert-path')(assert);
88
require('assert-fs')(assert);
99
var exists = support.exists;
10+
var eachExists = support.eachExists;
1011
var copy = require('..');
1112

1213
describe('copy', function() {
@@ -70,13 +71,17 @@ describe('copy', function() {
7071
});
7172
});
7273

73-
it.only('should copy nested directories', function(cb) {
74+
it('should copy nested directories', function(cb) {
7475
var src = ['test/fixtures/nested/**/*.txt', 'test/fixtures/nested-2/**/*'];
7576
var dest = 'test/actual';
76-
var expected = support.createExpected(['nested/**/*.txt', 'nested-2/**/*'], {cwd: 'test/fixtures', dest: dest});
77-
copy(src, dest, function(err) {
77+
var expected = support.createExpected(['nested/**/*.txt', 'nested-2/**/*'], {
78+
cwd: 'test/fixtures',
79+
destBase: dest
80+
});
81+
82+
copy(src, dest, {srcBase: 'test/fixtures'}, function(err) {
7883
if (err) return cb(err);
79-
existsEach(expected, cb);
84+
eachExists(expected, cb);
8085
});
8186
});
8287

test/support/index.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ exports.createExpected = function(pattern, options) {
4949
var opts = utils.extend({cwd: process.cwd()}, options);
5050
var filepaths = utils.glob.sync(pattern, opts);
5151
return filepaths.map(function(filepath) {
52-
var file = utils.toFile(path.resolve(opts.cwd, filepath), {cwd: opts.cwd, read: false});
53-
file.path = path.resolve(opts.dest, file.relative);
54-
return file;
52+
return {dest: path.resolve(opts.destBase, filepath)};
5553
});
5654
};

0 commit comments

Comments
 (0)