Skip to content

Commit

Permalink
Merge pull request #148 from texei/no-profile-no-fail
Browse files Browse the repository at this point in the history
Avoid sf texei skinnyprofile check to fail if no profiles folder
  • Loading branch information
FabienTaillon authored Jan 8, 2024
2 parents 75a1a60 + 39e7f4f commit 2cc42f2
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 40 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.2",
"version": "2.2.3",
"author": "Texeï",
"bugs": "https://github.com/texei/texei-sfdx-plugin/issues",
"dependencies": {
Expand Down
85 changes: 46 additions & 39 deletions src/commands/texei/skinnyprofile/check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,58 +40,65 @@ export default class Check extends SfCommand<CheckResult> {
let commandResult = '';

// Get profiles files path
let profileDirPath = '';
if (flags.path) {
// A path was provided with the flag
profilesToCheck = getProfilesInPath(flags.path, true);
profileDirPath = flags.path;
} else {
// Else look in the default package directory
const defaultPackageDirectory = path.join(await getDefaultPackagePath(), 'profiles');
profilesToCheck = getProfilesInPath(defaultPackageDirectory, true);
profileDirPath = path.join(await getDefaultPackagePath(), 'profiles');
}

if (profilesToCheck === undefined || profilesToCheck.length === 0) {
commandResult = 'No Profile found';
} else {
for (const profilePath of profilesToCheck) {
// Generate path
const filePath = path.join(process.cwd(), profilePath);

// Read data file
const data = fs.readFileSync(filePath, 'utf8');

// Parsing file
// TODO: refactor to avoid await in loop ? Not sure we want all metadata in memory
// eslint-disable-next-line
const profileJson: ProfileMetadataType = (await xml2js.parseStringPromise(data)) as ProfileMetadataType;

// Looking for unwanted nodes
for (const [key, value] of Object.entries(profileJson?.Profile)) {
this.debug('key:');
this.debug(key);
this.debug('value:');
this.debug(value);

if (Object.prototype.hasOwnProperty.call(profileJson.Profile, key)) {
if (nodesNotAllowed.includes(key)) {
// Could definitely be improved
if (!invalidProfiles.includes(profilePath)) {
invalidProfiles.push(profilePath);
if (fs.existsSync(profileDirPath)) {
// There is a profiles folder
profilesToCheck = getProfilesInPath(profileDirPath, true);

if (profilesToCheck === undefined || profilesToCheck.length === 0) {
commandResult = 'No Profile found';
} else {
for (const profilePath of profilesToCheck) {
// Generate path
const filePath = path.join(process.cwd(), profilePath);

// Read data file
const data = fs.readFileSync(filePath, 'utf8');

// Parsing file
// TODO: refactor to avoid await in loop ? Not sure we want all metadata in memory
// eslint-disable-next-line
const profileJson: ProfileMetadataType = (await xml2js.parseStringPromise(data)) as ProfileMetadataType;

// Looking for unwanted nodes
for (const [key, value] of Object.entries(profileJson?.Profile)) {
this.debug('key:');
this.debug(key);
this.debug('value:');
this.debug(value);

if (Object.prototype.hasOwnProperty.call(profileJson.Profile, key)) {
if (nodesNotAllowed.includes(key)) {
// Could definitely be improved
if (!invalidProfiles.includes(profilePath)) {
invalidProfiles.push(profilePath);
}
break;
}
break;
}
}
}
}
}

if (invalidProfiles.length === 0) {
commandResult = 'All Profiles valid';
if (invalidProfiles.length === 0) {
commandResult = 'All Profiles valid';
} else {
throw new SfError(
`Invalid Profile${invalidProfiles.length > 1 ? 's' : ''} found: ${invalidProfiles.join(
', '
)}. Please run sf texei skinnyprofile retrieve`
);
}
} else {
throw new SfError(
`Invalid Profile${invalidProfiles.length > 1 ? 's' : ''} found: ${invalidProfiles.join(
', '
)}. Please run sfdx texei:skinnyprofile:retrieve`
);
commandResult = 'No profiles folder found';
}

this.log(commandResult);
Expand Down

0 comments on commit 2cc42f2

Please sign in to comment.