Tweet |
Introduction
In Part 1 of this series, I explained how to generate a plugin and clone the example command. In this instalment I'll look at customising the cloned command to locate and load the package.json manifest, and to load a configuration file that defines how my objects should be divided up for documentation purposes. All loading, all the time.
Customising the Command
Renaming
The first thing to do around customising the command is to rename it - as I cloned the sample org command, although it is externally known as bbdoc:doc due to the folder structure, in the source code it is still Org:export default class Org extends SfdxCommand {
so I change the class name to Doc.
Flags
The next thing is to change the flags supported by the command to those that I need for documentation purposes. The flags for the org command are defined as follows:protected static flagsConfig = { // flag with a value (-n, --name=VALUE) name: flags.string({char: 'n', description: messages.getMessage('nameFlagDescription')}), force: flags.boolean({char: 'f', description: messages.getMessage('forceFlagDescription')}) };
This shows up that there is slightly more to this than changing the flag names - notice the description properties aren't hardcoded strings. Instead they are retrieved by a message object. Here's the setup for the messages object:
// Initialize Messages with the current plugin directory Messages.importMessagesDirectory(__dirname); // Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core, // or any library that is using the messages framework can also be loaded this way. const messages = Messages.loadMessages('sample', 'org');
The final line shows that the messages are loaded based on the command name - org. In the generated plugin, messages are stored in the messages folder and the command specific messages are stored in a file named <command>.json. So for the org command, we have the following file structure:
and org.json has the following contents:
{ "commandDescription": "print a greeting and your org IDs", "nameFlagDescription": "name to print", "forceFlagDescription": "example boolean flag", "errorNoOrgResults": "No results found for the org '%s'." }
so as part of defining my flags, I also need to define their descriptions and the description of the command itself as follows:
- config - configuration file
- report-dir - directory to store the generated report
- source-dir - source directory containing the org metadata
so I create a doc.json file:
{ "commandDescription": "generate documentation for an org", "configFlagDescription": "configuration file", "reportDirFlagDescription": "directory to store the generated report", "sourceDirFlagDescription": "source directory containing the org metadata" }
I initialise the messages with this new file:
const messages = Messages.loadMessages('sample', 'doc');
and update the flags definition in the source code to :
protected static flagsConfig = { // flag with a value (-n, --name=VALUE) "config": flags.string({char: 'c', description: messages.getMessage('configFlagDescription')}), "report-dir": flags.string({char: 'r', required: true, description: messages.getMessage('reportDirFlagDescription')}), "source-dir": flags.string({char: 's', required: true, description: messages.getMessage('sourceDirFlagDescription')}) };
Reading the package.json Manifest
The version number of my plugin is stored in the package.json manifest file and I want to include this in the generated HTML, so it's time to locate and read that file. I know the package.json is at the root of my plugin directory, but I don't know where the plugin has been installed. Node has a standard File System module, fs, that has been extended by Salesforce to add a handy utility function - traverseForFile. This starts from the folder the command lives in and makes its way up the folder hierarchy until it finds he requested file, or hits the root directory for the disk. I execute this to find the location of the package.json file, which is in the plugin root:
const pluginRoot=await fs.traverseForFile(__dirname, 'package.json');
Having found the directory, I then read the package.json file. Note that I'm using the join function from the standard Path module - this allows me to build a path without worrying about the operating system or path separator. Note also that as reading a file is an asynchronous operation, I use the await keyword to stop the plugin until the read is complete.
// get the version const packageJSON=await fs.readFile(join(pluginRoot, 'package.json'), 'utf-8');
Having read the JSON from the file, I parse it into a JavaScript object using the standard JSON.parse function and extract the version property:
const pkg=JSON.parse(packageJSON); let version=pkg.version;
Loading the Configuration
I find I'm using configuration files more and more often with plugins. While I could add my configuration to sfdx-project.json, and I do if it is only a few items, when I start getting into lengthy configuration it feels like I'm polluting that file with things that don't belong there.For this plugin and the example Salesforce metadata, I have the following configuration in bbdoc-config.json:
{ "objects": { "name": "objects", "description": "Custom Objects", "subdirectory": "objects", "suffix":"object", "groups": { "events": { "name":"events", "title":"Event Objects", "description": "Objects associated with events", "objects": "Book_Signing__c, Author_Reading__c" } , "other": { "name":"other", "title":"Uncategorised Objects", "description": "Objects that do not fall into any other category" } } } }
The objects property contains the configuration for generating the HTML document from the object metadata. It's in its own property as I intend to add more capability in the future. There's some information about the metadata, where it can be found, the suffix for the metadata files, and then a couple of groupings for the objects - the event specific objects and the rest.
The configuration file is optional - if one isn't provided there will be a single object grouping of uncategorised, so I check if he flag was supplied before trying to load any file.
// load the config, using the default if nothing provided via flags let config; if (!this.flags.config) { this.ux.log('Using default configuration'); config=defaultConfig; } else { config=await fs.readJson(this.flags.config); }
This time I'm using another Salesforce extension to the fs module - readJSON - which reads and parses the file into an object in one go - I should probably refactor the loading of the package.json file to use this!
In the next instalment, I'll show how to load and process the metadata source files. As before, if you can't wait, you can see the plug-in source on Github or install it yourself from npm.
RELATED POSTS
- Documenting from the metadata source with a Salesforce CLI Plug-In - Part 1
- Parallel Apex Unit Tests and Salesforce CLI Plug-Ins
- Going GUI over the Salesforce CLI Part 2
- Going GUI over the Salesforce CLI
- Mentz - the Mentee Workflow
- Offline mobile app template plug-in Dreamforce 18 session
- Salesforce CLI Play-by-Play
- Salesforce CLI Cheat Sheet
- SFDX and the Metadata API Part 4 - VSCode Integration
- SFDX and the Metadata API Part 3 - Destructive Changes
- SFDX and the Metadata API Part 2 - Scripting
- SFDX and the Metadata API
No comments:
Post a Comment