-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathexport.ts
More file actions
273 lines (240 loc) · 8.37 KB
/
export.ts
File metadata and controls
273 lines (240 loc) · 8.37 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
272
273
/*
* 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 {Args, Flags, ux} from '@oclif/core';
import {JobCommand} from '@salesforce/b2c-tooling-sdk/cli';
import {
LibraryNode,
exportContent,
fetchContentLibrary,
type ContentExportResult,
} from '@salesforce/b2c-tooling-sdk/operations/content';
export default class ContentExport extends JobCommand<typeof ContentExport> {
static args = {
pages: Args.string({
description: 'Content IDs to export (pages, content assets, or components)',
required: true,
}),
};
static description = 'Export Page Designer pages with components and assets from a content library';
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> --library SharedLibrary homepage',
'<%= config.bin %> <%= command.id %> --library SharedLibrary homepage about-us',
'<%= config.bin %> <%= command.id %> --library SharedLibrary "hero-.*" --regex',
'<%= config.bin %> <%= command.id %> --library RefArch --site-library homepage -o ./export',
'<%= config.bin %> <%= command.id %> --library SharedLibrary homepage --json',
'<%= config.bin %> <%= command.id %> --library SharedLibrary homepage --dry-run',
];
static flags = {
...JobCommand.baseFlags,
library: Flags.string({
description: 'Library ID or site ID (also configurable via dw.json "content-library")',
}),
output: Flags.string({
char: 'o',
description: 'Output directory',
}),
'site-library': Flags.boolean({
description: 'Library is a site-private library',
default: false,
}),
'asset-query': Flags.string({
char: 'q',
description: 'JSON dot-paths for asset extraction',
multiple: true,
default: ['image.path'],
}),
regex: Flags.boolean({
char: 'r',
description: 'Treat page IDs as regular expressions',
default: false,
}),
folder: Flags.string({
description: 'Filter by folder classification',
multiple: true,
}),
offline: Flags.boolean({
description: 'Skip asset downloads',
default: false,
}),
'library-file': Flags.string({
description: 'Use a local library XML file instead of fetching from instance',
}),
'keep-orphans': Flags.boolean({
description: 'Include orphan components in export',
default: false,
}),
'show-tree': Flags.boolean({
description: 'Display tree structure of exported content',
default: true,
}),
timeout: Flags.integer({
description: 'Export job timeout in seconds',
}),
'dry-run': Flags.boolean({
description: 'Preview export without downloading assets or writing files',
default: false,
}),
};
// Allow multiple page arguments
static strict = false;
protected operations = {
exportContent,
fetchContentLibrary,
};
async run(): Promise<ContentExportResult> {
const {argv, flags} = await this.parse(ContentExport);
const pageIds = argv as string[];
const outputPath =
flags.output ??
`content-${new Date()
.toISOString()
.replaceAll(/[-:.TZ]/g, '')
.slice(0, 14)}`;
if (pageIds.length === 0) {
this.error('At least one content ID is required.');
}
const libraryId = flags.library ?? this.resolvedConfig.values.contentLibrary;
if (!libraryId) {
this.error('Library is required. Set via --library flag or "content-library" in dw.json.');
}
if (!flags['library-file']) {
this.requireOAuthCredentials();
}
const waitOptions = flags.timeout ? {timeoutSeconds: flags.timeout} : undefined;
if (flags['dry-run']) {
const {library} = await this.operations.fetchContentLibrary(this.instance, libraryId, {
libraryFile: flags['library-file'],
isSiteLibrary: flags['site-library'],
assetQuery: flags['asset-query'],
keepOrphans: flags['keep-orphans'],
waitOptions,
});
// Build matchers from content IDs
const matchers: Array<RegExp | string> = flags.regex ? pageIds.map((p) => new RegExp(p)) : pageIds;
function matchesId(id: string): boolean {
return matchers.some((m) => (m instanceof RegExp ? m.test(id) : id === m));
}
// Filter root children (pages/content) by ID and optionally by folder
library.filter((node) => {
if (node.type !== 'PAGE' && node.type !== 'CONTENT') {
return false;
}
if (!matchesId(node.id)) {
return false;
}
if (flags.folder && flags.folder.length > 0) {
const xmlData = node.xml;
if (!xmlData) return false;
const folderLinks = xmlData['folder-links'] as Array<Record<string, unknown>> | undefined;
const classificationLink = folderLinks?.find(
(l) => (l['classification-link'] as Array<Record<string, unknown>>)?.[0],
);
const linkEl = (
classificationLink?.['classification-link'] as Array<Record<string, unknown>> | undefined
)?.[0] as Record<string, unknown> | undefined;
const folderId = (linkEl?.$ as Record<string, string> | undefined)?.['folder-id'];
if (!folderId || !flags.folder.includes(folderId)) {
return false;
}
}
return true;
});
// Promote matching components to root level
const allNodes = [...library.nodes({traverseHidden: true, callbackHidden: true})];
for (const node of allNodes) {
if (node.type === 'COMPONENT' && matchesId(node.id)) {
library.promoteToRoot(node as LibraryNode);
}
}
// Count pages, content, and components
let pageCount = 0;
let contentCount = 0;
let componentCount = 0;
const assetPaths: string[] = [];
library.traverse(
(node) => {
switch (node.type) {
case 'COMPONENT': {
componentCount++;
break;
}
case 'CONTENT': {
contentCount++;
break;
}
case 'PAGE': {
pageCount++;
break;
}
case 'STATIC': {
assetPaths.push(node.id);
break;
}
}
},
{traverseHidden: false},
);
if (flags['show-tree']) {
ux.stdout(library.getTreeString({colorize: ux.colorize}));
}
this.log(formatSummary('Dry run', pageCount, contentCount, componentCount, assetPaths.length, outputPath));
return {
library,
outputPath,
downloadedAssets: [],
failedAssets: [],
pageCount,
contentCount,
componentCount,
};
}
const result = await this.operations.exportContent(this.instance, pageIds, libraryId, outputPath, {
isSiteLibrary: flags['site-library'],
assetQuery: flags['asset-query'],
libraryFile: flags['library-file'],
offline: flags.offline,
folders: flags.folder,
regex: flags.regex,
keepOrphans: flags['keep-orphans'],
waitOptions,
onAssetProgress: (asset, index, total, success) => {
this.log(` [${index + 1}/${total}] ${asset} ${success ? '✓' : '✗'}`);
},
});
if (flags['show-tree']) {
ux.stdout(result.library.getTreeString({colorize: ux.colorize}));
}
this.log(
formatSummary(
'Exported',
result.pageCount,
result.contentCount,
result.componentCount,
result.downloadedAssets.length,
result.outputPath,
),
);
return result;
}
}
const pluralS = (n: number) => (n === 1 ? '' : 's');
function formatSummary(
prefix: string,
pages: number,
content: number,
components: number,
assets: number,
outputPath: string,
): string {
const parts: string[] = [];
if (pages > 0) parts.push(`${pages} page${pluralS(pages)}`);
if (content > 0) parts.push(`${content} content asset${pluralS(content)}`);
if (components > 0) parts.push(`${components} component${pluralS(components)}`);
if (assets > 0) parts.push(`${assets} static asset${pluralS(assets)}`);
const suffix = prefix === 'Dry run' ? `would be exported to ${outputPath}` : `to ${outputPath}`;
return parts.length > 0 ? `${prefix}: ${parts.join(', ')} ${suffix}` : `${prefix}: nothing to export`;
}