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
Open CommandBox and create an empty folder in the location of your choosing.
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
Back in CommandBox, type
initto create abox.jsonwhich is what makes this a package.Edit the
box.jsonby changing the following keys (at the minimum)slug :
yournametype: "commandbox-modules"
In CommandBox type
package link. You should see a response saying that CommandBox has linked to the packge.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