-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathlist.ts
More file actions
165 lines (138 loc) · 4.48 KB
/
list.ts
File metadata and controls
165 lines (138 loc) · 4.48 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
/*
* 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 {ux} from '@oclif/core';
import {JobCommand, createTable, type ColumnDef} from '@salesforce/b2c-tooling-sdk/cli';
import {fetchContentLibrary} from '@salesforce/b2c-tooling-sdk/operations/content';
interface ContentListItem {
id: string;
type: string;
typeId: string;
children: number;
}
const COLUMNS: Record<string, ColumnDef<ContentListItem>> = {
id: {
header: 'ID',
get: (item) => item.id,
},
type: {
header: 'TYPE',
get: (item) => item.type,
},
typeId: {
header: 'TYPE ID',
get: (item) => item.typeId,
},
children: {
header: 'CHILDREN',
get: (item) => String(item.children),
},
};
const DEFAULT_COLUMNS = ['id', 'type', 'typeId', 'children'];
const TYPE_MAP: Record<string, string> = {
page: 'PAGE',
content: 'CONTENT',
component: 'COMPONENT',
};
export default class ContentList extends JobCommand<typeof ContentList> {
static description = 'List pages and content in a content library';
static enableJsonFlag = true;
static examples = [
'<%= config.bin %> <%= command.id %> --library SharedLibrary',
'<%= config.bin %> <%= command.id %> --library SharedLibrary --tree',
'<%= config.bin %> <%= command.id %> --library RefArch --site-library --type page',
];
static flags = {
...JobCommand.baseFlags,
library: Flags.string({
description: 'Library ID or site ID (also configurable via dw.json "content-library")',
}),
'site-library': Flags.boolean({
description: 'Site-private library',
default: false,
}),
'library-file': Flags.string({
description: 'Local XML file',
}),
type: Flags.string({
description: 'Filter by node type',
options: ['page', 'content', 'component'],
}),
components: Flags.boolean({
description: 'Include components in output',
default: false,
}),
tree: Flags.boolean({
description: 'Show tree structure',
default: false,
}),
timeout: Flags.integer({
description: 'Job timeout in seconds',
}),
};
protected operations = {
fetchContentLibrary,
};
async run(): Promise<{data: ContentListItem[]}> {
const {flags} = await this.parse(ContentList);
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;
const instance = flags['library-file'] ? (null as unknown as typeof this.instance) : this.instance;
const {library} = await this.operations.fetchContentLibrary(instance, libraryId, {
libraryFile: flags['library-file'],
isSiteLibrary: flags['site-library'],
waitOptions,
});
const typeFilter = flags.type ? TYPE_MAP[flags.type] : undefined;
const items: ContentListItem[] = [];
function collectItems(nodes: typeof library.tree.children, includeComponents: boolean): void {
for (const child of nodes) {
// Skip static asset nodes in table view
if (child.type === 'STATIC') {
continue;
}
// Skip components unless --components is set
if (child.type === 'COMPONENT' && !includeComponents) {
continue;
}
// Apply type filter
const matchesType = !typeFilter || child.type === typeFilter;
if (matchesType) {
items.push({
id: child.id,
type: child.type,
typeId: child.typeId ?? '',
children: child.children.length,
});
}
// Recurse into children when --components is set
if (includeComponents && child.children.length > 0) {
collectItems(child.children, true);
}
}
}
collectItems(library.tree.children, flags.components);
if (flags.tree) {
ux.stdout(library.getTreeString({colorize: ux.colorize}));
return {data: items};
}
if (this.jsonEnabled()) {
return {data: items};
}
if (items.length === 0) {
ux.stdout('No content found.');
return {data: items};
}
createTable(COLUMNS).render(items, DEFAULT_COLUMNS);
return {data: items};
}
}