Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env node
- const readline = require('readline');
- const fs = require('fs');
- const program = require('commander');
- const chalk = require('chalk');
- var consoleArguments = process.argv;
- var fileValue ='';
- var linesArray = [];
- var main = function() {
- }
- function readFile(fileValue, callback){
- fs.readFile(fileValue, function(err, data) {
- console.log("Hello from fs.readFile callback! nReading file: " + fileValue);
- var i = 0;
- if(err) throw err;
- linesArray = data.toString().split("n");
- console.log("Hello from fs.readFile callback! Have just read the contents of file.nThere are " + linesArray.length + " lines in the file!");
- for(i = 0; i <= linesArray.length - 1; i++) {
- //console.log(i +": " + linesArray[i]);
- }
- console.log("Returned linesArray from fs.readFile callback!");
- return linesArray;
- });
- console.log("Type of callback: " + typeof(callback));
- if (typeof callback === "function") {
- // Call it, since we have confirmed it is callable
- callback(linesArray);
- console.log("Called callback");
- }
- }
- program
- .version('0.0.1')
- .arguments('<file>')
- .usage('[options] <file>')
- .option('-n, --number <number>', 'Display the last number of lines')
- .option('-f, --follow', 'Follow the file and display the last 10 lines as new lines are appended')
- .action(function(file) {
- fileValue = file;
- })
- .parse(process.argv);
- if(!program.args.length) {
- program.help();
- } else {
- //console.log('Keywords: ' + program.args);
- console.log("File: " + fileValue);
- if (program.number && program.follow) {
- console.log(' - follow');
- console.log(' - number');
- console.log('Number passed to number option: ' + program.number);
- } else {
- if (program.number) {
- console.log(' - number');
- console.log('Number passed to number option: ' + program.number);
- console.log(readFile(fileValue, function(linesArray){
- console.log("Hello from readFile callback! nAbout to output file lines!");
- var i = 0;
- var count = linesArray.length;
- var totalString = "";
- console.log(count);
- for (i = count - 11; i <= count - 1; i++) {
- totalString += linesArray[i];
- totalString += 'n';
- console.log(linesArray[i]);
- }
- }));
- }
- if (program.follow) {
- console.log(' - follow');
- }
- }
- }
- if (program.args === null) program.help();
- File: Word_List.txt
- - number
- Number passed to number option: 10
- Type of callback: function
- Hello from readFile callback!
- About to output file lines!
- 0
- undefined
- undefined
- undefined
- undefined
- undefined
- undefined
- undefined
- undefined
- undefined
- undefined
- undefined
- Called callback
- undefined
- Hello from fs.readFile callback!
- Reading file: Word_List.txt
- Hello from fs.readFile callback! Have just read the contents of file.
- There are 235887 lines in the file!
- Returned linesArray from fs.readFile callback!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement