Modules

Modules are one of the most powerful yet underused features of CommandBox. We're going to create a very simple module in CommandBox so get an idea of how to create these power peices of code.

Configure the Module

  1. Open CommandBox and create an empty folder in the location of your choosing.

  2. Open your IDE to that folder and create a file called ModuleConfig.cfc using the below as the contents. Use your own name in place of yourname.\

    component {
        this.mapping = "yourname" ;
        this.modelNameSpace="yourname";
        this.cfmapping="yourname";
    
        function configure(){}
    }

Create a Command

Create a new folder called commands/yourname and in it create a file called first.cfc. Using this as a template:

component {
    function run(){
        print.line("HELLO MY FIRST COMMAND");
    }
}

Make it a Package

  1. Back in CommandBox, type init to create a box.json which is what makes this a package.

  2. Edit the box.json by changing the following keys (at the minimum)

    1. slug : yourname

    2. type: "commandbox-modules"

  3. In CommandBox type package link. You should see a response saying that CommandBox has linked to the packge.

  4. Type r in CommandBox to reload the shell.

Use the Command

Once the shell has reloaded type the beginning of your name and hit tab. It should autocomplete and then put in your command in next (first). Press enter and it will output your message.

Refactoring to Use an input

Back in first.cfc, adapt your code to accept a parameter using this as a template

component {
    function run(message = "yoyoyoyo"){
        print.line(arguments.message);
    }
}

Additional Reading

Last updated