- [Instructor] In the last lesson
we introduced the command pattern
and we saw how we can incorporate that pattern
into an already existing node app
that implemented a read line interface.
In this lesson, we're going to implement the conductor
that we're importing,
as well as each command class
that we're importing online, too.
So I'm in the exercise files,
under chapter four lesson four, within the start folder.
And I'm going to go ahead and create a new file
called commands.js.
And I'm going to go ahead and put all of my command classes
within this file.
So we're going to need to create a class for the exit command
and we're also going to need to create one for create command.
And then I'm just going to make sure
that I export both of these.
Great, so now we have both classes
and we're ready to export them.
Let's go ahead and write the code for our exit command.
So I'm going to write a getter to get the name of this command.
So we'll just go ahead and return a string.
This is an exit command,
and we'll go ahead and add a bye to that.
Now each command must implement an execute method,
and that's where we're actually going to write the code
to execute this command.
So let's go ahead and execute.
And for the exit command we're just going to go ahead and exit.
And we'll go ahead and process.exit 0.
So great, that's our exit command.
Now before we actually write the code
for the create command,
let's go ahead and create the conductor
and test our exit command.
So I'm going to go ahead and add a new file.
Conductor.js.
And within the conductor we'll create a new class
called conductor,
and we'll make sure we export that class.
And we'll make this a node js singleton,
so we will export one instance
of the conductor from this module.
So we're going to make sure that we only have one conductor
by exporting one instance of the conductor
versus exporting the entire conductor class.
So I'm going to go ahead and have
a run method for the conductor.
And the run method is going to expect a command.
And then I'm just going to go ahead
and log some information to the console,
about which command we're actually running.
And we will send the command name.
And now we will actually execute command
by invoking the execute method.
So this is why each command needs an execute method.
This is how we actually run the command.
Let's go ahead and test it out.
So I'm coming over to my terminal,
and I will node index.js.
And if we type exit,
the conductor should run our exit command.
And it does.
And we can see the message executing command,
exit, bye.
So the first thing that we want to do
is we need to require a few things.
If we're going to create a new file
we need the right file method from the file system.
I'm also going to use the path module,
so we need the path as well.
Now in the create command object itself
we're going to go ahead and add the constructor.
Everything we need to create a file
is being passed to this constructor already.
So I'm going to go ahead and grab the file name
and the text for the file within the constructor,
and we'll set up some variables.
This.fileName is going to be equal to the file name.
This.body will be equal to the file text.
And let's also set up a full path.
This.fullPath is going to equal path.join.
So we're going to take the current directory
and we are going to add file name to it,
creating the full path.
Great, so that's everything that we need
to create a brand new file.
So I'm going to go ahead and also create a getter for the name,
so that we know what command this is,
and we're going to go ahead and return a create this.fileName.
So we know that this command is to create the specific file
that we're creating with this instance
of the create command.
So the next thing that we need is an execute method.
And this is where we're actually going to create the file.
So I'll go ahead and call it writeFile,
and we'll go ahead and say this.fullPath.
So that's the file that we're going to create.
This.body, that's what we're going to write to the file.
And then we need to have a callback function here.
I'm not ready to code this callback function yet.
We're going to do it in a later lesson.
So what I'm going to put is just a placeholder function.
It's a dummy function that I like to use.
So f arrow f.
All this function means is that it's just a function
that returns whatever argument is sent to it,
which is typically undefined.
So it's just a placeholder function.
So let's go ahead and save this
and then see if our create command works.
So I come back out to the terminal.
Okay, now I will run the application.
Node.index.js,
and let's go ahead and try to create a file
called hello.txt and we'll add Hello World to that file.
Ad it says executing command create hello.txt.
So it seems to have executed our command.
Let's try it one more time.
Create hi.txt, and we'll do Hi World in that one
with one exclamation point.
And it says that we've also executed that command.
So if we go ahead and take a look at our file folder,
we should see two text files.
We have one, hello text and one hi text.
So this is using the command pattern.
Everything that we need to create a file,
encapsulate it inside of this create command that we coded
inside of the commands.js module.
In the next lesson we're going to go ahead
and improve our conductor class to see the full power
that you can get out of using the command pattern.