Skip to content

Commit

Permalink
The Command problem
Browse files Browse the repository at this point in the history
Commands are objects that contain all of the data necessary to execute a
specific action on the client. The commander doesn't need to know how to
execute each piece a specific action. Each command is different, they
contain different parts and different instructions. The index.js file
contains a code where we can incorporate a command pattern. It builds
the prompt using the create interface method from read line. And our
process standard input and process standard output is going to be our
interface. And on line eight we are adding our first prompt. Whenever
the user enters any text we go ahead and handle it. Lines 11-13 are just
a matter of parsing that text to find the command and the actual file
name or a file text that would go into that command. And finally, we
have a switch statement where we are ready to run our commands. So if we
run an Exit, we've left ourselves a little to do for adding an Exit
command; and if we run a Create, we've left ourselves a little to do for
running a Create command. So we've already parsed the file name and the
text that would go into the file that needs to be created for the user.
And also if the user don't run either of these commands, is just let the
user know that it don't know what the user is trying to do.
  • Loading branch information
diegomais committed Jun 1, 2020
1 parent e783af0 commit dead876
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions command-pattern/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var { createInterface } = require("readline");
var rl = createInterface({
input: process.stdin,
output: process.stdout,
});

console.log("create <fileName> <text> | exit");
rl.prompt();

rl.on("line", (input) => {
var [commandText, ...remaining] = input.split(" ");
var [fileName, ...fileText] = remaining;
var text = fileText.join(" ");

switch (commandText) {
case "exit":
console.log("TODO: Exit");
break;

case "create":
console.log(`TODO: Create File ${fileName}`);
console.log("file contents:", text);
break;

default:
console.log(`${commandText} command not found!`);
}

rl.prompt();
});

0 comments on commit dead876

Please sign in to comment.