Generate a Basic Plugin
Use the interactive plugin generator, which is itself a plugin, to create your own Salesforce CLI plugin.
-
Open a terminal (macOS, Linux) or command prompt (Windows) and change to the top-level directory where you want to generate your plugin. For example:
-
Run the interactive command:
The Salesforce CLI plugin that contains the commands for generating new plugins, commands, and flags (
plugin-dev
) is "just in time" (JIT). This means that if you haven't explicitly installed the plugin, Salesforce CLI does it for you when you first run one of its commands.You're first prompted whether you're an internal Salesforce developer. The Salesforce CLI team uses this command to create plugins too! But answer
n
so you're not subject to our internal requirements. You're next prompted for information to populate your new plugin, such as its name, description, author, and percentage of code coverage you want. The command then clones either the plugin-template-sf or plugin-template-sf-external Github repo and installs the plugin's npm package dependencies usingyarn install
.After the
dev generate plugin
command completes, the new plugin contains an examplehello world
command. See this section for a description of some of the generated files.
Let's see how to run the commands in your new plugin.
-
Change to the new plugin directory, which is the same as the name you provided to the
dev generate plugin
command. -
To run the commands of your in-development plugin from the top-level directory that your code lives in, precede the commands with
bin/dev.js
. For example, to run the samplehello world
command:To view the
--help
output for the command:As you create new commands, test them the same way. For example:
-
When you’re ready to test drive your plugin, link your in-development plugin to Salesforce CLI. From the top-level plugin directory, such as
my-plugin
, run this command (be sure to include the period at the end!):The command installs your plugin in Salesforce CLI by creating a symlink to your
my-plugin
directory. After you link your plugin, you can run your commands without usingbin/dev.js
. For example:If you see this warning message, don't worry. It's expected and everything is working just fine.
This warning means that your plugin is written with ESM (officially called ECMAScript modules), which is what our templates use. We can't auto compile ESM while running a command, which means you must make sure that you compile your plugin after every change. We recommend running
yarn compile --watch
in a separate terminal so that all your changes are compiled every time you save a file.The
-h
flag, which displays a shorter version of help messages, is available only when you link or install the plugin. It's not available when usingbin/dev.js
.To see which plugins you've installed or linked, including your new plugin, run:
Your linked plugin is listed like this in the output:
To unlink the plugin, run:
The sf
plug-In Generator creates many files, some that support the entire plugin, some for the hello world
command. This table describes a few of the main ones which you can use as templates when you start coding your own commands.
File | Description |
---|---|
package.json | Npm file that describes package dependencies and versions. |
tsconfig.json | Typescript file that specifies the root files and the compiler options required to compile the project. |
src/commands/hello/world.ts | Main TypeScript file that contains the code for the hello world command. The command imports and extends classes from @salesforce/sf-plugins-core. When you add your own commands, you use the SfCommand abstract class. |
messages/hello.world.md | Markdown file that contains the messages that make up the command help and errors. |
test/commands/hello/world.test.ts | Unit tests. |
test/commands/hello/world.nut.ts | Complex integration, smoke, and end-to-end tests. Also known as NUTS (non-unit-tests.) |
.github/workflows/*.yml | Sample GitHub Actions workflow files for testing, releasing, and publishing your plugin. See this repo for how the Salesforce CLI developer team uses GitHub Actions. |