generated from salesforcecli/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 18
/
resume.ts
151 lines (133 loc) · 6.4 KB
/
resume.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
/*
* Copyright (c) 2022, 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 ansis from 'ansis';
import { EnvironmentVariable, Messages, Org, SfError } from '@salesforce/core';
import { SfCommand, toHelpSection, Flags } from '@salesforce/sf-plugins-core';
import { DeployResult, MetadataApiDeploy } from '@salesforce/source-deploy-retrieve';
import { Duration } from '@salesforce/kit';
import { DeployResultFormatter } from '../../../formatters/deployResultFormatter.js';
import { DeployProgress } from '../../../utils/progressBar.js';
import { API, DeployResultJson } from '../../../utils/types.js';
import { buildComponentSet, determineExitCode, executeDeploy, isNotResumable } from '../../../utils/deploy.js';
import { DeployCache } from '../../../utils/deployCache.js';
import { DEPLOY_STATUS_CODES_DESCRIPTIONS } from '../../../utils/errorCodes.js';
import { coverageFormattersFlag } from '../../../utils/flags.js';
Messages.importMessagesDirectoryFromMetaUrl(import.meta.url);
const messages = Messages.loadMessages('@salesforce/plugin-deploy-retrieve', 'deploy.metadata.resume');
const testFlags = 'Test';
export default class DeployMetadataResume extends SfCommand<DeployResultJson> {
public static readonly description = messages.getMessage('description');
public static readonly summary = messages.getMessage('summary');
public static readonly examples = messages.getMessages('examples');
public static readonly aliases = ['deploy:metadata:resume'];
public static readonly deprecateAliases = true;
public static readonly flags = {
concise: Flags.boolean({
summary: messages.getMessage('flags.concise.summary'),
exclusive: ['verbose'],
}),
'job-id': Flags.salesforceId({
char: 'i',
startsWith: '0Af',
length: 'both',
description: messages.getMessage('flags.job-id.description'),
summary: messages.getMessage('flags.job-id.summary'),
exactlyOne: ['use-most-recent', 'job-id'],
}),
'use-most-recent': Flags.boolean({
char: 'r',
description: messages.getMessage('flags.use-most-recent.description'),
summary: messages.getMessage('flags.use-most-recent.summary'),
exactlyOne: ['use-most-recent', 'job-id'],
}),
verbose: Flags.boolean({
summary: messages.getMessage('flags.verbose.summary'),
exclusive: ['concise'],
}),
// we want this to allow undefined so that we can use the default value from the cache
// eslint-disable-next-line sf-plugin/flag-min-max-default
wait: Flags.duration({
char: 'w',
summary: messages.getMessage('flags.wait.summary'),
description: messages.getMessage('flags.wait.description'),
unit: 'minutes',
helpValue: '<minutes>',
min: 1,
}),
'coverage-formatters': coverageFormattersFlag({ helpGroup: testFlags }),
junit: Flags.boolean({
summary: messages.getMessage('flags.junit.summary'),
helpGroup: testFlags,
}),
'results-dir': Flags.directory({
relationships: [{ type: 'some', flags: ['coverage-formatters', 'junit'] }],
summary: messages.getMessage('flags.results-dir.summary'),
helpGroup: testFlags,
}),
};
public static envVariablesSection = toHelpSection('ENVIRONMENT VARIABLES', EnvironmentVariable.SF_USE_PROGRESS_BAR);
public static errorCodes = toHelpSection('ERROR CODES', DEPLOY_STATUS_CODES_DESCRIPTIONS);
public async run(): Promise<DeployResultJson> {
const [{ flags }, cache] = await Promise.all([this.parse(DeployMetadataResume), DeployCache.create()]);
const jobId = cache.resolveLatest(flags['use-most-recent'], flags['job-id'], true);
// if it was async before, then it should not be async now.
const deployOpts = { ...cache.maybeGet(jobId), async: false };
let result: DeployResult;
// If we already have a status from cache that is not resumable, display a warning and the deploy result.
if (isNotResumable(deployOpts.status)) {
this.warn(messages.getMessage('warning.DeployNotResumable', [jobId, deployOpts.status]));
const org = await Org.create({ aliasOrUsername: deployOpts['target-org'] });
const componentSet = await buildComponentSet({ ...deployOpts, wait: Duration.seconds(0) });
const mdapiDeploy = new MetadataApiDeploy({
// setting an API version here won't matter since we're just checking deploy status
// eslint-disable-next-line sf-plugin/get-connection-with-version
usernameOrConnection: org.getConnection(),
id: jobId,
components: componentSet,
apiOptions: {
rest: deployOpts.api === API['REST'],
},
});
const deployStatus = await mdapiDeploy.checkStatus();
result = new DeployResult(deployStatus, componentSet);
} else {
const wait = flags.wait ?? Duration.minutes(deployOpts.wait ?? 33);
const { deploy } = await executeDeploy(
// there will always be conflicts on a resume if anything deployed--the changes on the server are not synced to local
{
...deployOpts,
wait,
'dry-run': false,
'ignore-conflicts': true,
// TODO: isMdapi is generated from 'metadata-dir' flag, but we don't have that flag here
// change the cache value to actually cache the metadata-dir, and if there's a value, it isMdapi
// deployCache~L38, so to tell the executeDeploy method it's ok to not have a project, we spoof a metadata-dir
// in deploy~L140, it checks the if the id is present, so this metadata-dir value is never _really_ used
'metadata-dir': deployOpts.isMdapi ? { type: 'file', path: 'testing' } : undefined,
},
this.project,
jobId
);
this.log(`Deploy ID: ${ansis.bold(jobId)}`);
new DeployProgress(deploy, this.jsonEnabled()).start();
result = await deploy.pollStatus(500, wait.seconds);
if (!deploy.id) {
throw new SfError('The deploy id is not available.');
}
cache.update(deploy.id, { status: result.response.status });
await cache.write();
}
process.exitCode = determineExitCode(result);
const formatter = new DeployResultFormatter(result, {
...flags,
verbose: deployOpts.verbose,
concise: deployOpts.concise,
});
if (!this.jsonEnabled()) formatter.display();
return formatter.getJson();
}
}