Skip to content

Commit ec0a3da

Browse files
committed
fix empty folders
1 parent 1927d64 commit ec0a3da

7 files changed

Lines changed: 1426 additions & 1384 deletions

File tree

CHANGELOG.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# path-reader - Change Log
2+
All notable changes to this project will be documented here.
3+
4+
## [1.0.5] - 2017-10-13
5+
### Fixed
6+
- empty folder reads are now empty array instead of undefined
7+

lib/paths.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,12 @@ exports.files = function files(dir, type, callback, options) {
151151

152152
const onDirRead = function(err, list) {
153153
if (err) return callback(err);
154-
154+
155155
pending = list.length;
156-
if (!pending) return done();
156+
if (!pending){
157+
done(list);
158+
return list
159+
}
157160

158161
var statHanOptions = {}
159162
if(options.shortName){

lib/readfiles.js

Lines changed: 95 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
var fs = require('fs'),
2-
path = require('path');
2+
path = require('path');
33

44
/**
55
* merge two objects by extending target object with source object
@@ -9,14 +9,14 @@ var fs = require('fs'),
99
* @returns {Object} extended object
1010
*/
1111
function extend(target, source, modify) {
12-
var result = target ? modify ? target : extend({}, target, true) : {};
13-
if (!source) return result;
14-
for (var key in source) {
15-
if (source.hasOwnProperty(key) && source[key] !== undefined) {
16-
result[key] = source[key];
17-
}
12+
var result = target ? modify ? target : extend({}, target, true) : {};
13+
if (!source) return result;
14+
for (var key in source) {
15+
if (source.hasOwnProperty(key) && source[key] !== undefined) {
16+
result[key] = source[key];
1817
}
19-
return result;
18+
}
19+
return result;
2020
}
2121

2222
/**
@@ -26,8 +26,8 @@ function extend(target, source, modify) {
2626
* @returns {Boolean} whether there is a match
2727
*/
2828
function matches(str, match) {
29-
if (Array.isArray(match)) return match.indexOf(str) > -1;
30-
return match.test(str);
29+
if (Array.isArray(match)) return match.indexOf(str) > -1;
30+
return match.test(str);
3131
}
3232

3333
/**
@@ -39,84 +39,96 @@ function matches(str, match) {
3939
* @param {Function(error)} complete fn to call when finished
4040
*/
4141
function readFiles(dir, options, callback, complete) {
42-
if (typeof options === 'function') {
43-
complete = callback;
44-
callback = options;
45-
options = {};
46-
}
47-
if (typeof options === 'string') options = {
48-
encoding: options
49-
};
50-
options = extend({
51-
recursive: true,
52-
encoding: 'utf8',
53-
doneOnErr: true
54-
}, options);
55-
var files = [];
42+
if (typeof options === 'function') {
43+
complete = callback;
44+
callback = options;
45+
options = {};
46+
}
47+
if (typeof options === 'string') options = {
48+
encoding: options
49+
};
50+
options = extend({
51+
recursive: true,
52+
encoding: 'utf8',
53+
doneOnErr: true
54+
}, options);
55+
var files = [];
5656

57-
var done = function(err) {
58-
if (typeof complete === 'function') {
59-
if (err) return complete(err);
60-
complete(null, files);
61-
}
62-
};
57+
var done = function(err) {
58+
if (typeof complete === 'function') {
59+
if (err) return complete(err);
60+
complete(null, files);
61+
}
62+
};
6363

64-
fs.readdir(dir, function(err, list) {
65-
if (err) {
66-
if (options.doneOnErr === true) {
67-
if (err.code === 'EACCES') return done();
68-
return done(err);
69-
}
70-
}
71-
var i = 0;
64+
fs.readdir(dir, function(err, list) {
65+
if (err) {
66+
if (options.doneOnErr === true) {
67+
if (err.code === 'EACCES') return done();
68+
return done(err);
69+
}
70+
}
71+
var i = 0;
7272

73-
if (options.reverse === true ||
74-
(typeof options.sort == 'string' &&
75-
(/reverse|desc/i).test(options.sort))) {
76-
list = list.reverse();
77-
} else if (options.sort !== false) list = list.sort();
73+
if (options.reverse === true ||
74+
(typeof options.sort == 'string' &&
75+
(/reverse|desc/i).test(options.sort))) {
76+
list = list.reverse();
77+
} else if (options.sort !== false) list = list.sort();
7878

79-
(function next() {
80-
var filename = list[i++];
81-
if (!filename) return done(null, files);
82-
var file = path.join(dir, filename);
83-
fs.stat(file, function(err, stat) {
84-
if (err && options.doneOnErr === true) return done(err);
85-
if (stat && stat.isDirectory()) {
86-
if (options.recursive) {
87-
if (options.matchDir && !matches(filename, options.matchDir)) return next();
88-
if (options.excludeDir && matches(filename, options.excludeDir)) return next();
89-
readFiles(file, options, callback, function(err, sfiles) {
90-
if (err && options.doneOnErr === true) return done(err);
91-
files = files.concat(sfiles);
92-
next();
93-
});
94-
} else next();
95-
} else if (stat && stat.isFile()) {
96-
if (options.match && !matches(filename, options.match)) return next();
97-
if (options.exclude && matches(filename, options.exclude)) return next();
98-
if (options.filter && !options.filter(filename)) return next();
99-
if (options.shortName) files.push(filename);
100-
else files.push(file);
101-
fs.readFile(file, options.encoding, function(err, data) {
102-
if (err) {
103-
if (err.code === 'EACCES') return next();
104-
if (options.doneOnErr === true) {
105-
return done(err);
106-
}
107-
}
108-
if (callback.length > 3)
109-
if (options.shortName) callback(null, data, filename, next);
110-
else callback(null, data, file, next);
111-
else callback(null, data, next);
112-
});
113-
}
114-
else {
115-
next();
116-
}
79+
(function next() {
80+
var filename = list[i++];
81+
if (!filename) return done(null, files);
82+
83+
var file = path.join(dir, filename);
84+
85+
fs.stat(file, function(err, stat) {
86+
if (err && options.doneOnErr === true) return done(err);
87+
if (stat && stat.isDirectory()) {
88+
if (options.recursive) {
89+
if (options.matchDir && !matches(filename, options.matchDir)) return next();
90+
if (options.excludeDir && matches(filename, options.excludeDir)) return next();
91+
readFiles(file, options, callback, function(err, sfiles) {
92+
if (err && options.doneOnErr === true) return done(err);
93+
files = files.concat(sfiles);
94+
next();
11795
});
118-
})();
96+
} else next();
97+
} else if (stat && stat.isFile()) {
98+
if (options.match && !matches(filename, options.match)) return next();
99+
if (options.exclude && matches(filename, options.exclude)) return next();
100+
if (options.filter && !options.filter(filename)) return next();
101+
102+
if (options.shortName){
103+
files.push(filename);
104+
}else{
105+
files.push(file);
106+
}
107+
108+
fs.readFile(file, options.encoding, function(err, data) {
109+
if (err) {
110+
if (err.code === 'EACCES') return next();
111+
if (options.doneOnErr === true) {
112+
return done(err);
113+
}
114+
}
115+
if (callback.length > 3){
116+
if (options.shortName){
117+
callback(null, data, filename, next);
118+
}else{
119+
callback(null, data, file, next);
120+
}
121+
}else{
122+
callback(null, data, next);
123+
}
124+
});
125+
}
126+
else {
127+
next();
128+
}
129+
});
130+
})();
119131

120-
});
132+
});
121133
}
122134
module.exports = readFiles;

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "path-reader",
3-
"version": "1.0.4",
3+
"version": "1.0.5",
44
"description": "asynchronous file and directory operations for Node.js",
55
"main": "index",
66
"homepage": "https://github.com/ackerapple",

test/fixtures/testdir5/testuções.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)