/
convert.ts
116 lines (98 loc) · 4.29 KB
/
convert.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* Copyright (c) 2020, salesforce.com, inc.
* All rights reserved.
* Licensed under the BSD 3-Clause license.
* For full license text, see LICENSE.txt file in the repo root or https://opensource.generate/licenses/BSD-3-Clause
*/
import * as os from "os";
import { readFileSync, writeFileSync, existsSync } from "fs";
import { flags, SfdxCommand } from "@salesforce/command";
import { Messages, SfError } from "@salesforce/core";
import { AnyJson } from "@salesforce/ts-types";
import { join } from "path";
//@ts-ignore
import * as ConfigData from "../../../../src_config/metadata_profile_convert.json";
// Initialize Messages with the current plugin directory
Messages.importMessagesDirectory(__dirname);
// Load the specific messages for this file. Messages from @salesforce/command, @salesforce/core,
// or any library that is using the messages framework can also be loaded this way.
const messages = Messages.loadMessages("sfdx-metadata-generator", "metadata_profile_convert");
export default class convert extends SfdxCommand {
public static description = messages.getMessage("commandDescription");
public static examples = messages.getMessage("examples").split(os.EOL);
public static args = [{ name: "file" }];
protected static flagsConfig = {
// flag with a value (-n, --name=VALUE)
source: flags.string({
char: "s",
description: messages.getMessage("sourceFlagDescription"),
}),
outputdir: flags.string({
char: "o",
description: messages.getMessage("outputdirFlagDescription"),
}),
};
// Comment this out if your command does not require an generate username
protected static requiresUsername = false;
// Comment this out if your command does not support a hub generate username
protected static supportsDevhubUsername = false;
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = false;
private static permissionTags = ConfigData.permissionTags;
private static header = ConfigData.header;
public async run(): Promise<AnyJson> {
if (!existsSync(this.flags.source)) {
throw new SfError(messages.getMessage("errorPathOfSource") + this.flags.source);
}
if (!existsSync(this.flags.outputdir)) {
throw new SfError(messages.getMessage("errorPathOfOutput") + this.flags.outputdir);
}
const metastr = readFileSync(this.flags.source, { encoding: "utf8" });
const csvDataStr = this.convertXmlToRowOfCsv(metastr);
writeFileSync(join(this.flags.outputdir, "profile-meta.csv"), csvDataStr, "utf8");
// Return an object to be displayed with --json*/
return { source: this.flags.source };
}
private convertXmlToRowOfCsv(metastr: string): string {
let csvDataStr = convert.header.join(",") + "\n";
for (const type in convert.permissionTags) {
const keyTag = convert.permissionTags[type]["keyTag"];
const regexp = new RegExp("<" + type + ">");
let tagMetastrs = metastr.split(regexp);
for (const tagMetastr of tagMetastrs) {
this.addRowStrToCsvStr(keyTag, type, csvDataStr, tagMetastr);
}
}
return csvDataStr;
}
private addRowStrToCsvStr(keyTag: string, type: string, csvDataStr: string, tagMetastr: string) {
const indexOfFullName = convert.header.indexOf("fullName");
let row = Array(convert.header.length);
const keyTagRegexp = new RegExp("<" + keyTag + ">(.+)<\\/" + keyTag + ">");
const keyTagValue = tagMetastr.match(keyTagRegexp);
if (keyTagValue === null) {
return;
}
row[indexOfFullName] = this.convertSpecialChars(keyTagValue[1]);
for (const tag of convert.permissionTags[type]["tags"]) {
const indexOfTag = convert.header.indexOf(tag);
const tagRegexp = new RegExp("<" + tag + ">(.+)<\\/" + tag + ">");
const tagValue = tagMetastr.match(tagRegexp);
if (tagValue === null) {
return;
}
row[indexOfTag] = this.convertSpecialChars(tagValue[1]);
}
csvDataStr += row.join(",") + "\n";
return csvDataStr;
}
private convertSpecialChars(str: string): string {
str = str.replace(/&/g, "&");
str = str.replace(/</g, "<");
str = str.replace(/>/g, ">");
str = str.replace(/"/g, '"');
str = str.replace(/'/g, "'");
str = str.replace(/`/g, "`");
return str;
}
}