From 4eb1142a0dd810243df4cea5701f9a31e203dcdb Mon Sep 17 00:00:00 2001
From: junners <jose.junryan@gmail.com>
Date: Mon, 27 Jan 2025 14:54:35 -0800
Subject: [PATCH] fix: add backward compatibility and warning for sourcepath
 flag

---
 package-lock.json         | 36 ++++++++++++++++++++++++++++++++++++
 package.json              |  1 +
 src/commands/flow/scan.ts | 34 ++++++++++++++++++++++++++++------
 3 files changed, 65 insertions(+), 6 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index f96b088..17c2aaa 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -14,6 +14,7 @@
         "@salesforce/sf-plugins-core": "^12.1.3",
         "chalk": "^5.4.1",
         "cosmiconfig": "^9.0.0",
+        "fs-extra": "^11.3.0",
         "glob": "^11.0.1",
         "lightning-flow-scanner-core": "4.15.0"
       },
@@ -7300,6 +7301,41 @@
       ],
       "license": "MIT"
     },
+    "node_modules/fs-extra": {
+      "version": "11.3.0",
+      "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-11.3.0.tgz",
+      "integrity": "sha512-Z4XaCL6dUDHfP/jT25jJKMmtxvuwbkrD1vNSMFlo9lNLY2c5FHYSQgHPRZUjAB26TpDEoW9HCOgplrdbaPV/ew==",
+      "license": "MIT",
+      "dependencies": {
+        "graceful-fs": "^4.2.0",
+        "jsonfile": "^6.0.1",
+        "universalify": "^2.0.0"
+      },
+      "engines": {
+        "node": ">=14.14"
+      }
+    },
+    "node_modules/fs-extra/node_modules/jsonfile": {
+      "version": "6.1.0",
+      "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz",
+      "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==",
+      "license": "MIT",
+      "dependencies": {
+        "universalify": "^2.0.0"
+      },
+      "optionalDependencies": {
+        "graceful-fs": "^4.1.6"
+      }
+    },
+    "node_modules/fs-extra/node_modules/universalify": {
+      "version": "2.0.1",
+      "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.1.tgz",
+      "integrity": "sha512-gptHNQghINnc/vTGIk0SOFGFNXw7JVrlRUtConJRlvaw6DuX0wO5Jeko9sWrMBhh+PsYAZ7oXAiOnf/UKogyiw==",
+      "license": "MIT",
+      "engines": {
+        "node": ">= 10.0.0"
+      }
+    },
     "node_modules/fs.realpath": {
       "version": "1.0.0",
       "dev": true,
diff --git a/package.json b/package.json
index 99cbb83..6027be9 100644
--- a/package.json
+++ b/package.json
@@ -8,6 +8,7 @@
     "@salesforce/sf-plugins-core": "^12.1.3",
     "chalk": "^5.4.1",
     "cosmiconfig": "^9.0.0",
+    "fs-extra": "^11.3.0",
     "glob": "^11.0.1",
     "lightning-flow-scanner-core": "4.15.0"
   },
diff --git a/src/commands/flow/scan.ts b/src/commands/flow/scan.ts
index 15493a9..14c1872 100644
--- a/src/commands/flow/scan.ts
+++ b/src/commands/flow/scan.ts
@@ -1,6 +1,7 @@
 import { SfCommand, Flags } from "@salesforce/sf-plugins-core";
 import { Messages, SfError } from "@salesforce/core";
 import chalk from "chalk";
+import * as fse from "fs-extra";
 import { exec } from "child_process";
 
 import { loadScannerOptions } from "../../libs/ScannerConfig.js";
@@ -27,7 +28,8 @@ export default class Scan extends SfCommand<ScanResult> {
     "sf flow scan -c path/to/config.json",
     "sf flow scan -c path/to/config.json --failon warning",
     "sf flow scan -d path/to/flows/directory",
-    "sf flow scan -p path/to/single/file.flow-meta.xml",
+    "sf flow scan -p path/to/single/file.flow-meta.xml,path/to/another/file.flow-meta.xml",
+    "sf flow scan --files path/to/single/file.flow-meta.xml path/to/another/file.flow-meta.xml",
   ];
 
   protected static requiresUsername = false;
@@ -63,13 +65,16 @@ export default class Scan extends SfCommand<ScanResult> {
       description: "Force retrieve Flows from org at the start of the command",
       default: false,
     }),
-    sourcepath: Flags.file({
+    sourcepath: Flags.directory({
       char: "p",
       description: "Comma-separated list of source flow paths to scan",
       required: false,
-      aliases: ["files"],
+      multiple: false,
+    }),
+    files: Flags.file({
       multiple: true,
       exists: true,
+      description: "List of source flows paths to scan",
     }),
     targetusername: Flags.string({
       char: "u",
@@ -88,7 +93,10 @@ export default class Scan extends SfCommand<ScanResult> {
     if (flags.targetusername) {
       await this.retrieveFlowsFromOrg(flags.targetusername);
     }
-    const flowFiles = this.findFlows(flags.directory, flags.sourcepath);
+
+    const targets: string | string[] = flags.sourcepath ?? flags.files;
+
+    const flowFiles = this.findFlows(flags.directory, targets);
     this.spinner.start(`Identified ${flowFiles.length} flows to scan`);
     // to
     // core.Flow
@@ -186,7 +194,7 @@ export default class Scan extends SfCommand<ScanResult> {
     return { summary, status: status, results };
   }
 
-  private findFlows(directory: string, sourcepath: string[]) {
+  private findFlows(directory: string, sourcepath: string | string[]) {
     // List flows that will be scanned
     let flowFiles;
     if (directory && sourcepath) {
@@ -197,7 +205,21 @@ export default class Scan extends SfCommand<ScanResult> {
     } else if (directory) {
       flowFiles = FindFlows(directory);
     } else if (sourcepath) {
-      flowFiles = sourcepath;
+      if (typeof sourcepath === "string") {
+        this.log(chalk.cyanBright("********"));
+        this.log("");
+        this.log(
+          chalk.yellowBright(
+            "WARN: flag --path or -p will be deprecated, please use --files to scan flow source files",
+          ),
+        );
+        this.log("");
+        this.log(chalk.cyanBright("********"));
+        this.log("");
+        flowFiles = sourcepath.split(",").filter((f) => fse.exists(f));
+      } else {
+        flowFiles = sourcepath;
+      }
     } else {
       flowFiles = FindFlows(".");
     }