-
Notifications
You must be signed in to change notification settings - Fork 0
/
print_module.js
54 lines (47 loc) · 1.15 KB
/
print_module.js
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
"use strict"
var fs = require('fs');
var path = require('path');
var Q = require('q');
function readFilesFromDir(dir) {
var defer = Q.defer();
fs.readdir(dir, function(err, files) {
if (err) defer.reject(new Error(err));
defer.resolve(files);
});
return defer.promise;
}
function filterFiles(files, ext) {
var defer = Q.defer();
// console.log(arguments);
var filteredFiles = files.filter(function(val){
return path.extname(val) === '.' + ext;
});
defer.resolve(filteredFiles);
return defer.promise;
}
function printToConsole(list) {
list.forEach(function(val){
console.log(val);
})
}
function printFilteredFiles(dir, ext, callback) {
readFilesFromDir(dir)
.then(function(files) {
return Q.fcall(filterFiles, files, ext)
}, function(err) {
throw new Error(err);
})
.then(callback);
}
// function printFilteredFiles(dir, ext, callback) {
// fs.readdir(dir, function(err, files) {
// if (err) {
// return callback(err);
// }
// var filteredFiles = files.filter(function(val){
// return path.extname(val) === '.' + ext;
// });
// return callback(null, filteredFiles);
// });
// }
module.exports = printFilteredFiles;