Start where we left off

In the previous part of this tutorial, we have created a command that can perform a simple calculation. It takes an configurable input value from parameter ‘n‘ and multiplies it by 100. Then it stores the outcome of this calculation inside a commandResult object, which will be the value of the resolved promise when it is returned by the command. So our next task is to configure the command.

Configure a single command

The macro-command can run in single-mode or in macro-mode. This example will show you how to configure a command in single-mode.
(The configuration of commands in macro-mode will be explained in another part of this tutorial.)
In part one we have set-up the directory structure of our project. I contains a ‘config‘-directory in which all configuration files must be stored. So, lets create one for our FirstCommand class.

Now I can edit the file and set a value for parameter ‘n’ as follows:

It is as easy as that! However, if the number of commands that you create gets bigger and bigger, it will get hard, not to say impossible, to remember all the required parameters for each individual class.  Therefore you should consider documenting your classes before things run out of control. In the last part of this tutorial I will provide you with a strategy for building documentation on your commands by using JSDOC.

In single-mode, the macro-command will (in our case) search for a file called   ‘firstCommand.json’. If it is not there, or misspelled, the application will warn us about the missing config-file, but still continue running.

The configuration-file is optional.
It is the responsibility of the command to deal with any missing parameters. 

Create an application

To run the command, we first need an application. So, lets create one in the root of our project:

Now edit the file as follows:

This is all it takes to run your command and log it’s result to the console.
First, I require the ‘Kickstarter‘-class from the macro-command package.
Then I create a new ‘kickstarter‘-object and pass it the (file)name of the command that I want to run.
Finally, I invoke the run() method of the kickstarter and log the returned value to the console, after the promise is resolved.
Again, this is the bare minimum of code required to make things work, but in real-life you should of course add more code to catch any exceptions and handle any errors correctly.

Run the command by running the application

Now, when we run the above application,

we should get an output similar to this:

Summary

With 10 lines of code we were able to configure and to run our first command. How cool is that! Lets take this a step further and create another command that depends upon our first command. Continue to the next part of this tutorial, to see how we prepare ourselves for the final goal of this tutorial; Running multiple commands in sync that depend upon each others output.

< Previous part    |    Next part >