Class: Macro

Macro

This extended Command-class is the build-in macro-command. It's implementation of the execute() method, is a mechanism to synchronously run subcommands and return their individual results as an array of CommandResults, wrapped inside a promise. It totally depends on the user configuration file, which can be specified in the module's 'config.json'. The user configuration must have a key called 'cmdSequence', in which a chain of commands is specified as an array. Since those commands are assumed to depend on each other, they will be executed in order from left to right (start with index 0 and increment with 1).
Also checkout: Example project

IMPORTANT NOTE!: Each command that extends the abstract Command-class must return a promise. Since each command has a build in CommandResult object, you can use this to store your data, and pass it around.

Constructor

new Macro()

Author:
  • ruckola
Source:

Extends

Methods

abort(err) → {Promise}

Will be removed in the next major version. If the execution fails before the command task() has returned a promise, we have to return a rejected promise manually to the client. Since there is no data to return yet, we pass an error object as the value.
Parameters:
Name Type Description
err Error An error object that will be set as the value for the rejected promise.
Inherited From:
Deprecated:
  • Yes
Source:
Returns:
- Allways rejects with an error object as its value.
Type
Promise

execute(context) → {Promise}

The execute method is used to run the command. Normally this is taken care of by the controller and you don't have to worry about it at all. When you start developing your own subcommands the following is very important:

1) Each subcommand that extends this abstract Command-class must override the execute()-method. This is the entry and exit point, for your own implementation.
2) The execute() method must have a 'context'-parameter.
3) The execute() method must return a new Promise.
4) The promise must resolve/reject with a value of type CommandResult.
Parameters:
Name Type Description
context CommandContext The context object is used to inject the data that is required to successfully execute the command. It contains all configurations and all commandResult-objects of any previous subcommands in the same macro. So you don need any other parameters. You can even add extra parameters to the context on-the-fly, and the new parameters will be available to any subcommand running after this one. (in the same macro)
Overrides:
Source:
Returns:
- The value of the promise should be this.result in most cases.
Type
Promise
Example

Sample subcommand extending this abstract Command class and overriding the 'execute()' method

Command = require('macro-command').Command;
class Example extends Command {

 execute(context) {

          var key = this.constructor.name;
          this.context = context;
          var self = this;
          var cmdConf = context.parameters.cmdConfiguration;

          return new Promise(function (resolve, reject) {

              //CHECK REQUIRED INPUT PARAMETERS ...

              //DO SOMETHING HERE ...

              self.result.cmd = key;
              self.result.data.push({ 'some example key': 'some example value' });
              self.result.msg = 'The result from ' + key;
              resolve(self.result);

          });
      }
  }
module.exports = Example;

findPrevResult(commandName, contextopt) → {commandResult}

Finds and returns the commandResult of a previous command.
Parameters:
Name Type Attributes Default Description
commandName string The name of the previous command.
context commandContext <optional>
false A context other than the current context.
Inherited From:
Source:
Returns:
- The result of the previous command.
Type
commandResult