From 40400bff41a1b371119700e7a92f7c60add75f89 Mon Sep 17 00:00:00 2001 From: Fabien Taillon Date: Wed, 10 Jan 2024 15:31:46 +0100 Subject: [PATCH] Avoid sf texei skinnyprofile create to fail if no profiles folder --- package.json | 2 +- src/commands/texei/skinnyprofile/create.ts | 151 +++++++++++---------- 2 files changed, 80 insertions(+), 73 deletions(-) diff --git a/package.json b/package.json index 05fe01e..b8b9de3 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "texei-sfdx-plugin", "description": "Texeï's plugin for sfdx", - "version": "2.2.3", + "version": "2.2.4", "author": "Texeï", "bugs": "https://github.com/texei/texei-sfdx-plugin/issues", "dependencies": { diff --git a/src/commands/texei/skinnyprofile/create.ts b/src/commands/texei/skinnyprofile/create.ts index e1281f6..977f370 100644 --- a/src/commands/texei/skinnyprofile/create.ts +++ b/src/commands/texei/skinnyprofile/create.ts @@ -40,6 +40,7 @@ export default class Create extends SfCommand { public async run(): Promise { const noProfile = 'No Profile found'; + const noProfileFolder = 'No profiles folder found'; const profileSucceed = 'Creation succeeded'; const profileFailed = 'Some Profiles creation failed, beware that some profiles may have been created anyway'; @@ -60,88 +61,94 @@ export default class Create extends SfCommand { // Get profiles files path const profilePath = flags.path ? flags.path : path.join(await getDefaultPackagePath(), 'profiles'); - profilesInPath = getProfilesInPath(profilePath, false); - if (profilesInPath === undefined || profilesInPath.length === 0) { - commandResult = noProfile; - } else { - // Get existing custom profiles in target org - // Profile can be queried via PermissionSet, only way to find if a Profile is Custom ? - // https://salesforce.stackexchange.com/questions/38447/determine-custom-profile - const existingCustomProfiles = ( - ( - await this.connection.query( - 'SELECT Profile.Name FROM PermissionSet Where IsCustom = true AND ProfileId != null' - ) - ).records as PermissionSetRecord[] - ).map((record) => record.Profile.Name); - - // Get User Licenses Ids from target org - const userLicensesMap = await this.getUserLicensesMap(); - - for (const profile of profilesInPath) { - // Generate path - const filePath = path.join(process.cwd(), profilePath, profile); - - // Read data file - const data = fs.readFileSync(filePath, 'utf8'); - - // Parsing file - const profileJson: ProfileMetadataType = parser.parse(data) as ProfileMetadataType; - - const profileName = profile.replace('.profile-meta.xml', ''); - - if (profileJson.Profile.custom) { - if (existingCustomProfiles.includes(profileName)) { - // Profile is custom but already exists, don't create it - profilesAlreadyInOrg.push(profileName); - } else { - const userLicense = userLicensesMap.get(profileJson.Profile.userLicense); - - if (userLicense === undefined) { - // User License not found in org - profilesWithError.push({ - name: profileName, - errors: [ - { - message: `userLicense '${profileJson.Profile.userLicense}' does not exist in target org`, - statusCode: 'USER_LICENSE_NOT_IN_ORG', - }, - ], - }); + if (fs.existsSync(profilePath)) { + // There is a profiles folder + profilesInPath = getProfilesInPath(profilePath, false); + + if (profilesInPath === undefined || profilesInPath.length === 0) { + commandResult = noProfile; + } else { + // Get existing custom profiles in target org + // Profile can be queried via PermissionSet, only way to find if a Profile is Custom ? + // https://salesforce.stackexchange.com/questions/38447/determine-custom-profile + const existingCustomProfiles = ( + ( + await this.connection.query( + 'SELECT Profile.Name FROM PermissionSet Where IsCustom = true AND ProfileId != null' + ) + ).records as PermissionSetRecord[] + ).map((record) => record.Profile.Name); + + // Get User Licenses Ids from target org + const userLicensesMap = await this.getUserLicensesMap(); + + for (const profile of profilesInPath) { + // Generate path + const filePath = path.join(process.cwd(), profilePath, profile); + + // Read data file + const data = fs.readFileSync(filePath, 'utf8'); + + // Parsing file + const profileJson: ProfileMetadataType = parser.parse(data) as ProfileMetadataType; + + const profileName = profile.replace('.profile-meta.xml', ''); + + if (profileJson.Profile.custom) { + if (existingCustomProfiles.includes(profileName)) { + // Profile is custom but already exists, don't create it + profilesAlreadyInOrg.push(profileName); } else { - profileMetadata.push({ - Name: profileName, - UserLicenseId: userLicense, - type: 'Profile', - }); + const userLicense = userLicensesMap.get(profileJson.Profile.userLicense); + + if (userLicense === undefined) { + // User License not found in org + profilesWithError.push({ + name: profileName, + errors: [ + { + message: `userLicense '${profileJson.Profile.userLicense}' does not exist in target org`, + statusCode: 'USER_LICENSE_NOT_IN_ORG', + }, + ], + }); + } else { + profileMetadata.push({ + Name: profileName, + UserLicenseId: userLicense, + type: 'Profile', + }); + } } + } else { + // It's a standard Profile, don't create it + profilesStandardSkipped.push(profileName); } - } else { - // It's a standard Profile, don't create it - profilesStandardSkipped.push(profileName); } } - } - if (profileMetadata.length > 0) { - const results = await this.connection?.soap.create(profileMetadata); - - for (let i = 0; i < results.length; i++) { - const profile = profileMetadata[i]; - const result = results[i]; - if (result.success) { - profilesCreated.push(profile.Name as string); - } else { - profilesWithError.push({ - name: profile.Name as string, - errors: result.errors, - }); + if (profileMetadata.length > 0) { + const results = await this.connection?.soap.create(profileMetadata); + + for (let i = 0; i < results.length; i++) { + const profile = profileMetadata[i]; + const result = results[i]; + if (result.success) { + profilesCreated.push(profile.Name as string); + } else { + profilesWithError.push({ + name: profile.Name as string, + errors: result.errors, + }); + } } } + } else { + commandResult = noProfileFolder; } - if (commandResult === noProfile) { + if (commandResult === noProfile || commandResult === noProfileFolder) { this.log(commandResult); } else { commandResult = profilesWithError.length > 0 ? profileFailed : profileSucceed; @@ -172,7 +179,7 @@ export default class Create extends SfCommand { profilesWithError, }; - if (profilesWithError && !flags['ignoreerrors']) { + if (profilesWithError?.length > 0 && !flags['ignoreerrors']) { const finalError = new SfError(profileFailed); finalError.setData(finalResult); throw finalError;