hardis:project:clean:systemdebug
Description
Command Behavior
Removes or comments out System.debug() statements from Apex classes and triggers in your Salesforce DX project.
This command helps maintain clean and optimized Apex code by eliminating debug statements that are often left in production code. While System.debug() is invaluable during development, it can impact performance and expose sensitive information if left in deployed code.
Key functionalities:
- Targeted File Scan: Scans all Apex class (.cls) and trigger (.trigger) files within the specified root folder (defaults to
force-app). - Conditional Action:
- Comment Out (default): By default, it comments out
System.debug()lines by prepending // to them. - Delete (
--deleteflag): If the--deleteflag is used, it completely removes the lines containingSystem.debug().
- Comment Out (default): By default, it comments out
- Exclusion: Lines containing
NOPMDare ignored, allowing developers to intentionally keep specific debug statements.
Technical explanations
The command's technical implementation involves:
- File Discovery: Uses
globto find all Apex class and trigger files. - Content Reading: Reads the content of each Apex file line by line.
- Pattern Matching: Checks each line for the presence of
System.debug(case-insensitive). - Line Modification:
- If
System.debugis found and the--deleteflag is not used, it modifies the line to comment out the debug statement. - If
System.debugis found and the--deleteflag is used, it removes the line entirely.
- If
- File Writing: If any changes are made to a file, the modified content is written back to the file using
fs.writeFile. - Logging: Provides a summary of how many files were cleaned.
Parameters
| Name | Type | Description | Default | Required | Options |
|---|---|---|---|---|---|
| delete -d |
boolean | Delete lines with System.debug | |||
| flags-dir | option | undefined | |||
| folder -f |
option | Root folder | force-app | ||
| json | boolean | Format output as json. | |||
| skipauth | boolean | Skip authentication check when a default username is required | |||
| websocket | option | Websocket host:port for VsCode SFDX Hardis UI integration |
Examples
$ sf hardis:project:clean:systemdebug