- Description
- Usage
- Salesforce CLI Plugin Development
- Plugin Generator Development
- Related Docs and Repositories
This is the generator plugin for building plugins for the Salesforce CLI. The generated sfdx plugin and command are built on top of the oclif cli framework.
As a beta feature, Salesforce Plugin Generator is a preview and isn’t part of the “Services” under your master subscription agreement with Salesforce. Use this feature at your sole discretion, and make your purchase decisions only on the basis of generally available products and features. Salesforce doesn’t guarantee general availability of this feature within any particular time frame or at all, and we can discontinue it at any time. This feature is for evaluation purposes only, not for production use. It’s offered as is and isn’t supported, and Salesforce has no liability for any harm or damage arising out of or in connection with it. All restrictions, Salesforce reservation of rights, obligations concerning the Services, and terms for related Non-Salesforce Applications and Content apply equally to your use of this feature. You can provide feedback and suggestions for Salesforce Plugin Generator in the issues section of this repo.
Starting with Salesforce CLI version 6.7.1, the plugin generator is offered as a core plugin and can be used out of the box. To check your Salesforce CLI version:
$ sfdx --version
Create and configure your own plugin for the Salesforce CLI.
-
Run
sfdx plugins:generate yourPluginName
.Unless you include the
--defaults
flag, you are prompted for information that's used to populate your new plugin. Answer the questions, or press Enter to use the default values.$ sfdx plugins:generate yourPluginName ? npm package name (yourPluginName) ... Created yourPluginName in $HOME/<path-to-current-working-directory>/yourPluginName:
The generator scaffolds a new sfdx plugin and installs the plugin's npm package dependencies.
-
Change directories into the newly created plugin directory.
$ cd yourPluginName
The new plugin contains an example
hello:org
command. You can find the code for that command atyourPluginName/src/commands/hello/org.ts
. -
Run your
hello:org
command.This can be done in one of two ways:
-
Link your new plugin to the Salesforce CLI. This installs the plugin in the Salesforce CLI by creating a symlink to the
yourPluginName
directory.$ sfdx plugins:link
With the plugin linked, you can see command details by adding the
-h
|--help
flag.$ sfdx hello:org --help USAGE $ sfdx hello:org [FILE] OPTIONS -f, --force -n, --name=name name to print -u, --targetusername=targetusername username or alias for the target org; overrides default target org -v, --targetdevhubusername=targetdevhubusername username or alias for the dev hub org; overrides default dev hub org --apiversion=apiversion override the api version used for api requests made by this command --json format output as json --loglevel=(trace|debug|info|warn|error|fatal) logging level for this command invocation EXAMPLES $ sfdx hello:org --targetusername [email protected] --targetdevhubusername [email protected] Hello world! This is org: MyOrg and I will be around until Tue Mar 20 2018! My hub org id is: 00Dxx000000001234 $ sfdx hello:org --name myname --targetusername [email protected] Hello myname! This is org: MyOrg and I will be around until Tue Mar 20 2018!
-
Alternatively, you can run the
hello:org
command without linking it to the Salesforce CLI by using the providedbin/run
script.$ bin/run hello:org --help USAGE $ sfdx hello:org [FILE] OPTIONS -f, --force -n, --name=name name to print -u, --targetusername=targetusername username or alias for the target org; overrides default target org -v, --targetdevhubusername=targetdevhubusername username or alias for the dev hub org; overrides default dev hub org --apiversion=apiversion override the api version used for api requests made by this command --json format output as json --loglevel=(trace|debug|info|warn|error|fatal) logging level for this command invocation EXAMPLES $ sfdx hello:org --targetusername [email protected] --targetdevhubusername [email protected] Hello world! This is org: MyOrg and I will be around until Tue Mar 20 2018! My hub org id is: 00Dxx000000001234 $ sfdx hello:org --name myname --targetusername [email protected] Hello myname! This is org: MyOrg and I will be around until Tue Mar 20 2018!
-
Now you are ready to develop your own commands!
The generated hello:org
command extends sfdx-command, which in turn extends oclif/command. When you build your own commands, you extend sfdx-command
too.
sfdx-command
comes packed with functionality to speed up your command development and interact more easily with Salesforce DX projects and Salesforce orgs. sfdx-command
uses features of sfdx-core, which exposes Salesforce API functionality that's useful in command development. See these libraries' documentation pages for a full list of features.
The hello:org
command highlights a small subset of the features available through sfdx-command, sfdx-core, and oclif. You can find the code for the hello:org
command at yourPluginName/src/commands/hello/org.ts
.
Add standard and custom parameters to your commands.
sfdx-command
automatically enables the --json
and --loglevel
flags on every command to make continuous integration setup and debugging easier.
sfdx-command
also includes functionality to help you set up connections with Salesforce orgs. For example, to enable the --targetusername
parameter for your command:
protected static requiresUsername = true;
Now, the command can access the org that has a specified target username.
public async run(): Promise<any> {
const org = this.org;
}
TODO: link to sfdx-command flag docs.
In addition to the sfdx-command
parameters, you can specify your own custom parameters by setting the flagsConfig
variable.
protected static flagsConfig = {
// flag with a value (-n, --name=VALUE)
name: flags.string({char: 'n', description: messages.getMessage('nameFlagDescription')}),
force: flags.boolean({char: 'f'})
};
TODO: link to custom flag configuration documentation.
The [sfdx-core](TODO: add link to sfdx-core messaging docs) APIs provide a framework for handling command messaging.
core.Messages.importMessagesDirectory(pathToPluginRootDirectory);
const messages = core.Messages.loadMessages('yourPluginName', 'org');
As you add more commands to your CLI plugin, it can also be useful to nest your commands within topics. In the case of the generated plugin, hello
is the topic and org
is the command name. This structure was created by placing the org.ts
file in the hello
subdirectory.
package.json
src/
└── commands/
└── hello/
└── org.ts
See oclif/oclif#-topics for more information on how to structure your directories to utilize topics.
Because this is a TypeScript project, you need to compile the changes you make to your commands before running the commands.
-
If you linked your plugin to the Salesforce CLI, to compile your code and update the
.oclif.manifest.json
file that is consumed by the Salesforce CLI, run:yarn run prepare
-
If you use the
bin/run
script to run your commands, to compile your changes, run:yarn run build
We recommend using the Visual Studio Code (VS Code) IDE for your plugin development. Included in the .vscode
directory of the generated plugin is a launch.json
config file, which allows you to attach a debugger to the node process when running your commands.
To debug the hello:org
command from the myNewPlugin
directory:
-
Start the inspector
-
If you linked your plugin to the sfdx cli, call your command with the
dev-suspend
switch:$ sfdx hello:org -u [email protected] --dev-suspend
-
Alternatively, to call your command using the
bin/run
script, set theNODE_OPTIONS
environment variable to--inspect-brk
when starting the debugger:$ NODE_OPTIONS=--inspect-brk bin/run hello:org -u [email protected]
-
-
Set some breakpoints in your command code
-
Click on the Debug icon in the Activity Bar on the side of VS Code to open up the Debug view.
-
In the upper left hand corner of VS Code, verify that the "Attach to Remote" launch configuration has been chosen.
-
Hit the green play button to the left of the "Attach to Remote" launch configuration window. The debugger should now be suspended on the first line of the program.
-
Hit the green play button at the top middle of VS Code (this play button will be to the right of the play button that you clicked in step #5).
Congrats, you are now debugging!
Note: Only Node 8+ is supported. If you are new to Node.js, use nvm to install node.
- Start by cloning the repo.
$ git clone TODO:update-with-repo
- Change directories into the cloned repo.
$ cd TODO:update-with-repo-name
- If you don't have Node.js version 8 or above installed, install it now.
$ nvm install v8.9.4
- Install the Yarn package manager.
$ npm install -g yarn
- Install the plugin generator.
$ yarn install
- Compile the TypeScript code.
$ yarn run build
- Now you are ready to run the plugins:generate command and make any changes to the generator.
$ bin/run plugins:generate yourPluginName
- salesforcedx/sfdx-command - Base Salesforce CLI command
- salesforcedx/sfdx-core - Helper API for working with a Salesforce DX project and managing Salesforce orgs
- @oclif/command - Base command for oclif; this can be used directly without the generator
- @oclif/config - Most of the core setup for oclif lives here
- @oclif/errors - Renders and logs errors from commands
- @oclif/cli-ux - Library for common CLI UI utilities
- @oclif/test - Test helper for oclif