-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_and_deploy_changes.js
235 lines (191 loc) · 6.53 KB
/
build_and_deploy_changes.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var ignoreDeployErrors = true; //allows deployment to continue even if some items fail.
var doDeployment = false; //should any kind of deployment be attempted or should we just generate the XML files? If this is false the checkONly mode flag has no effect.
var checkOnlyMode = true; //will only validate that the deployment will succeed but not do it if true.
var openDeployPageOnComplete = true; //open a web browser tab to the deployment status screen after each deploy.
var usernames = ['[email protected]','[email protected]']; //list of usernames configured in this sfdx project that you want to build and deploy for.
//don't change these unless you have a good reason to.
var apexScriptFile = 'buildPackage.apex';
var apexResultFile = 'executeResult.txt';
var outputPackageFile = 'destructiveChanges.xml';
var packageFolder = 'destructiveChanges';
var resultsObject;
const fs = require('fs');
const path = require('path')
const { exec } = require("child_process");
function init()
{
log(' Destructive Package Builder/Deployer 1.0!\r\n',true,'green');
log('Processing for usernames: ' + usernames);
var d = new Date();
d.toLocaleString();
log('Started process at ' + d, false);
mainLoop(0);
}
function mainLoop(index)
{
if(index>0) log('\n\nProcessing next org');
username = usernames[index];
log('Hang on getting some stuff together for ' + username +'.....',true);
runCommand('sfdx force:org:display --json -u ' +username,function(error, stdout, stderr){
resultsObject = JSON.parse(stdout);
log('Generating destructive changes for \x1b[33m' + username + '\x1b[0m org alias \x1b[33m' + resultsObject.result.alias + '\x1b[0m \r\n');
buildPackage(apexScriptFile,apexResultFile, username, function(error, stdout, stderr){
log('Apex Script Ran',true,'green');
var content = fs.readFileSync(apexResultFile);
log('Got File Content',true,'green');
writePackageXML();
var packagePayloadXML = readXMLFromApexResult(content.toString());
log('Extracted Package XML',true,'green');
writePackagePayload(outputPackageFile,packagePayloadXML);
log('Package XML Written to file',true,'green');
if(packagePayloadXML.indexOf('<name>') == -1)
{
log('Nothing found to destroy',true,'green');
if(index<usernames.length-1) mainLoop(index+1)
else finish();
}
else
{
if(doDeployment)
{
log('Deploying changes...',true,'green');
deployPackage(username, function(error, stdout, stderr){
deployResult = JSON.parse(stdout);
log(deployResult,false);
if(error || stderr || !deployResult.result.success)
{
log('----------------------- DEPLOYMENT ERROR! --------------------------',true,'red');
if(error) log(JSON.stringify(error, null, 2),true,'red');
if(stderr) log(JSON.stringify(stderr, null, 2),true,'red');
log(JSON.stringify(deployResult.result, null, 2));
}
else
{
log('DEPLOYMENT SUCCESS!',true,'green');
}
for(prop in deployResult.result)
{
if(deployResult.hasOwnProperty(prop))
{
log(prop + ': ' + deployResult[prop]);
log(deployResult.result,false);
}
}
if(openDeployPageOnComplete)
{
runCommand('start ' + resultsObject.result.instanceUrl+'/lightning/setup/DeployStatus/home');
}
if(index<usernames.length-1) mainLoop(index+1)
else finish();
});
}
else
{
log('Deployment disabled. Set doDeployment=true to perform validation/deployment');
finish();
}
}
});
});
}
function finish()
{
log('Process completed',true,'yellow');
log('\r\n\r\n------------------------------------------------ ', false);
process.exit(1);
}
function buildPackage(inputFile,outputFile,username,callback)
{
runCommand('sfdx force:apex:execute -f '+inputFile+' -u '+username+ '>'+outputFile, function(error, stdout, stderr){
callback(error, stdout, stderr);
});
}
function readXMLFromApexResult(content)
{
var xmlData = content.split('[packageScript]').pop().split('[/packageScript]')[0];
return xmlData;
}
function writePackageXML()
{
var packageXML = '<?xml version="1.0" encoding="UTF-8"?><Package xmlns="http://soap.sforce.com/2006/04/metadata"><version>48.0</version></Package>';
if (!fs.existsSync(packageFolder)){
fs.mkdirSync(packageFolder);
}
if (!fs.existsSync(packageFolder+'\\'+resultsObject.result.alias)){
fs.mkdirSync(packageFolder+'\\'+resultsObject.result.alias);
}
fs.writeFileSync(packageFolder+'\\'+resultsObject.result.alias+'\\package.xml', packageXML, function(err)
{
if(err)
{
return log(err);
}
log("Package.xml created.");
});
}
function writePackagePayload(filename, content)
{
if (!fs.existsSync(packageFolder)){
fs.mkdirSync(packageFolder);
}
if (!fs.existsSync(packageFolder+'\\'+resultsObject.result.alias)){
fs.mkdirSync(packageFolder+'\\'+resultsObject.result.alias);
}
fs.writeFileSync(packageFolder+'\\'+resultsObject.result.alias+'\\'+filename, content, function(err)
{
if(err)
{
return log(err);
}
log("destructiveChanges.xml created");
});
}
function deployPackage(username,callback)
{
var checkFlag = '';
if(checkOnlyMode) checkFlag = '-c';
var ignoreErrorFlag = ''
if(ignoreDeployErrors) ignoreErrorFlag = '-o';
runCommand('sfdx force:mdapi:deploy -d "destructiveChanges\\'+resultsObject.result.alias+'" -u '+username+' -w -1 --verbose --json '+checkFlag+ ' ' +ignoreErrorFlag, function(error, stdout, stderr){
callback(error, stdout, stderr);
});
}
function runCommand(command,callback)
{
exec(command, (error, stdout, stderr) => {
if (error)
{
log('error: ' + error.message,true,'red');
}
if (stderr)
{
//this command throws an error even though its working. probably because its like activly processing or something.
if(!command.startsWith('sfdx force:mdapi:deploy')) log('stderr: ' + stderr,true,'red');
}
if(callback) callback(error, stdout, stderr);
});
}
function log(logItem,printToScreen,color)
{
printToScreen = printToScreen != null ? printToScreen : true;
var colorCode='';
switch(color) {
case 'red':
colorCode='\x1b[31m'
break;
case 'green':
colorCode='\x1b[32m';
break;
case 'yellow':
colorCode='\x1b[33m';
}
if(printToScreen) console.log(colorCode+''+logItem+'\x1b[0m');
fs.appendFile('log.txt', logItem + '\r\n', function (err) {
if (err) throw err;
});
}
process.on('uncaughtException', (err) => {
log(err,true,'red');
process.exit(1) //mandatory (as per the Node docs)
})
init();