Guest User

Untitled

a guest
Oct 16th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. const glob = require('glob');
  2. const ROOT = 'root';
  3. const EXT = 'ext';
  4.  
  5. function getSettings (argv) {
  6. return argv.reduce((params, arg, i, all) => {
  7. if (arg.indexOf('--') === 0 && all[i + 1]) {
  8. params[arg.slice(2)] = all[i + 1];
  9. }
  10.  
  11. return params;
  12. }, {});
  13. }
  14.  
  15. function checkSettings(settings) {
  16.  
  17. if (!settings[ROOT]) {
  18. throw 'Please provide the root path for search';
  19. }
  20.  
  21. if (!settings[EXT]) {
  22. throw 'Please provide file extention that have to be searched';
  23. }
  24.  
  25. return true;
  26. }
  27.  
  28. function getRootFolders(paths, root) {
  29. const roots = {};
  30. let counter = 0;
  31.  
  32. paths.forEach(path => {
  33.  
  34. const rootFolder = path.replace(root, '').replace(/^\//ig, '').split('/')[0];
  35.  
  36. if (typeof roots[rootFolder] !== 'undefined') {
  37. roots[rootFolder] += 1;
  38. counter += 1;
  39. } else {
  40. roots[rootFolder] = 1;
  41. }
  42. });
  43.  
  44. return {
  45. counter,
  46. folders: roots
  47. }
  48. }
  49.  
  50.  
  51. const settings = getSettings(process.argv);
  52.  
  53. if (checkSettings(settings)) {
  54. const root = settings[ROOT].replace(/\/$/ig, '');
  55.  
  56. glob(`${root}/**/*.${settings[EXT]}`, null, (er, files) => {
  57. const {counter, folders} = getRootFolders(files, root);
  58. console.log('total amount:', counter);
  59. console.log('');
  60. console.log('folders:', folders)
  61. })
  62. }
Add Comment
Please, Sign In to add comment