Skip to content

Instantly share code, notes, and snippets.

@joshblack
Created May 19, 2014 13:47
Show Gist options
  • Save joshblack/31f6e9837c9ccb0a95c7 to your computer and use it in GitHub Desktop.
Save joshblack/31f6e9837c9ccb0a95c7 to your computer and use it in GitHub Desktop.
Walkthrough of developing flashcards.js for Web Dev Workshops
// First thing that we will need to set up are our dependencies for the application.
// Since we are using Node.js we need to utilize it's readline module to take in input
// from STDIN as well as output to STDOUT.
// 1. Import our dependencies
var readline = require('readline'),
rl = readline.createInterface(process.stdin, process.stdout);
// In the above code I've required the readline module and created its interface that
// allows us to read input from the command line as well as send output to the command line.
// Now that we have that, we can set up the prompt for our mini CLI that we will use for
// our flashcard application.
// Setup the prompt style
rl.setPrompt('$ ');
rl.prompt();
// This will prompt the user for their input. Obviously, before this shows we will want to add
// our greeting message + action that we expect from the user. Let's add that now.
// Write our intro messages
process.stdout.write("Welcome to the flashcard application!\n");
process.stdout.write("What would you like to do?\n");
// Now that we have a prompt for user input, we need to setup an event listener
rl.on('line', function(response) {
// do something with response
console.log("Response: " + response);
});
// This allows Node to listen in on whenever we hit the return key and will fire the function
// given as the second parameter. This function is also known as a callback. Let's give it a shot.
// We can see that it works, although we also run into the issue that our program never ends.
// Let's fix that.
rl.on('line', function(response) {
// do something with response
console.log("Response: " + response);
}).on('close', function() {
console.log('Goodbye!');
process.exit(0);
});
// Now we have the basics of our simple CLI for our flashcard application. Unfortunately,
// Node.js doesn't handle waiting for user prompts since that's against its design. As a
// result, developers often enter a place called callback hell where they wait and wait and
// wait for events to happen and it ends up looking like a tangled nest of unintelligible code.
// Using this basic setup, I look to simplify our utilization of node and try to avoid this caveat
// of the language.
// Now that we have our CLI setup, I want to start thinking about what I want to be able to do
// with this flashcard application. A good place to start would be to allow the user to type
// 'help' into the prompt and see a list of options which they can perform. Let's do that.
rl.on('line', function(response) {
if (response === 'help') {
process.stdout.write("You can perform the following actions:\n");
process.stdout.write("1) Add deckName deck\n");
process.stdout.write("2) Add flashcard-front/flashcard-back flashcard to deckName deck\n");
process.stdout.write("3) Remove deckName deck\n");
process.stdout.write("4) Remove flashcard-front/flashcard-back flashcard from deckName deck\n");
}
else if (response === 'exit') {
rl.close(); // Let's add an exit branch as well for good measure
}
}).on('close', function() {
console.log('Goodbye!');
process.exit(0);
});
// Now the user has the ability to type help and the see the options available to him or her,
// and they can also exit out of our flashcard application.
// Obviously, a key feature is missing here and that is parsing of the input of available actions
// to our user. i.e., when the user says "Add French Deck", how are we going to implement that?
// Let's start with "Add deckName Deck"
rl.on('line', function(response) {
if (response === 'help') {
process.stdout.write("You can perform the following actions:\n");
process.stdout.write("1) Add deckName deck\n");
process.stdout.write("2) Add flashcard-front/flashcard-back flashcard to deckName deck\n");
process.stdout.write("3) Remove deckName deck\n");
process.stdout.write("4) Remove flashcard-front/flashcard-back flashcard from deckName deck\n");
}
else if (response === 'exit') {
rl.close(); // Let's add an exit branch as well for good measure
}
else {
}
}).on('close', function() {
console.log('Goodbye!');
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment