-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathalign.ts
173 lines (155 loc) · 8.45 KB
/
align.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import { flags, SfdxCommand } from '@salesforce/command';
import chalk from 'chalk';
import { getExisting } from '../../../../shared/getExisting';
import { metadataTypes } from '../../../../shared/permsetProfileMetadata';
import { setupArray } from '../../../../shared/setupArray';
import { writeJSONasXML } from '../../../../shared/JSONXMLtools';
import fs = require('fs-extra');
export default class PermAlign extends SfdxCommand {
public static description = 'align profiles with ';
public static examples = [
`sfdx shane:object:perms:align
// go through all the profiles/permsets in force-app/main/default and remove references to stuff that isn't in local source
`
];
protected static flagsConfig = {
directory: flags.directory({ char: 'd', default: 'force-app/main/default', description: 'Where is all this metadata?' })
};
protected static requiresProject = true;
public async run(): Promise<any> {
const profileDirectory = `${this.flags.directory}/profiles`;
const permsetDirectory = `${this.flags.directory}/permissionsets`;
if (!this.flags.specific) {
// just do all of them
if (fs.existsSync(profileDirectory)) {
const profiles = fs.readdirSync(profileDirectory);
for (const p of profiles) {
const targetFilename = `${profileDirectory}/${p}`;
await this.removePerms(targetFilename, 'Profile');
}
} else {
this.ux.warn('no profiles found');
}
if (fs.existsSync(permsetDirectory)) {
const permsets = fs.readdirSync(permsetDirectory);
for (const p of permsets) {
const targetFilename = `${permsetDirectory}/${p}`;
await this.removePerms(targetFilename, 'PermissionSet');
}
} else {
this.ux.warn('no perm sets found');
}
// } else if (this.flags.specific) {
// // ok, what kind is it and does it exist?
// if (fs.existsSync(`${profileDirectory}/${this.flags.specific}`)) {
// await this.removePerms(this.flags.specific, 'Profile');
// } else if (fs.existsSync(`${permsetDirectory}/${this.flags.specific}`)) {
// await this.removePerms(this.flags.specific, 'PermissionSet');
// } else {
// throw new Error(`not found: ${this.flags.specific}`);
// }
}
this.ux.log(chalk.green('Done!'));
}
public async removePerms(targetFilename: string, profileOrPermset: 'Profile' | 'PermissionSet'): Promise<any> {
let existing = await getExisting(targetFilename, profileOrPermset);
const objDir = `${this.flags.directory}/objects`;
const objects = fs.existsSync(objDir) ? fs.readdirSync(objDir) : [];
const layouts = fs.existsSync(`${this.flags.directory}/layouts`) ? fs.readdirSync(`${this.flags.directory}/layouts`) : [];
const tabs = fs.existsSync(`${this.flags.directory}/tabs`) ? fs.readdirSync(`${this.flags.directory}/tabs`) : [];
const dataSources = fs.existsSync(`${this.flags.directory}/dataSources`) ? fs.readdirSync(`${this.flags.directory}/dataSources`) : [];
const applications = fs.existsSync(`${this.flags.directory}/applications`) ? fs.readdirSync(`${this.flags.directory}/applications`) : [];
const typeKey = profileOrPermset === 'Profile' ? 'profileType' : 'permSetType';
// only use types with matching typekeys
for (const mdType of metadataTypes.filter(item => item[typeKey])) {
existing = setupArray(existing, mdType[typeKey]);
if (mdType.key === 'object') {
// objects exist as folders, so they don't have file extensions, just the object name
existing.objectPermissions = existing.objectPermissions.filter(item => {
if (objects.includes(item.object)) {
return true;
}
this.ux.log(`${chalk.cyan(targetFilename)}: removing object perm for ${item.object}`);
return false;
});
}
if (mdType.key === 'field') {
// the object exists locally, and if so, the field does, too.
existing.fieldPermissions = existing.fieldPermissions.filter(item => {
const objectName = item.field.split('.')[0];
const fieldName = `${item.field.split('.')[1]}.field-meta.xml`;
if (objects.includes(objectName) && fs.readdirSync(`${objDir}/${objectName}/fields`).includes(fieldName)) {
return true;
}
this.ux.log(`${chalk.cyan(targetFilename)}: removing field perm for ${item.field}`);
return false;
});
}
if (mdType.key === 'layout') {
// the object exists AND so does the specified layout
existing.layoutAssignments = existing.layoutAssignments.filter(item => {
const objectName = item.layout.split('-')[0];
if (objects.includes(objectName) && layouts.includes(`${item.layout}.layout-meta.xml`)) {
return true;
}
this.ux.log(`${chalk.cyan(targetFilename)}: removing layout assignment for ${item.layout}`);
return false;
});
}
if (mdType.key === 'recordType') {
existing.recordTypeVisibilities = existing.recordTypeVisibilities.filter(item => {
const objectName = item.recordType.split('.')[0];
const recordTypeName = `${item.recordType.split('.')[1]}.recordType-meta.xml`;
if (objects.includes(objectName) && fs.readdirSync(`${objDir}/${objectName}/recordTypes`).includes(recordTypeName)) {
return true;
}
this.ux.log(`${chalk.cyan(targetFilename)}: removing recordTypeVisibility for ${item.recordType}`);
return false;
});
}
if (mdType.key === 'tab') {
existing[mdType[typeKey]] = existing[mdType[typeKey]].filter(item => checkTab(item.tab, tabs, objects));
}
if (mdType.key === 'externalDataSource') {
existing.externalDataSourceAccesses = existing.externalDataSourceAccesses.filter(item => {
if (dataSources.includes(`${item.externalDataSource}.dataSource-meta.xml`)) {
return true;
}
this.ux.log(`${chalk.cyan(targetFilename)}: removing external data source ${item.externalDataSource}`);
return false;
});
}
if (mdType.key === 'application') {
existing.applicationVisibilities = existing.applicationVisibilities.filter(item => {
if (applications.includes(`${item.application}.app-meta.xml`)) {
return true;
}
this.ux.log(`${chalk.cyan(targetFilename)}: removing app ${item.application}`);
return false;
});
}
}
// remove empty stuff
for (const mdType of metadataTypes) {
if (existing[mdType[typeKey]] && existing[mdType[typeKey]].length === 0) {
delete existing[mdType[typeKey]];
}
}
await writeJSONasXML({
path: targetFilename,
type: profileOrPermset,
json: existing
});
// this.ux.log(`removed ${objectBefore - existing.objectPermissions.length} objects, ${recordTypeBefore - existing.recordTypeVisibilities.length} recordTypes, ${layoutBefore - existing.layoutAssignments.length} layout, ${fieldBefore - existing.fieldPermissions.length} fields from ${this.flags.object} ${chalk.cyan(targetFilename)}`);
}
}
const checkTab = (tabName: string, tabs: string[], objects: string[]): boolean => {
const tabFileName = `${tabName}.tab-meta.xml`;
if (tabs.includes(tabFileName)) {
return true; // the tab is there locally, so include it in the profile
}
if (objects.includes(tabName.replace('standard-', ''))) {
return true; // the object is there locally, so include the tab
}
return false;
};