Guest User

Untitled

a guest
Mar 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. // Get needed arguemnts for running the script
  2. // USAGE: node search [EXT] [TEXT]
  3. var args = process.argv.slice(2);
  4.  
  5. if(args[0].length == 0 && args[1].length == 0) {
  6. console.log("USAGE: node search [EXT] [TEXT]");
  7. process.exit();
  8. }
  9.  
  10. ext_args = args[0];
  11. word_args = args[1];
  12.  
  13. var fs = require('fs'); // include file reader module
  14. var path = process.cwd(); // get current path of script
  15. var fileList = getFiles(path, ext_args, word_args);
  16. console.log(fileList);
  17. console.log("end");
  18. /*
  19. Function that get files recursivly in a current script path
  20. */
  21. function getFiles(path, ext, word) {
  22. var fileList = [];
  23. fs.readdir(path, function(err, items) {
  24. for (var i=0; i<items.length; i++) {
  25. var currentFile = items[i];
  26. var itemPath = path + "\" + currentFile;
  27. if(fs.lstatSync(itemPath).isDirectory()) {
  28. return getFiles(path + '\' + currentFile, ext, word);
  29. } else {
  30. if(currentFile.endsWith(ext)) {
  31. fileList.push(checkString(itemPath, word));
  32. } else {
  33. }
  34. }
  35. }
  36. });
  37. return fileList;
  38. }
  39.  
  40. /*
  41. Function that checks if the word exist in the text file
  42. */
  43. function checkString(filePath, word) {
  44. fs.readFile(filePath, 'utf8', function(err, data) {
  45. //console.log("> Checking String");
  46. if(err) throw err;
  47. if(data.indexOf(word) >= 0) {
  48. return filePath;
  49. } else {
  50. }
  51.  
  52. })
  53. }
  54.  
  55. // Get needed arguemnts for running the script
  56. // USAGE: node search [EXT] [TEXT]
  57. var args = process.argv.slice(2);
  58.  
  59. if (args[0].length == 0 && args[1].length == 0) {
  60. console.log("USAGE: node search [EXT] [TEXT]");
  61. process.exit();
  62. }
  63.  
  64. ext_args = args[0];
  65. word_args = args[1];
  66.  
  67. var fs = require('fs'); // include file reader module
  68. var path = process.cwd(); // get current path of script
  69. getFiles(path, ext_args, word_args).then((fileList) => {
  70. console.log(fileList);
  71. console.log("end");
  72. });
  73. /*
  74. Function that get files recursivly in a current script path
  75. */
  76. function getFiles(path, ext, word) {
  77. var fileList = [];
  78. return new Promise((resolve, reject) => {
  79. fs.readdir(path, (err, items) => {
  80. for (var i = 0; i < items.length; i++) {
  81. var currentFile = items[i];
  82. var itemPath = path + "\" + currentFile;
  83. if (fs.lstatSync(itemPath).isDirectory()) {
  84. return getFiles(path + '\' + currentFile, ext, word);
  85. } else {
  86. if (currentFile.endsWith(ext)) {
  87. fileList.push(checkString(itemPath, word));
  88. } else {}
  89. }
  90. }
  91. });
  92. resolve(fileList);
  93. })
  94. }
  95.  
  96. /*
  97. Function that checks if the word exist in the text file
  98. */
  99. function checkString(filePath, word) {
  100. return new Promise((resolve, reject) => {
  101. fs.readFile(filePath, 'utf8', (err, data) => {
  102. //console.log("> Checking String");
  103. if (err) {
  104. reject(err);
  105. }
  106. if (data.indexOf(word) >= 0) {
  107. resolve(filePath);
  108. }
  109. })
  110. })
  111. }
Add Comment
Please, Sign In to add comment