Skip to content

Commit

Permalink
feat: objects and permission sets are in different folders
Browse files Browse the repository at this point in the history
  • Loading branch information
PreziosiRaffaele committed Jun 7, 2024
1 parent bd37c1d commit d41e4cd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 20 deletions.
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# sf-perms

Commands to manage Salesforce permission sets.
The sf perms field new command facilitates the addition of field permissions to permission sets. It guides the user through the process of configuring these permissions.

To specify the directories containing the permissionsets and objects folders, use the --directory-permission-set and --directory-object flags, respectively. If both folders are located within the same directory, you can simplify the command by using the --directory flag to specify the common directory.

If no directory is specified, the default directory ./force-app/main/default will be used.

## Install

Expand All @@ -18,17 +22,26 @@ sf plugins install sf-perms

```
USAGE
$ sf perms field new [--json] [-d <value>]
$ sf perms field new [--json] [-d <value>] [-p <value>] [-f <value>]
FLAGS
-d, --directory=<value> [default: ./force-app/main/default] Default Directory
-d, --directory=<value> The directory that contains the permissionsets/objects folder.
-f, --directory-object=<value> The directory that contains the objects folder.
-p, --directory-permission-set=<value> The directory that contains the permissionsets folder.
GLOBAL FLAGS
--json Format output as json.
DESCRIPTION
Create field permissions for permission sets.
Add field permissions to permission sets.
The sf perms field new command facilitates the addition of field permissions to permission sets. It guides the user through the process of configuring these permissions.
To specify the directories containing the permissionsets and objects folders, use the --directory-permission-set and --directory-object flags, respectively. If both folders are located within the
same directory, you can simplify the command by using the --directory flag to specify the common directory.
If no directory is specified, the default directory ./force-app/main/default will be used.
EXAMPLES
sf perms field new
$ sf perms field new
```
18 changes: 15 additions & 3 deletions messages/perms.field.new.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
# summary

Add field permissions to a permission set.
Add field permissions to permission sets.

# description

Add field permissions to a permission set.
The sf perms field new command facilitates the addition of field permissions to permission sets. It guides the user through the process of configuring these permissions.

To specify the directories containing the permissionsets and objects folders, use the --directory-permission-set and --directory-object flags, respectively. If both folders are located within the same directory, you can simplify the command by using the --directory flag to specify the common directory.

If no directory is specified, the default directory ./force-app/main/default will be used.

# flags.directory.summary

The directory that contains the permissionSet/objects folder.
The directory that contains the permissionsets/objects folder.

# flags[directory-permission-set].summary

The directory that contains the permissionsets folder.

# flags[directory-object].summary

The directory that contains the objects folder.

# examples

Expand Down
55 changes: 43 additions & 12 deletions src/commands/perms/field/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,22 @@ export default class PermsFieldNew extends SfCommand<PermsFieldNewResult> {
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
directory: Flags.directory({
summary: 'Default Directory',
summary: messages.getMessage('flags.directory.summary'),
exists: true,
default: './force-app/main/default',
char: 'd',
}),
'directory-permission-set': Flags.directory({
// eslint-disable-next-line sf-plugin/no-missing-messages
summary: messages.getMessage('flags[directory-permission-set].summary'),
exists: true,
char: 'p',
}),
'directory-object': Flags.directory({
// eslint-disable-next-line sf-plugin/no-missing-messages
summary: messages.getMessage('flags[directory-object].summary'),
exists: true,
char: 'f',
}),
};

public static fs = fsPromises;
Expand All @@ -38,19 +49,35 @@ export default class PermsFieldNew extends SfCommand<PermsFieldNewResult> {
};

const { flags } = await this.parse(PermsFieldNew);

const directoryPath: string = path.resolve(flags.directory);

try {
await this.checkDirectory(directoryPath);
const { permissionSetsSelected, objectSelected } = await this.selectPermissionSetsAndObject(directoryPath);
const selectedFields = await this.selectFields(objectSelected, directoryPath);
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
if (flags.directory && (flags['directory-permission-set'] || flags['directory-object'])) {
throw new Error('You cannot provide Default Directory with Permission Set Directory or Object Directory');
}
if (!flags.directory && (!flags['directory-permission-set'] || !flags['directory-object'])) {
throw new Error('You should provide both permission set directory and object directory');
}
if (!flags.directory) {
flags.directory = './force-app/main/default';
}
const directoryPermissionSets = flags['directory-permission-set']
? path.resolve(flags['directory-permission-set'])
: path.resolve(flags.directory);
const directoryObjects = flags['directory-object']
? path.resolve(flags['directory-object'])
: path.resolve(flags.directory);
await Promise.all([this.checkDirectory(directoryPermissionSets), this.checkDirectory(directoryObjects)]);
const { permissionSetsSelected, objectSelected } = await this.selectPermissionSetsAndObject(
directoryPermissionSets,
directoryObjects
);
const selectedFields = await this.selectFields(objectSelected, directoryObjects);
const fieldsPermissionSelected = await this.selectFieldsPermissions(selectedFields);
const permissionSetUpdater = new PermissionSetUpdater(PermsFieldNew.fs);
await Promise.all(
permissionSetsSelected.map((permissionSet) =>
permissionSetUpdater.updatePermissionSet(
directoryPath,
directoryPermissionSets,
permissionSet,
fieldsPermissionSelected,
objectSelected
Expand All @@ -61,6 +88,7 @@ export default class PermsFieldNew extends SfCommand<PermsFieldNewResult> {
} catch (err) {
result.isSuccess = false;
result.errorMessage = (err as Error).message;
this.log(chalk.redBright(result.errorMessage));
}

return result;
Expand Down Expand Up @@ -101,13 +129,16 @@ export default class PermsFieldNew extends SfCommand<PermsFieldNewResult> {
return selectedFields.Fields;
}

private async selectPermissionSetsAndObject(directoryPath: string): Promise<{
private async selectPermissionSetsAndObject(
directoryPermissionSets: string,
directoryObjects: string
): Promise<{
permissionSetsSelected: string[];
objectSelected: string;
}> {
const pathToPermissionSets: string = path.resolve(directoryPath, 'permissionsets');
const pathToPermissionSets: string = path.resolve(directoryPermissionSets, 'permissionsets');
await this.checkDirectory(pathToPermissionSets);
const pathToObjects: string = path.resolve(directoryPath, 'objects');
const pathToObjects: string = path.resolve(directoryObjects, 'objects');
await this.checkDirectory(pathToObjects);

const [permissionsets, objects]: [string[], string[]] = await Promise.all([
Expand Down

0 comments on commit d41e4cd

Please sign in to comment.