-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdeploy.ts
More file actions
179 lines (156 loc) · 5.65 KB
/
deploy.ts
File metadata and controls
179 lines (156 loc) · 5.65 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
/*
* 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,
reloadCodeVersion,
type DeployResult,
} from '@salesforce/b2c-tooling-sdk/operations/code';
import {CartridgeCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {t} from '../../i18n/index.js';
export default class CodeDeploy extends CartridgeCommand<typeof CodeDeploy> {
static args = {
...CartridgeCommand.baseArgs,
};
static description = t('commands.code.deploy.description', 'Deploy cartridges to a B2C Commerce instance');
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 %> --reload',
'<%= config.bin %> <%= command.id %> --delete --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,
reload: Flags.boolean({
char: 'r',
description: 'Reload (re-activate) code version after deploy',
default: false,
}),
delete: Flags.boolean({
description: 'Delete existing cartridges before upload',
default: false,
}),
};
async run(): Promise<DeployResult> {
this.requireWebDavCredentials();
this.requireOAuthCredentials();
const hostname = this.resolvedConfig.values.hostname!;
let version = this.resolvedConfig.values.codeVersion;
// 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 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,
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,
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.logger?.info(
{path: this.cartridgePath, server: hostname, codeVersion: version},
t('commands.code.deploy.deploying', 'Deploying {{path}} to {{hostname}} ({{version}})', {
path: this.cartridgePath,
hostname,
version,
}),
);
// Log found cartridges
for (const c of cartridges) {
this.logger?.debug({cartridgeName: c.name, path: c.src}, ` ${c.name}`);
}
try {
// Optionally delete existing cartridges first
if (this.flags.delete) {
await deleteCartridges(this.instance, cartridges);
}
// Upload cartridges
await uploadCartridges(this.instance, cartridges);
// Optionally reload code version
let reloaded = false;
if (this.flags.reload) {
try {
await reloadCodeVersion(this.instance, version);
reloaded = true;
} catch (error) {
this.logger?.debug(`Could not reload code version: ${error instanceof Error ? error.message : error}`);
}
}
const result: DeployResult = {
cartridges,
codeVersion: version,
reloaded,
};
this.logger?.info(
{codeVersion: result.codeVersion, cartridgeCount: result.cartridges.length},
t('commands.code.deploy.summary', 'Deployed {{count}} cartridge(s) to {{codeVersion}}', {
count: result.cartridges.length,
codeVersion: result.codeVersion,
}),
);
if (result.reloaded) {
this.log(t('commands.code.deploy.reloaded', 'Code version reloaded'));
}
// 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;
}
}
}