Skip to content

Commit

Permalink
Merge pull request #149 from texei/no-profile-no-fail
Browse files Browse the repository at this point in the history
Avoid sf texei skinnyprofile create to fail if no profiles folder
  • Loading branch information
FabienTaillon authored Jan 10, 2024
2 parents 2cc42f2 + 40400bf commit fa80b28
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 73 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
151 changes: 79 additions & 72 deletions src/commands/texei/skinnyprofile/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export default class Create extends SfCommand<SkinnyprofileCreateResult> {

public async run(): Promise<SkinnyprofileCreateResult> {
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';

Expand All @@ -60,88 +61,94 @@ export default class Create extends SfCommand<SkinnyprofileCreateResult> {

// 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;
Expand Down Expand Up @@ -172,7 +179,7 @@ export default class Create extends SfCommand<SkinnyprofileCreateResult> {
profilesWithError,
};

if (profilesWithError && !flags['ignoreerrors']) {
if (profilesWithError?.length > 0 && !flags['ignoreerrors']) {
const finalError = new SfError(profileFailed);
finalError.setData(finalResult);
throw finalError;
Expand Down

0 comments on commit fa80b28

Please sign in to comment.