forked from salesforcecli/plugin-metadata-hook-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadataReplaceDeploy.ts
69 lines (56 loc) · 1.9 KB
/
metadataReplaceDeploy.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
import * as fs from 'fs';
import { Builder, parseString } from 'xml2js';
import { Command, Hook } from '@oclif/config';
type HookFunction = (this: Hook.Context, options: HookOptions) => any;
type HookOptions = {
Command: Command.Class;
argv: string[];
commandId: string;
result?: PreDeployResult;
};
type PreDeployResult = {
[aggregateName: string]: {
mdapiFilePath: string;
workspaceElements: {
fullName: string;
metadataName: string;
sourcePath: string;
state: string;
deleteSupported: boolean;
}[];
};
};
export const hook: HookFunction = async function(options) {
console.log('PreDepoy Hook Running');
// Run only on the push command, not the deploy command
// if (options.commandId === 'force:source:push') {
if (options.result) {
Object.keys(options.result).forEach(mdapiElementName => {
console.log('Updating the ' + mdapiElementName + ' object');
let mdapiElement = options.result![mdapiElementName]!;
// Update the object in the org (the metadata that is being deployed)
updateObjectDescription(mdapiElement.mdapiFilePath);
// Update the object locally
// updateObjectDescription(mdapiElement.workspaceElements[0].sourcePath);
});
}
// }
};
export default hook;
function updateObjectDescription(objectPath: string) {
fs.readFile(objectPath, 'utf-8', function(err, data) {
if (err) throw err;
if (data) {
parseString(data, function(err, json) {
if (err) throw err;
// Replace the description of the object being pushed with the value of an environment variable
if (json.CustomObject && json.CustomObject.description) {
json.CustomObject.description =
process.env.SFDX_NEW_METADATA_VALUE || 'Default new description';
}
let xml = new Builder().buildObject(json);
fs.writeFile(objectPath, xml, function() {});
});
}
});
}