Tracking our thoughts. It is something we all think about doing at one time or another. Myself I have tried to start a journal and keep up with it countless times. I usually get several days in to a couple weeks then move on to something else.
I don’t think that is such a bad thing. Sometimes you need to think something out over a couple days and then you are good to go.
So today we are going to build a Command Line Journal App. You can run it from your powershell terminal and it will check for a journal.txt file and if no such file exists it will create one and take entries until you tell it to stop or quit.
Let’s get started!
First we need to dependencies fs and readline:
const fs = require('fs');
const readline = require('readline');
Our journal is going to contain a couple of different parts with multiple tasks in each part, so I want to take a sec to add some comments that will serve as a plan for the application.
// create a variable for file path
// prepare rl variable
// check if a file exists
// if file exists open the file
// // open the file
// Ask for users name and then write name in Journal by template string
// get input from user
// Create a file by writing the word journal to the file
// if input is exit or quit then rl.close
So we want to try to open an existing file and if we throw an error (the file doesn’t exist) then we want to alert the user of the error, let them know we are creating a file and then create a file with the text “Journal inside:
let textIn ;
try {
// open the file
textIn = fs.readFileSync(FILEPATH, 'utf-8');
} catch (err) {
console.log('Error', err);
console.log('Creating File...');
// Ask for users name and then write name in Journal by template string
//TODO at Later Date
// Create a file by writing the word journal to the file
fs.writeFileSync(FILEPATH, 'Journal');
}
Now that we have a file open and stored in a variable we have to accept some input from the user and write it to the file. I initially try to use a while loop to continue to take input from the user and write to file until the entry is quit, but this created a loop that crashed VS Code.
I found an article on stack over flow where a user recommends using recursion to solve this issue.
The other suggestions looked interesting but I wasn’t able to wrap my head around them at this time. I copied the recursion code to my file and commented out my old erroneous code. Then I copied what was necessary from my code replacing the example code to come up with the following code:
var recursiveAsyncReadLine = function (textIn) {
rl.question('Journal Entry: ', function (entry) {
if (entry == 'exit' || entry == 'quit') //we need some base case, for recursion
return rl.close(); //closing RL and returning from function.
fs.writeFileSync(FILEPATH, `${textIn} \n (${Date.now()}) - ${entry}`);
recursiveAsyncReadLine(); //Calling this function again to ask new question
});
};
recursiveAsyncReadLine(textIn);
Finally, we need to exit the app on “close” and output the file to the console for all to see.
rl.on("close", function() {
console.log("\n Journal thus far... \n");
console.log(fs.readFileSync(FILEPATH, 'utf-8'));
process.exit(0);
});
That did it! We now have a command line app that will read a file or create one if journal.txt doesn’t exist and then ask for input from the user and will write each entry to a file until the user types “quit” or “exit” into the prompt.
Check out the video, like, subscribe and leave a comment. Thanks for your time!