npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

sf-code-scanner

v1.0.5

Published

Salesforce Code Scanner CLI for Apex and LWC best practices

Downloads

223

Readme

Salesforce Code Scanner CLI

A powerful Node.js CLI tool for scanning Salesforce Apex and LWC code for best practices and violations using PMD and custom rules.

Pre-requisites

1. Java

Install Java using Homebrew (macOS):

brew install openjdk@17

Check java path:


which java
/opt/homebrew/opt/openjdk@17/bin/java

2. PMD

Download PMD from: https://pmd.github.io/

For macOS:

cd $HOME
curl -OL https://github.com/pmd/pmd/releases/download/pmd_releases%2F7.18.0/pmd-dist-7.18.0-bin.zip
unzip pmd-dist-7.18.0-bin.zip
alias pmd="$HOME/pmd-bin-7.18.0/bin/pmd"
pmd check -d /usr/src -R rulesets/java/quickstart.xml -f text

Features

  • 🔍 Apex Code Analysis - Static code analysis using PMD
  • LWC Best Practices - Custom checks for Lightning Web Components
  • 📊 Beautiful HTML Reports - Interactive reports with dark/light theme
  • 📈 Data Visualization - Charts showing violations by priority and type
  • 🔄 Export to CSV - Download violation data for further analysis
  • 🎨 Modern UI - DataTables with search, sorting, and pagination
  • 📥 Download Charts - Export charts as PNG images

Prerequisites

  • Node.js >= 14.0.0

  • Salesforce CLI (sf) installed and authenticated

  • PMD (Download from https://pmd.github.io/)

  • Active Salesforce org connection

  • Demo-1

  • Demo-2

Installation

npm install -g sf-code-scanner

Usage

sf-scan -u <username> -p <pmd-path> -s <scan-types> [options]

Options

| Option | Alias | Required | Description | | ------------ | ----- | -------- | ------------------------------------------------------- | | --username | -u | Yes | Salesforce org username or alias | | --pmd-path | -p | Yes | Path to PMD binary directory | | --scan | -s | Yes | Types to scan: Apex, LWC, or both (comma-separated) | | --ruleset | -r | For Apex | Path to PMD ruleset file | | --output | -o | No | Output directory for reports (default: ./reports) |

Examples

Scan Apex code only:

sf-scan \
  -u [email protected] \
  -p /usr/local/pmd/bin \
  -s Apex \
  -r ./rulesets/apex-ruleset.xml

Scan LWC code only:

sf-scan \
  -u [email protected] \
  -p /usr/local/pmd/bin \
  -s LWC

Scan both Apex and LWC:

sf-scan \
  -u [email protected] \
  -p /usr/local/pmd/bin \
  -s Apex,LWC \
  -r ./rulesets/apex-ruleset.xml \
  -o ./my-reports

PMD Ruleset Configuration

Create a PMD ruleset XML file for Apex scanning. Example apex-ruleset.xml: Download it from here:

apex-ruleset.xml

<?xml version="1.0"?>
<ruleset name="Salesforce Apex Ruleset"
    xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0
    https://pmd.sourceforge.io/ruleset_2_0_0.xsd">

    <description>Custom PMD Ruleset for Salesforce Apex</description>

    <rule ref="category/apex/bestpractices.xml">
        <priority>3</priority>
    </rule>

    <rule ref="category/apex/codestyle.xml">
        <priority>4</priority>
    </rule>

    <rule ref="category/apex/design.xml">
        <priority>3</priority>
    </rule>

    <rule ref="category/apex/errorprone.xml">
        <priority>2</priority>
    </rule>

    <rule ref="category/apex/performance.xml">
        <priority>2</priority>
    </rule>

    <rule ref="category/apex/security.xml">
        <priority>1</priority>
    </rule>

</ruleset>

LWC Best Practices Checks

The tool automatically checks for:

  • ✅ Missing @api decorators on public properties
  • ✅ Deprecated @track usage
  • ✅ Direct DOM manipulation (document/window access)
  • ✅ Improper event handling

Report Features

The generated HTML report includes:

Summary Dashboard

  • Total violations count
  • Apex violations count
  • LWC violations count
  • Critical issues (Priority 1) count

Interactive Charts

  • Violations by Priority
  • Violations by Type
  • Download charts as PNG

Data Table

  • Search
  • Sort
  • Pagination
  • Export to CSV

Output Structure

reports/
├── scan-report-1234567890123.html
├── scan-report-1234567890456.html
└── ...

Troubleshooting

"PMD executable not found"

Ensure the PMD path points to the bin directory containing the pmd executable.

"Failed to fetch Salesforce credentials"

Authenticate using:

sf force auth web login -r <instance_url>
sf org display -o myorg --json

"No Apex classes found"

Verify org has Apex classes and user permissions.

"PMD scan failed"

Check ruleset XML validity and PMD compatibility.

Performance Tips

  • Use specific rulesets for faster scans
  • Run scans during off-peak hours
  • Use org aliases for quicker authentication

Happy Scanning! 🚀