Guest User

Untitled

a guest
Feb 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. var fs = require('fs');
  2. var path = require('path');
  3.  
  4. function isDirectory(dirPath) {
  5. return fs.statSync(dirPath).isDirectory();
  6. }
  7.  
  8. function endsWithExtension(file, extension) {
  9. return file.endsWith(extension);
  10. }
  11.  
  12. class FileSearch {
  13.  
  14. constructor(fileExtension, skipDirectories = null) {
  15. this.fileExtension = fileExtension || '';
  16. if (!fileExtension.includes('.')) {
  17. throw new Error('File extension should contain a dot (.)');
  18. }
  19. this.skipDirectories = skipDirectories || ['.vscode', '.git', 'node_modules'];
  20. }
  21.  
  22. /* Function for borrow (namespace.js)*/
  23. /**
  24. *
  25. * @param {string} item - full path to the directory or a file
  26. */
  27. skipCurrentDirectory(item) {
  28. return this.skipDirectories.includes(item);
  29. }
  30.  
  31. getFiles(dirPath, files) {
  32. files = files || [];
  33.  
  34. var items = fs.readdirSync(dirPath);
  35. items.forEach(item => {
  36. if (this.skipCurrentDirectory(item)) {
  37. return;
  38. }
  39. var fullPath = path.join(dirPath, item);
  40. if (isDirectory(fullPath)) {
  41. files = this.getFiles(fullPath, files);
  42. } else if (endsWithExtension(item, this.fileExtension)) {
  43. files.push(fullPath);
  44. }
  45. });
  46. return files;
  47. }
  48.  
  49. }
  50.  
  51. module.exports = FileSearch;
Add Comment
Please, Sign In to add comment