Tweet |
Introduction
In Part 1 of this series, I explained how to generate a plugin and clone the example command. In Part 2 I covered finding and loading the package.json manifest and my custom configuration file
In this instalment, I'll show how to load and process source format metadata files - a key requirement when documenting from the metadata source, I'm sure you'll agree.
Walking Directories
In order to process the metadata files, I have to be able to find and iterate them. As I'm processing object files, I also need to process the fields subdirectory that contains the metadata for the custom fields that I'm including in my document. I'll use a number of standard functions provided by Node in order to achieve this.
I know the top level folder name for the source format data, as it is the parameter of the -s (--source-dir) switch passed to my plugin command. To simplify the sample code, I'm pretending I know that the custom objects are in the objects subfolder, which isn't a bad guess, but in the actual code this information is pulled from the configuration file. To generate the combined full pathname, I use the standard path.join function, as this figures out which operating system I'm running on and uses the correct separator:
import { join } from 'path'; ... let objSourceDir=join(sourceDir, 'objects');
I then check that this path exists and is indeed a directory - this time while I use the standard fs.lstatSync to check the path is a directory, I use a homegrown function to check that the path exists:
Once I know the path is there and is a directory, I can read the contents, again using a standard function - fs.readdirSync - and iterate them :
import { lstatSync} from 'fs'; import { fileExists } from '../../shared/files'; ... if ( (fileExists(objSourceDir)) && (lstatSync(objSourceDir).isDirectory()) ) {
Once I know the path is there and is a directory, I can read the contents, again using a standard function - fs.readdirSync - and iterate them :
let entries=readdirSync(objSourceDir); import { appendFileSync, mkdirSync, readdirSync } from 'fs'; ... for (let idx=0, len=entries.length; idx<len; idx++) { let entry=entries[idx]; let entryPath=join(objSourceDir, entries[idx]); }
Reading Metadata
The Salesforce metadata is stored as XML format files, which presents a slight challenge when working in JavaScript. I can use the DOM parser, but I find the code that I end up writing looks quite ugly, with lots of chained functions. My preferred solution is to parse the XML into a JavaScript object using a third party tool - there are a few of these around, but my current favourite is fast-xml-parser because- it's fast (clue is in the name)
- it's synchronous - this isn't a huge deal when writing a CLI plugin, as the commands are async and can thus await asynchronous functions, but when I'm working with the command line I tend towards synchronous
- it's popular - 140k downloads this week
- it's MIT licensed
It's also incredibly easy to use. After installing by running npm install --save, I just need to import the parse function, load the XML format file into a string and parse it!
import { parse} from 'fast-xml-parser'; let objFileBody=readFileSync( join(entryPath, entry + '.object-meta.xml'), 'utf-8'); let customObject=parse(objFileBody);
My customObject variable now contains a JavaScript object representation of the XML file, so I can access the XML elements as properties:
Which allows me to extract the various properties that I am interested in for my document.
In the next exciting episode I'll show how to enrich the objects by pulling in additional information from other files.
let label=customObject.CustomObject.label; let description==customObject.CustomObject.description;
Which allows me to extract the various properties that I am interested in for my document.
In the next exciting episode I'll show how to enrich the objects by pulling in additional information from other files.
Related Posts
- Documenting from the metadata source with a Salesforce CLI Plug-In - Part 2
- 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