generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 19
/
deploy.ts
256 lines (240 loc) · 9.88 KB
/
deploy.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* 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.org/licenses/BSD-3-Clause
*/
import { Duration, env } from '@salesforce/kit';
import { Lifecycle, Messages, Org } from '@salesforce/core';
import { AsyncResult, DeployVersionData, MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
import { Flags, loglevel, orgApiVersionFlagWithDeprecations, Ux } from '@salesforce/sf-plugins-core';
import { Interfaces } from '@oclif/core';
import {
DeployCommand,
getCoverageFormattersOptions,
reportsFormatters,
targetUsernameFlag,
TestLevel,
} from '../../../deployCommand.js';
import { DeployCommandAsyncResult } from '../../../formatters/source/deployAsyncResultFormatter.js';
import { MdDeployResult, MdDeployResultFormatter } from '../../../formatters/mdapi/mdDeployResultFormatter.js';
import { DeployProgressBarFormatter } from '../../../formatters/deployProgressBarFormatter.js';
import { DeployProgressStatusFormatter } from '../../../formatters/deployProgressStatusFormatter.js';
import { MdDeployAsyncResultFormatter } from '../../../formatters/mdapi/mdDeployAsyncResultFormatter.js';
import { ResultFormatterOptions } from '../../../formatters/resultFormatter.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-source', 'md.deploy');
const deployMessages = Messages.loadMessages('@salesforce/plugin-source', 'deployCommand');
const xorFlags = ['zipfile', 'validateddeployrequestid', 'deploydir'];
export type DeployResult = MdDeployResult | DeployCommandAsyncResult;
const replacement = 'project deploy start';
export class Deploy extends DeployCommand {
public static readonly state = 'deprecated';
public static readonly deprecationOptions = {
to: replacement,
message: messages.getMessage('deprecation', [replacement]),
};
public static readonly hidden = true;
public static readonly summary = messages.getMessage('summary');
public static readonly examples = messages.getMessages('examples');
public static readonly flags = {
'api-version': orgApiVersionFlagWithDeprecations,
loglevel,
'target-org': targetUsernameFlag,
checkonly: Flags.boolean({
char: 'c',
description: messages.getMessage('flags.checkOnly.description'),
summary: messages.getMessage('flags.checkOnly.summary'),
}),
deploydir: Flags.directory({
char: 'd',
description: messages.getMessage('flags.deployDir.description'),
summary: messages.getMessage('flags.deployDir.summary'),
exactlyOne: xorFlags,
}),
wait: Flags.duration({
char: 'w',
unit: 'minutes',
description: messages.getMessage('flags.wait.description'),
summary: messages.getMessage('flags.wait.summary'),
default: Duration.minutes(0),
min: -1,
}),
testlevel: Flags.string({
char: 'l',
description: messages.getMessage('flags.testLevel.description'),
summary: messages.getMessage('flags.testLevel.summary'),
options: ['NoTestRun', 'RunSpecifiedTests', 'RunLocalTests', 'RunAllTestsInOrg'],
}),
runtests: Flags.string({
char: 'r',
multiple: true,
delimiter: ',',
summary: messages.getMessage('flags.runTests.summary'),
}),
ignoreerrors: Flags.boolean({
// break this
char: 'o',
description: messages.getMessage('flags.ignoreErrors.description'),
summary: messages.getMessage('flags.ignoreErrors.summary'),
}),
ignorewarnings: Flags.boolean({
char: 'g',
description: messages.getMessage('flags.ignoreWarnings.description'),
summary: messages.getMessage('flags.ignoreWarnings.summary'),
}),
validateddeployrequestid: Flags.salesforceId({
char: 'q',
length: 'both',
startsWith: '0Af',
description: messages.getMessage('flags.validatedDeployRequestId.description'),
summary: messages.getMessage('flags.validatedDeployRequestId.summary'),
exactlyOne: xorFlags,
exclusive: ['testlevel', 'runtests', 'checkonly'],
}),
verbose: Flags.boolean({
description: messages.getMessage('flags.verbose.description'),
summary: messages.getMessage('flags.verbose.summary'),
}),
zipfile: Flags.file({
char: 'f',
description: messages.getMessage('flags.zipFile.description'),
summary: messages.getMessage('flags.zipFile.summary'),
exactlyOne: xorFlags,
}),
singlepackage: Flags.boolean({
char: 's',
description: messages.getMessage('flags.singlePackage.description'),
summary: messages.getMessage('flags.singlePackage.summary'),
}),
soapdeploy: Flags.boolean({
description: messages.getMessage('flags.soapDeploy.description'),
summary: messages.getMessage('flags.soapDeploy.summary'),
}),
purgeondelete: Flags.boolean({
summary: messages.getMessage('flags.purgeOnDelete.summary'),
}),
concise: Flags.boolean({
summary: messages.getMessage('flags.concise.summary'),
}),
resultsdir: Flags.directory({
summary: messages.getMessage('flags.resultsDir.summary'),
}),
coverageformatters: Flags.string({
multiple: true,
delimiter: ',',
summary: messages.getMessage('flags.coverageFormatters.summary'),
options: reportsFormatters,
helpValue: reportsFormatters.join(','),
}),
junit: Flags.boolean({ summary: messages.getMessage('flags.junit.summary') }),
};
private flags!: Interfaces.InferredFlags<typeof Deploy.flags>;
private org!: Org;
public async run(): Promise<DeployResult> {
this.flags = (await this.parse(Deploy)).flags;
this.org = await Org.create({ aliasOrUsername: this.flags['target-org'] });
await this.deploy();
this.resolveSuccess();
return this.formatResult();
}
protected async deploy(): Promise<void> {
const waitFlag = this.flags.wait;
const waitDuration = waitFlag.minutes === -1 ? Duration.days(7) : waitFlag;
this.isAsync = waitDuration.quantity === 0;
this.isRest = this.isRestDeploy(this.flags.soapdeploy);
if (this.isAsync && (this.flags.coverageformatters || this.flags.junit)) {
this.warn(messages.getMessage('asyncCoverageJunitWarning'));
}
if (this.flags.validateddeployrequestid) {
this.deployResult = await this.deployRecentValidation(
this.flags.validateddeployrequestid,
this.org.getConnection()
);
return;
}
const deploymentOptions = this.flags.zipfile
? { zipPath: this.flags.zipfile }
: { mdapiPath: this.flags.deploydir };
const username = this.org.getUsername() as string;
// still here? we need to deploy a zip file then
const deploy = new MetadataApiDeploy({
usernameOrConnection: username,
...deploymentOptions,
apiOptions: {
// properties that will always have values
...{
purgeOnDelete: this.flags.purgeondelete ?? false,
ignoreWarnings: this.flags.ignorewarnings ?? false,
rollbackOnError: !this.flags.ignoreerrors ?? false,
checkOnly: this.flags.checkonly ?? false,
singlePackage: this.flags.singlepackage ?? false,
rest: this.isRest,
},
// if runTests is defaulted as 'NoTestRun' and deploying to prod, you'll get this error
// https://github.com/forcedotcom/cli/issues/1542
// add additional properties conditionally
...(this.flags.testlevel
? {
testLevel: this.flags.testlevel as TestLevel,
}
: {}),
...(this.flags.runtests ? { runTests: this.flags.runtests } : {}),
},
});
// eslint-disable-next-line @typescript-eslint/require-await
Lifecycle.getInstance().on('apiVersionDeploy', async (apiData: DeployVersionData) => {
this.log(deployMessages.getMessage('apiVersionMsgBasic', [username, apiData.apiVersion, apiData.webService]));
});
await deploy.start();
this.asyncDeployResult = deploy.id ? { id: deploy.id } : undefined;
this.updateDeployId(deploy.id);
if (!this.isAsync) {
if (!this.jsonEnabled()) {
const progressFormatter = env.getBoolean('SF_USE_PROGRESS_BAR', true)
? new DeployProgressBarFormatter()
: new DeployProgressStatusFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }));
progressFormatter.progress(deploy);
}
this.displayDeployId(deploy.id);
this.deployResult = await deploy.pollStatus({ frequency: Duration.milliseconds(500), timeout: waitDuration });
}
}
protected formatResult(): MdDeployResult | DeployCommandAsyncResult {
this.resultsDir = this.resolveOutputDir(
this.flags.coverageformatters ?? [],
this.flags.junit,
this.flags.resultsdir,
this.deployResult?.response?.id,
true
);
const formatterOptions: ResultFormatterOptions = {
concise: this.flags.concise ?? false,
verbose: this.flags.verbose ?? false,
username: this.org.getUsername(),
coverageOptions: getCoverageFormattersOptions(this.flags.coverageformatters),
junitTestResults: this.flags.junit,
resultsDir: this.resultsDir,
testsRan: (this.flags.testlevel ?? 'NoTestRun') !== 'NoTestRun',
};
const formatter = this.isAsync
? new MdDeployAsyncResultFormatter(
new Ux({ jsonEnabled: this.jsonEnabled() }),
formatterOptions,
this.asyncDeployResult as AsyncResult
)
: new MdDeployResultFormatter(new Ux({ jsonEnabled: this.jsonEnabled() }), formatterOptions, this.deployResult);
if (!this.isAsync) {
this.maybeCreateRequestedReports({
coverageformatters: this.flags.coverageformatters ?? [],
junit: this.flags.junit,
org: this.org,
});
}
// Only display results to console when JSON flag is unset.
if (!this.jsonEnabled()) {
formatter.display();
}
return formatter.getJson();
}
}