diff --git a/README.md b/README.md index 5353b5c..3e19e72 100644 --- a/README.md +++ b/README.md @@ -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 @@ -18,17 +22,26 @@ sf plugins install sf-perms ``` USAGE - $ sf perms field new [--json] [-d ] + $ sf perms field new [--json] [-d ] [-p ] [-f ] FLAGS - -d, --directory= [default: ./force-app/main/default] Default Directory + -d, --directory= The directory that contains the permissionsets/objects folder. + -f, --directory-object= The directory that contains the objects folder. + -p, --directory-permission-set= 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 ``` diff --git a/messages/perms.field.new.md b/messages/perms.field.new.md index bf31e70..f6648d6 100644 --- a/messages/perms.field.new.md +++ b/messages/perms.field.new.md @@ -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 diff --git a/src/commands/perms/field/new.ts b/src/commands/perms/field/new.ts index 887345d..e9c4e54 100644 --- a/src/commands/perms/field/new.ts +++ b/src/commands/perms/field/new.ts @@ -22,11 +22,22 @@ export default class PermsFieldNew extends SfCommand { 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; @@ -38,19 +49,35 @@ export default class PermsFieldNew extends SfCommand { }; 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 @@ -61,6 +88,7 @@ export default class PermsFieldNew extends SfCommand { } catch (err) { result.isSuccess = false; result.errorMessage = (err as Error).message; + this.log(chalk.redBright(result.errorMessage)); } return result; @@ -101,13 +129,16 @@ export default class PermsFieldNew extends SfCommand { 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([