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
94 lines • 4.22 kB
JavaScript
/* jscpd:ignore-start */
import { SfCommand, Flags } from '@salesforce/sf-plugins-core';
import { Messages, SfError } 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 CleanManagedItems extends SfCommand {
static title = 'Clean retrieved managed items in dx sources';
static description = 'Remove unwanted managed items within sfdx project sources';
static examples = ['$ sf hardis:project:clean:manageditems --namespace crta'];
static flags = {
namespace: Flags.string({
char: 'n',
default: '',
description: 'Namespace to remove',
}),
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;
namespace;
folder;
debugMode = false;
async run() {
const { flags } = await this.parse(CleanManagedItems);
this.namespace = flags.namespace || '';
this.folder = flags.folder || './force-app';
this.debugMode = flags.debug || false;
if (this.namespace === '') {
throw new SfError('namespace argument is mandatory');
}
// Delete standard files when necessary
uxLog(this, c.cyan(`Removing unwanted dx managed source files with namespace ${c.bold(this.namespace)}...`));
/* jscpd:ignore-end */
const rootFolder = path.resolve(this.folder);
const findManagedPattern = rootFolder + `/**/${this.namespace}__*`;
const matchingCustomFiles = await glob(findManagedPattern, { cwd: process.cwd(), ignore: GLOB_IGNORE_PATTERNS });
for (const matchingCustomFile of matchingCustomFiles) {
if (!fs.existsSync(matchingCustomFile)) {
continue;
}
// Do not remove managed folders when there are local custom items defined on it
if (fs.lstatSync(matchingCustomFile).isDirectory()) {
const localItems = await this.folderContainsLocalItems(matchingCustomFile);
if (localItems) {
continue;
}
}
// Keep .object-meta.xml item if there are local custom items defined on it
if (matchingCustomFile.endsWith('.object-meta.xml')) {
const localItems = await this.folderContainsLocalItems(path.dirname(matchingCustomFile));
if (localItems) {
continue;
}
}
await fs.remove(matchingCustomFile);
uxLog(this, c.cyan(`Removed managed item ${c.yellow(matchingCustomFile)}`));
}
// Return an object to be displayed with --json
return { outputString: 'Cleaned managed items from sfdx project' };
}
async folderContainsLocalItems(folder) {
// Do not remove managed folders when there are local custom items defined on it
const subFiles = await glob(folder + '/**/*', { cwd: process.cwd(), ignore: GLOB_IGNORE_PATTERNS });
const standardItems = subFiles.filter((file) => {
return !fs.lstatSync(file).isDirectory() && !path.basename(file).startsWith(`${this.namespace}__`);
});
if (standardItems.length > 0) {
return true;
}
return false;
}
}
//# sourceMappingURL=manageditems.js.map