-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathsplit.ts
148 lines (120 loc) · 6.03 KB
/
split.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* tslint:disable */
import {core, flags, SfdxCommand} from '@salesforce/command';
import fs = require('fs-extra');
import convert = require('xml-js');
import path = require('path');
import config = require('../../../shared/config.json');
import chalk from 'chalk';
function createModel(key, value) {
const data = {
_declaration: {
_attributes: {
version: '1.0',
encoding: 'UTF-8'
}
},
Profile: {
_attributes: {
xmlns: 'http://soap.sforce.com/2006/04/metadata'
}
}
};
data.Profile[key] = value;
return data;
}
export default class Split extends SfdxCommand {
public static description = 'Split profiles into smaller parts.';
public static examples = [`
sfdx metadata:profiles:split -i force-app/main/default/profiles -o force-app/main/default/test
//Splits profiles located in specified input dir and copies them into the output dir.
`];
protected static flagsConfig = {
input: flags.string({char: 'i', default: 'force-app/main/default/profiles', required: true, description: 'the input directory where the full profiles exist.'}),
output: flags.string({char: 'o', default: 'force-app/main/default/profiles', required: true, description: 'the output directory to store the chunked profiles.'}),
delete: flags.boolean({char: 'd', default: false, description: 'Delete the existing profiles once converted?'})
};
// Set this to true if your command requires a project workspace; 'requiresProject' is false by default
protected static requiresProject = false;
public async split(inputDir: string, outputDir: string, deleteProfile: boolean): Promise<any> {
try {
const root = path.resolve(inputDir);
const location = path.resolve(outputDir);
await fs.ensureDir(location);
const fileNames = await fs.readdir(root);
for (const fileName of fileNames) {
if (fileName.includes('.profile')) {
this.ux.log(chalk.bold(chalk.black(('Splitting profile: ' + fileName))));
const dirRoot = location + '/' + fileName.replace('.profile', '');
await fs.ensureDir(dirRoot);
const xml = await fs.readFile(root + '/' + fileName);
const stream = convert.xml2js(xml.toString(), config.jsonExport);
for (const metatag of Object.values(config.profiles.metaTags)) {
if (stream['Profile'][metatag] === undefined) {
continue;
}
const model = createModel(metatag, stream['Profile'][metatag]);
const itemRoot = dirRoot + '/' + metatag;
await fs.ensureDir(itemRoot);
await fs.writeFile(
itemRoot + '/' + metatag + '-meta.xml',
convert.json2xml(JSON.stringify(model), config.xmlExport)
);
}
for (const metadata of Object.keys(config.profiles.tags)) {
const itemRoot = dirRoot + '/' + metadata;
await fs.ensureDir(itemRoot);
const targetName = config.profiles.tags[metadata].nameTag;
if (stream['Profile'][metadata] === undefined) {
continue;
}
if (Array.isArray(stream['Profile'][metadata])) {
if (targetName === '_self') {
const model = createModel(metadata, stream['Profile'][metadata]);
await fs.writeFile(
itemRoot + '/' + metadata + '-meta.xml',
convert.json2xml(JSON.stringify(model), config.xmlExport)
);
} else {
for (const item of stream['Profile'][metadata]) {
let model = createModel(metadata, [item]);
await fs.writeFile(
itemRoot + '/' + item[targetName]._text + '-meta.xml',
convert.json2xml(JSON.stringify(model), config.xmlExport)
);
}
}
} else {
const item = stream['Profile'][metadata];
const model = createModel(metadata, item);
let newFileName = '';
if (targetName === '_self') {
newFileName = metadata + '-meta.xml';
} else {
newFileName = stream['Profile'][metadata][targetName]._text + '-meta.xml';
}
await fs.writeFile(
itemRoot + '/' + newFileName,
convert.json2xml(JSON.stringify(model), config.xmlExport)
);
}
}
if (deleteProfile === true) {
await fs.remove(root + '/' + fileName);
}
}
}
} catch(ex) {
this.ux.error(chalk.bold(chalk.red(ex)));
return 1;
}
return 0;
}
public async run(): Promise<core.AnyJson> {
const inputDir = this.flags.input;
const outputDir = this.flags.output;
const deleteProfile = this.flags.delete;
await this.split(inputDir, outputDir, deleteProfile);
// Return an object to be displayed with --json
return {};
}
}