Creating Salesforce CLI plugins for extra SFDX goodness

Kevin Boyle on

Share with

LinkedIn
Twitter

At Gearset we’ve stayed on the cutting edge of DevOps on the Salesforce platform by taking advantage of the new APIs that Salesforce gives us in each release. With the release of Summer ‘18 I had a go at writing a plugin for the Salesforce CLI (sfdx) to expose the new Metadata Coverage report in the terminal.

Salesforce DX is an enabling technology and each layer of the stack has been written with extensibility in mind. This is evident throughout the new APIs and with Scratch Orgs, but shines through clearest in the Salesforce CLI and its plugin architecture.

If you’re not familiar with the Salesforce CLI then the short version is that it’s the foundational tool that other tools are built on. Things like Visual Studio Code and Gearset build on the sfdx command and don’t need to reinvent a whole bunch of plumbing, like authentication. Out of the box it comes with commonly-used base functions:

> sfdx

Usage: sfdx COMMAND

Help topics, type sfdx help TOPIC for more details:

 force     tools for the Salesforce developer
 help      display help for <%= config.bin %>
 plugins   add/remove/create CLI plug-ins
 update    update the sfdx CLI

If you want to dig into any of those areas, you can see that each “topic” has “commands” that hang off it and do a particular task for you.

> sfdx help force:apex

Usage: sfdx force:apex:COMMAND [command-specific-options]

work with Apex code

sfdx force:apex commands: (get help with sfdx help force:apex:COMMAND)
 force:apex:class:create    create an Apex class
 force:apex:execute         execute anonymous Apex code
 force:apex:log:get         fetch a debug log
 force:apex:log:list        list debug logs
 force:apex:log:tail        start debug logging and display logs
 force:apex:test:report     display test results
 force:apex:test:run        invoke Apex tests
 force:apex:trigger:create  create an Apex trigger

This topic and command approach allows people to plug in to the CLI and expose topics or commands that make sense for them.

Pete Chittum, whilst being his usual awesome self, gave me the idea to expose the new Metadata Coverage Report, released in Summer ‘18, into the CLI.

The CLI plugin framework made it really easy to:

  1. Get up and running in no time with the plugin generator.
  2. Get consistent look and feel with the first-party plugins through a good template.
  3. Have a sensible core API with great docs so that I could make requests from an authenticated Salesforce org, without needing to know how that authentication worked.
  4. Present the result to the user through the terminal, and also make it available as JSON output so that other tools can then consume this command.

This was mostly an experiment around how the new Salesforce CLI plugin architecture works, and I’ve come away super impressed! If you’re already an SFDX pro then you can grab the plugin from npm. If you want to have a go at doing this yourself then you should start by visiting Keir Bowden’s blog, which has a great intro to writing your first plugin!

Just be warned this is all a fast-moving area and things are going to change... and break!

If you’re new to the world of Salesforce DX and you want to get started with a friendly user interface that wraps up this sort of functionality then you can get started with your free 30-day trial of Gearset from https://gearset.com.

Ready to get started with Gearset?