diginext-utils
Version:
README.md
46 lines (45 loc) • 1.49 kB
JavaScript
var path = require("path");
var fs = require("fs");
/**
* This is an alias of "findFileByExt()"
*/
export const forEachFileByExt = (base, ext, cb) => {
var walk = function (directoryName) {
try {
fs.readdir(directoryName, function (e, files) {
if (e) {
console.log("Error: ", e);
return;
}
files.forEach(function (file) {
var fullPath = path.join(directoryName, file);
fs.stat(fullPath, function (e, f) {
if (e) {
console.log("Error: ", e);
return;
}
if (f.isDirectory()) {
walk(fullPath);
}
else {
if (fullPath.toLowerCase().indexOf(ext.toLowerCase()) > -1) {
if (cb)
cb(fullPath);
}
}
});
});
});
}
catch (error) {
console.log("error", error);
}
};
walk(base);
};
/**
* This is an alias of "forEachFileByExt()"
*/
export const findFileByExt = forEachFileByExt;
const fileExt = { findFileByExt, forEachFileByExt };
export default fileExt;