sfdx-hardis
Version:
Swiss-army-knife Toolbox for Salesforce. Allows you to define a complete CD/CD Pipeline. Orchestrate base commands and assist users with interactive wizards
96 lines (89 loc) • 4.86 kB
JavaScript
/* jscpd:ignore-start */
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages } from '@salesforce/core';
import c from 'chalk';
import fs from 'fs-extra';
import { glob } from 'glob';
import * as path from 'path';
import { uxLog } from '../../../../common/utils/index.js';
import { GLOB_IGNORE_PATTERNS } from '../../../../common/utils/projectUtils.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('sfdx-hardis', 'org');
export default class CleanHiddenItems extends SfCommand {
static title = 'Clean retrieved hidden items in dx sources';
static description = `
## Command Behavior
**Removes hidden or temporary metadata items from your Salesforce DX project sources.**
This command helps clean up your local Salesforce project by deleting files that are marked as hidden or are temporary artifacts. These files can sometimes be generated by Salesforce CLI or other tools and are not intended to be part of your version-controlled source.
Key functionalities:
- **Targeted File Scan:** Scans for files with specific extensions (\`.app\`, \`.cmp\`, \`.evt\`, \`.tokens\`, \`.html\`, \`.css\`, \`.js\`, \`.xml\`) within the specified root folder (defaults to \`force-app\`).
- **Hidden Content Detection:** Identifies files whose content starts with (hidden). This is a convention used by some Salesforce tools to mark temporary or internal files.
- **Component Folder Removal:** If a hidden file is part of a Lightning Web Component (LWC) or Aura component folder, the entire component folder is removed to ensure a complete cleanup.
<details markdown="1">
<summary>Technical explanations</summary>
The command's technical implementation involves:
- **File Discovery:** Uses \`glob\` to find files matching the specified patterns within the \`folder\`.
- **Content Reading:** Reads the content of each file.
- **Hidden Marker Check:** Checks if the file content starts with the literal string (hidden).
- **Folder or File Removal:** If a file is identified as hidden:
- If it's within an lwc or aura component folder, the entire component folder is removed using \`fs.remove\`.
- Otherwise, only the individual file is removed.
- **Logging:** Provides clear messages about which items are being removed and a summary of the total number of hidden items cleaned.
</details>
`;
static examples = ['$ sf hardis:project:clean:hiddenitems'];
static flags = {
folder: Flags.string({
char: 'f',
default: 'force-app',
description: 'Root folder',
}),
debug: Flags.boolean({
char: 'd',
default: false,
description: messages.getMessage('debugMode'),
}),
websocket: Flags.string({
description: messages.getMessage('websocket'),
}),
skipauth: Flags.boolean({
description: 'Skip authentication check when a default username is required',
}),
};
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
static requiresProject = true;
folder;
debugMode = false;
async run() {
const { flags } = await this.parse(CleanHiddenItems);
this.folder = flags.folder || './force-app';
this.debugMode = flags.debug || false;
// Delete standard files when necessary
uxLog("action", this, c.cyan(`Removing hidden dx managed source files`));
/* jscpd:ignore-end */
const rootFolder = path.resolve(this.folder);
const findManagedPattern = rootFolder + `/**/*.{app,cmp,evt,tokens,html,css,js,xml}`;
const matchingCustomFiles = await glob(findManagedPattern, { cwd: process.cwd(), ignore: GLOB_IGNORE_PATTERNS });
let counter = 0;
for (const matchingCustomFile of matchingCustomFiles) {
if (!fs.existsSync(matchingCustomFile)) {
continue;
}
const fileContent = await fs.readFile(matchingCustomFile, 'utf8');
if (fileContent.startsWith('(hidden)')) {
const componentFolder = path.dirname(matchingCustomFile);
const folderSplit = componentFolder.split(path.sep);
const toRemove = folderSplit.includes('lwc') || folderSplit.includes('aura') ? componentFolder : matchingCustomFile;
await fs.remove(toRemove);
uxLog("action", this, c.cyan(`Removed hidden item ${c.yellow(toRemove)}`));
counter++;
}
}
// Summary
const msg = `Removed ${c.green(c.bold(counter))} hidden source items`;
uxLog("action", this, c.cyan(msg));
// Return an object to be displayed with --json
return { outputString: msg };
}
}
//# sourceMappingURL=hiddenitems.js.map