Advertisement
Guest User

Untitled

a guest
Jul 24th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. #!/usr/bin/env node
  2.  
  3. const readline = require('readline');
  4. const fs = require('fs');
  5. const program = require('commander');
  6. const chalk = require('chalk');
  7.  
  8. var consoleArguments = process.argv;
  9.  
  10. var fileValue ='';
  11. var linesArray = [];
  12.  
  13. var main = function() {
  14.  
  15. }
  16.  
  17. function readFile(fileValue, callback){
  18. fs.readFile(fileValue, function(err, data) {
  19. console.log("Hello from fs.readFile callback! nReading file: " + fileValue);
  20. var i = 0;
  21. if(err) throw err;
  22. linesArray = data.toString().split("n");
  23. console.log("Hello from fs.readFile callback! Have just read the contents of file.nThere are " + linesArray.length + " lines in the file!");
  24. for(i = 0; i <= linesArray.length - 1; i++) {
  25. //console.log(i +": " + linesArray[i]);
  26. }
  27. console.log("Returned linesArray from fs.readFile callback!");
  28. return linesArray;
  29. });
  30. console.log("Type of callback: " + typeof(callback));
  31. if (typeof callback === "function") {
  32. // Call it, since we have confirmed it is callable​
  33. callback(linesArray);
  34. console.log("Called callback");
  35. }
  36. }
  37.  
  38.  
  39.  
  40. program
  41. .version('0.0.1')
  42. .arguments('<file>')
  43. .usage('[options] <file>')
  44. .option('-n, --number <number>', 'Display the last number of lines')
  45. .option('-f, --follow', 'Follow the file and display the last 10 lines as new lines are appended')
  46. .action(function(file) {
  47. fileValue = file;
  48. })
  49. .parse(process.argv);
  50.  
  51. if(!program.args.length) {
  52. program.help();
  53. } else {
  54. //console.log('Keywords: ' + program.args);
  55. console.log("File: " + fileValue);
  56.  
  57. if (program.number && program.follow) {
  58. console.log(' - follow');
  59. console.log(' - number');
  60. console.log('Number passed to number option: ' + program.number);
  61.  
  62. } else {
  63.  
  64. if (program.number) {
  65. console.log(' - number');
  66. console.log('Number passed to number option: ' + program.number);
  67.  
  68. console.log(readFile(fileValue, function(linesArray){
  69. console.log("Hello from readFile callback! nAbout to output file lines!");
  70. var i = 0;
  71. var count = linesArray.length;
  72. var totalString = "";
  73.  
  74. console.log(count);
  75.  
  76. for (i = count - 11; i <= count - 1; i++) {
  77. totalString += linesArray[i];
  78. totalString += 'n';
  79. console.log(linesArray[i]);
  80. }
  81.  
  82. }));
  83. }
  84.  
  85. if (program.follow) {
  86. console.log(' - follow');
  87. }
  88. }
  89. }
  90.  
  91. if (program.args === null) program.help();
  92.  
  93. File: Word_List.txt
  94. - number
  95. Number passed to number option: 10
  96. Type of callback: function
  97. Hello from readFile callback!
  98. About to output file lines!
  99. 0
  100. undefined
  101. undefined
  102. undefined
  103. undefined
  104. undefined
  105. undefined
  106. undefined
  107. undefined
  108. undefined
  109. undefined
  110. undefined
  111. Called callback
  112. undefined
  113. Hello from fs.readFile callback!
  114. Reading file: Word_List.txt
  115. Hello from fs.readFile callback! Have just read the contents of file.
  116. There are 235887 lines in the file!
  117. Returned linesArray from fs.readFile callback!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement