-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ahmed BENSAAD
committed
Dec 15, 2021
1 parent
8e7e09a
commit 8112c79
Showing
2 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { flags, SfdxCommand } from "@salesforce/command"; | ||
import { AnyJson } from "@salesforce/ts-types"; | ||
import * as chalk from "chalk"; | ||
import * as x2jParser from "fast-xml-parser"; | ||
import * as path from "path"; | ||
import { j2xParser } from "fast-xml-parser"; | ||
|
||
import { j2xOptions, x2jOptions } from "../../../config/fastXMLOptions"; | ||
|
||
import { | ||
substringBefore, | ||
readFile, | ||
writeXMLFile, | ||
profileAccessFilenamesCompare, | ||
} from "../../../utils/utilities"; | ||
|
||
export default class Cleaner extends SfdxCommand { | ||
public static examples = [ | ||
`$ sfdx mdt:profile:clean -p {sourcepath} -l {cleansourcepath} [-d {outputdirectory}] | ||
Clean Profile xml file | ||
`, | ||
]; | ||
|
||
protected static flagsConfig = { | ||
sourcepath: flags.string({ | ||
char: "p", | ||
required: true, | ||
description: "The path to the source metadata file", | ||
}), | ||
cleansourcepath: flags.string({ | ||
char: "l", | ||
description: "The path to the file that list the profile access to clean", | ||
}), | ||
outputdir: flags.string({ | ||
char: "d", | ||
description: "The output directory that stores metadata files", | ||
}), | ||
}; | ||
|
||
public async run(): Promise<AnyJson> { | ||
this.ux.startSpinner(chalk.yellowBright("Cleaning Profile")); | ||
try { | ||
await this.clean( | ||
this.flags.sourcepath, | ||
this.flags.cleansourcepath, | ||
this.flags.outputdir | ||
); | ||
} catch (e) { | ||
// output error | ||
this.ux.stopSpinner("❌"); | ||
this.ux.error(chalk.redBright(e)); | ||
} | ||
this.ux.stopSpinner("✔️"); | ||
// Return an object to be displayed with --json | ||
return { success: true }; | ||
} | ||
|
||
public async clean( | ||
sourcepath: string, | ||
cleansourcepath: string, | ||
outputdir: string | ||
) { | ||
const profileXML: string = await readFile(sourcepath); | ||
const cleanXML: string = await readFile(cleansourcepath); | ||
const profileName: string = substringBefore(path.basename(sourcepath), "."); | ||
const destpath: string = outputdir | ||
? `${outputdir}/${profileName}.profile-meta.xml` | ||
: sourcepath; | ||
|
||
if (x2jParser.validate(cleanXML) === true) { | ||
if (x2jParser.validate(profileXML) === true) { | ||
// parse xml to json | ||
const profileJSON = x2jParser.parse(profileXML, x2jOptions); | ||
const cleanJSON = x2jParser.parse(cleanXML, x2jOptions); | ||
|
||
for (const accessTypeToClean in cleanJSON.Profile) { | ||
const accessList = profileJSON.Profile[accessTypeToClean]; | ||
if (Array.isArray(accessList)) { | ||
profileJSON.Profile[accessTypeToClean] = accessList.filter( | ||
(access) => { | ||
const cleanList = cleanJSON.Profile[accessTypeToClean]; | ||
if (Array.isArray(cleanList)) { | ||
for (const accessToClean of cleanJSON.Profile[ | ||
accessTypeToClean | ||
]) { | ||
if ( | ||
profileAccessFilenamesCompare( | ||
accessTypeToClean, | ||
access, | ||
accessToClean | ||
) === 0 | ||
) { | ||
return false; | ||
} | ||
} | ||
} else { | ||
if ( | ||
profileAccessFilenamesCompare( | ||
accessTypeToClean, | ||
access, | ||
cleanList | ||
) === 0 | ||
) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
); | ||
} | ||
} | ||
|
||
// init the json to xml parser | ||
const json2xmlParser = new j2xParser(j2xOptions); | ||
|
||
// parse json to xml | ||
const formattedXml: string = json2xmlParser.parse(profileJSON); | ||
|
||
// write xml version & encoding | ||
await writeXMLFile(`${destpath}`, formattedXml); | ||
} else { | ||
throw `${sourcepath} is an invalid xml file`; | ||
} | ||
} else { | ||
throw `${cleansourcepath} is an invalid xml file`; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters