-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdeploy.ts
More file actions
271 lines (244 loc) · 9.07 KB
/
deploy.ts
File metadata and controls
271 lines (244 loc) · 9.07 KB
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* Copyright (c) 2025, Salesforce, Inc.
* SPDX-License-Identifier: Apache-2
* For full license text, see the license.txt file in the repo root or http://www.apache.org/licenses/LICENSE-2.0
*/
import {Flags} from '@oclif/core';
import {
uploadCartridges,
deleteCartridges,
getActiveCodeVersion,
activateCodeVersion,
reloadCodeVersion,
type DeployResult,
} from '@salesforce/b2c-tooling-sdk/operations/code';
import {CartridgeCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {t, withDocs} from '../../i18n/index.js';
export default class CodeDeploy extends CartridgeCommand<typeof CodeDeploy> {
static hiddenAliases = ['code:deploy'];
static args = {
...CartridgeCommand.baseArgs,
};
static description = withDocs(
t('commands.code.deploy.description', 'Deploy cartridges to a B2C Commerce instance'),
'/cli/code.html#b2c-code-deploy',
);
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %>',
'<%= config.bin %> <%= command.id %> ./my-cartridges',
'<%= config.bin %> <%= command.id %> --server my-sandbox.demandware.net --code-version v1',
'<%= config.bin %> <%= command.id %> --activate',
'<%= config.bin %> <%= command.id %> --delete --activate',
'<%= config.bin %> <%= command.id %> --reload',
'<%= config.bin %> <%= command.id %> -c app_storefront_base -c plugin_applepay',
'<%= config.bin %> <%= command.id %> -x test_cartridge',
];
static flags = {
...CartridgeCommand.baseFlags,
...CartridgeCommand.cartridgeFlags,
activate: Flags.boolean({
char: 'a',
description: 'Activate code version after deploy',
default: false,
exclusive: ['reload'],
}),
reload: Flags.boolean({
char: 'r',
description: 'Reload (toggle activation to force reload) code version after deploy',
default: false,
exclusive: ['activate'],
}),
delete: Flags.boolean({
description: 'Delete existing cartridges before upload',
default: false,
}),
};
protected operations = {
uploadCartridges,
deleteCartridges,
getActiveCodeVersion,
activateCodeVersion,
reloadCodeVersion,
};
async run(): Promise<DeployResult> {
this.requireWebDavCredentials();
const hostname = this.resolvedConfig.values.hostname!;
let version = this.resolvedConfig.values.codeVersion;
// OAuth is required if:
// 1. No code version specified (need to auto-discover via OCAPI)
// 2. --activate or --reload flag is set (need to call OCAPI)
const needsOAuth = !version || this.flags.activate || this.flags.reload;
if (needsOAuth && !this.hasOAuthCredentials()) {
const reason = version
? t(
'commands.code.deploy.oauthRequiredForActivate',
'The --activate/--reload flag requires OAuth credentials to manage the code version via OCAPI.',
)
: t(
'commands.code.deploy.oauthRequiredForDiscovery',
'No code version specified. OAuth credentials are required to auto-discover the active code version.',
);
this.error(
t(
'commands.code.deploy.oauthRequired',
'{{reason}}\n\nProvide --code-version to use basic auth only, or configure OAuth credentials.\nSee: https://salesforcecommercecloud.github.io/b2c-developer-tooling/guide/configuration.html',
{reason},
),
);
}
// If no code version specified, discover the active one
if (!version) {
this.warn(
t('commands.code.deploy.noCodeVersion', 'No code version specified, discovering active code version...'),
);
const activeVersion = await this.operations.getActiveCodeVersion(this.instance);
if (!activeVersion?.id) {
this.error(
t('commands.code.deploy.noActiveVersion', 'No active code version found. Specify one with --code-version.'),
);
}
version = activeVersion.id;
// Update the instance config so findAndDeployCartridges uses it
this.instance.config.codeVersion = version;
}
// Create lifecycle context
const context = this.createContext('code:deploy', {
cartridgePath: this.cartridgePath,
hostname,
codeVersion: version,
activate: this.flags.activate,
reload: this.flags.reload,
delete: this.flags.delete,
...this.cartridgeOptions,
});
// Run beforeOperation hooks - check for skip
const beforeResult = await this.runBeforeHooks(context);
if (beforeResult.skip) {
this.log(
t('commands.code.deploy.skipped', 'Deployment skipped: {{reason}}', {
reason: beforeResult.skipReason || 'skipped by plugin',
}),
);
return {
cartridges: [],
codeVersion: version,
activated: false,
reloaded: false,
};
}
// Find cartridges using providers (supports custom discovery plugins)
const cartridges = await this.findCartridgesWithProviders();
if (cartridges.length === 0) {
this.error(t('commands.code.deploy.noCartridges', 'No cartridges found in {{path}}', {path: this.cartridgePath}));
}
this.log(
t('commands.code.deploy.deploying', 'Deploying {{path}} to {{hostname}} ({{version}})...', {
path: this.cartridgePath,
hostname,
version,
}),
);
// Log found cartridges
for (const c of cartridges) {
this.log(` ${c.name} (${c.src})`);
}
// After safety evaluation passes, temporarily allow WebDAV DELETE operations
// that are part of the deploy flow (cleanup of temp zip, --delete cartridge removal).
const cleanupSafetyRule = this.safetyGuard.temporarilyAddRule({
method: 'DELETE',
path: '**/Cartridges/**',
action: 'allow',
});
try {
// Optionally delete existing cartridges first
if (this.flags.delete) {
this.log(t('commands.code.deploy.deleting', 'Deleting existing cartridges...'));
await this.operations.deleteCartridges(this.instance, cartridges);
}
// Upload cartridges
const uploadPhaseLabels = {
archiving: t('commands.code.deploy.archiving', 'Creating cartridge archive'),
uploading: t('commands.code.deploy.uploading', 'Uploading archive'),
unzipping: t('commands.code.deploy.unzipping', 'Unzipping on server'),
cleanup: t('commands.code.deploy.cleanup', 'Cleaning up'),
};
await this.operations.uploadCartridges(this.instance, cartridges, {
onProgress: (info) => {
if (this.jsonEnabled()) return;
const label = uploadPhaseLabels[info.phase];
if (info.elapsedSeconds === 0) {
this.log(` ${label}...`);
} else {
this.log(` ${label}... (${info.elapsedSeconds}s elapsed)`);
}
},
});
// Optionally activate or reload code version
let activated = false;
let reloaded = false;
try {
if (this.flags.activate) {
await this.operations.activateCodeVersion(this.instance, version);
activated = true;
} else if (this.flags.reload) {
await this.operations.reloadCodeVersion(this.instance, version);
activated = true;
reloaded = true;
}
} catch (error) {
const clientId = this.resolvedConfig.values.clientId ?? 'unknown';
this.error(
t(
'commands.code.deploy.activateFailed',
'Failed to activate code version "{{version}}": {{message}}\n\nEnsure your OCAPI client ({{clientId}}) is configured with Data API permissions.\nSee: https://salesforcecommercecloud.github.io/b2c-developer-tooling/guide/authentication.html#ocapi-configuration',
{version, message: error instanceof Error ? error.message : String(error), clientId},
),
);
}
const result: DeployResult = {
cartridges,
codeVersion: version,
activated,
reloaded,
};
this.log(
t(
'commands.code.deploy.summary',
'Deployed {{count}} cartridge(s) to version "{{codeVersion}}" successfully!',
{
count: result.cartridges.length,
codeVersion: result.codeVersion,
},
),
);
if (result.activated) {
this.log(
result.reloaded
? t('commands.code.deploy.reloaded', 'Code version reloaded')
: t('commands.code.deploy.activated', 'Code version activated'),
);
}
// Run afterOperation hooks with success
await this.runAfterHooks(context, {
success: true,
duration: Date.now() - context.startTime,
data: result,
});
return result;
} catch (error) {
// Run afterOperation hooks with failure
await this.runAfterHooks(context, {
success: false,
error: error instanceof Error ? error : new Error(String(error)),
duration: Date.now() - context.startTime,
});
if (error instanceof Error) {
this.error(t('commands.code.deploy.failed', 'Deployment failed: {{message}}', {message: error.message}));
}
throw error;
} finally {
cleanupSafetyRule();
}
}
}