Security News
Supply Chain Attack Detected in @solana/web3.js Library
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
@salesforce/command
Advanced tools
@salesforce/command is an npm package that provides a framework for building CLI commands for Salesforce. It is designed to help developers create commands that interact with Salesforce APIs and services, making it easier to automate tasks and integrate Salesforce with other systems.
Command Creation
This feature allows developers to create custom CLI commands by extending the SfdxCommand class. The example demonstrates a simple command that logs a message to the console.
const { SfdxCommand } = require('@salesforce/command');
class MyCommand extends SfdxCommand {
async run() {
this.ux.log('Hello, Salesforce!');
}
}
module.exports = MyCommand;
Parameter Handling
This feature provides a way to define and handle command-line parameters using the flags property. The example shows how to define a 'name' parameter and use it within the command.
const { flags, SfdxCommand } = require('@salesforce/command');
class MyCommand extends SfdxCommand {
static flagsConfig = {
name: flags.string({ char: 'n', description: 'name to print' })
};
async run() {
const name = this.flags.name || 'world';
this.ux.log(`Hello, ${name}!`);
}
}
module.exports = MyCommand;
Salesforce Authentication
This feature allows commands to authenticate and interact with Salesforce orgs. The example demonstrates how to create a connection and query Salesforce data.
const { SfdxCommand } = require('@salesforce/command');
const { Connection } = require('@salesforce/core');
class MyCommand extends SfdxCommand {
async run() {
const conn = await Connection.create({ authInfo: this.org.getConnection().getAuthInfo() });
const result = await conn.query('SELECT Id, Name FROM Account');
this.ux.logJson(result.records);
}
}
module.exports = MyCommand;
oclif is a framework for building command-line interfaces in Node.js. It is highly extensible and supports plugins, making it suitable for creating complex CLI applications. Compared to @salesforce/command, oclif is more general-purpose and not specifically tailored for Salesforce.
commander is a popular Node.js library for building command-line interfaces. It provides a simple and flexible API for defining commands and options. While it is not specifically designed for Salesforce, it can be used to create CLI tools for various purposes, including Salesforce automation.
yargs is another widely-used library for building command-line tools in Node.js. It offers a rich set of features for parsing arguments and generating help messages. Like commander, yargs is a general-purpose library and can be used to create CLI tools for different use cases, including Salesforce.
This is the base command class that plugin command authors will extend for convenient access to common SFDX flags, a logger, CLI output formatting, scratch orgs, and devhubs. It extends @oclif/command and is available within a plugin generated by the Salesforce Plugin Generator.
As a beta feature, the Salesforce command module 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 the Salesforce command module in the issues section of this repo.
$ sfdx --version
Commands that extend SfdxCommand can only be used with SFDX CLI version 6.8.2 or later.
There are many features that can be enabled with SfdxCommand subclasses simply by setting static properties on the subclassed command. Other features will always be available for your command such as a logger and output renderer.
--json
and --loglevel
flags automatically added to their command. Other SFDX Flags are enabled either by setting static properties directly on the command or within the flagsConfig static property.SfdxError
is not thrown then the error handler wraps it in an SfdxError for consistent error display.true
to add the --targetusername (-u)
and --apiversion
flags to this command, and have the org added (if provided) as an instance property which you can access in your command as this.org
.static supportsUsername = true;
true
to add the --targetusername (-u)
and --apiversion
flags to this command and require that a targetusername is set, either via the flag or by having a default set in your SFDX config.static requiresUsername = true;
true
to add the --targetdevhubusername (-v)
and --apiversion
flags to this command, and have the dev hub org added (if provided) as an instance property which you can access in your command as this.hubOrg
.static supportsDevhubUsername = true;
true
to add the --targetdevhubusername (-v)
and --apiversion
flags to this command and require that a targetdevhubusername is set, either via the flag or by having a default set in your SFDX config.static requiresDevhubUsername = true;
true
if this command must be run within a SFDX project, and have the project object added as an instance property which you can access in your command as this.project
.static requiresProject = true;
static flagsConfig = {
name: flags.string({char: 'n', description: messages.getMessage('nameFlagDescription')}),
force: flags.boolean({char: 'f'})
};
static tableColumnData = ['id', 'name', 'description'];
static result = {
tableColumnData: [
{key: 'id', label: 'ID'},
{key: 'name', label: 'Name'},
{key: 'description', label: 'Description'}
],
display(): {
if (Array.isArray(this.data) && this.data.length) {
if (this.data.length > 100) {
// special display for large number of results
} else {
this.ux.table(this.data, this.tableColumnData);
}
}
}
};
--targetusername (-u)
flag or a default defined in the SFDX config.--targetdevhubusername (-v)
flag or a default defined in the SFDX config.--json
flag so that output is supressed when set.run
method has completed.There are many SFDX flags and flag types made available by extending SfdxCommand. As mentioned above, some of these flags are added when static properties are set on your command such as supportsUsername, some are added necessarily such as --json
and --loglevel
, and some can be added or removed from your command via the flagsConfig static property. You can also override the flags implementation completely by defining your own static flags
property. See the oclif flags docs for details.
this.ux.*
methods and formats output as JSON. A boolean flag that is necessarily on all SfdxCommands.--myflag=first,second,third
--myflag=01-02-2000
--myflag=01/02/2000 01:02:34
--myflag=/my/path/to
--myflag=me@my.org
--myflag=/my/path/to/myfile.json
--myflag=00Dxxxxxxxxxxxx
--myflag=42
--myflag=01:02:03
--myflag=http://www.salesforce.com
static flagsConfig = {
names: {name: 'names', char: 'n', required: true, type: 'array'}, // use an SFDX flag type
force: flags.boolean({char: 'f'}), // use an oclif flag builder
verbose: true, // enable the SFDX verbose flag
apiversion: false, // disable the SFDX apiversion flag
targetusername: {required: true} // override default targetusername behavior by making it required
};
by specifying that your command supports a username your command will automatically have the --targetusername (-u)
and apiversion
flags added. The --json
and --loglevel
flags are also added by default.
SfdxCommand handles runtime errors in the catch
method for consistent error handling format and behavior. By default, the exit code will be 1
unless otherwise specified within the SfdxError. Stack traces are supressed unless the SFDX_ENV
environment variable is set to "development". Override the catch
method if you'd prefer to handle errors within your command.
FAQs
Salesforce CLI base command class
We found that @salesforce/command demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 54 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
A supply chain attack has been detected in versions 1.95.6 and 1.95.7 of the popular @solana/web3.js library.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.