forked from forcedotcom/salesforcedx-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdigitalExperienceSiteGenerator.ts
More file actions
407 lines (371 loc) · 11.6 KB
/
Copy pathdigitalExperienceSiteGenerator.ts
File metadata and controls
407 lines (371 loc) · 11.6 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* Copyright (c) 2026, 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 * as crypto from 'node:crypto';
import * as path from 'path';
import { CreateUtil } from '../utils';
import { DigitalExperienceSiteOptions } from '../utils/types';
import { BaseGenerator } from './baseGenerator';
import { nls } from '../i18n';
const TEMPLATE_ROOT = 'digitalexperiencesite';
export default class DigitalExperienceSiteGenerator extends BaseGenerator<DigitalExperienceSiteOptions> {
private uuidCache: Record<string, string> = {};
constructor(options: DigitalExperienceSiteOptions) {
super(options);
}
public validateOptions(): void {
CreateUtil.checkInputs(this.options.template);
// site name allows anything, but we will strip it down for dev name
// url path prefix must be strictly alphanumeric, no "-" or "_", can start with a number
// empty string is allowed (for sites without a prefix)
const alphaRegExp = /^[a-zA-Z0-9]*$/;
if (!alphaRegExp.test(this.options.urlpathprefix)) {
throw new Error(
nls.localize('AlphaNumericValidationError', 'url-path-prefix')
);
}
}
public async generate(): Promise<void> {
const { template, sitename, urlpathprefix, adminemail } = this.options;
const siteDevName = this.toSiteDevName(sitename);
const picassoSiteDevName = this.toPicassoSiteDevName(siteDevName);
this.sourceRootWithPartialPath(path.join(TEMPLATE_ROOT, template));
await this.generateNetwork(
sitename,
siteDevName,
picassoSiteDevName,
urlpathprefix,
adminemail
);
await this.generateCustomSite(sitename, siteDevName, urlpathprefix);
await this.generateDigitalExperienceConfig(
sitename,
picassoSiteDevName,
urlpathprefix
);
const bundlePath = path.join(
this.outputdir,
'digitalExperiences',
'site',
picassoSiteDevName
);
await this.generateDEBMeta(bundlePath, picassoSiteDevName);
await this.generateDEBAppPage(bundlePath);
await this.generateDEBBrandingSet(bundlePath);
await this.generateDEBLanguageSettings(bundlePath);
await this.generateDEBMobilePublisherConfig(bundlePath);
await this.generateDEBRoutes(bundlePath);
await this.generateDEBSite(bundlePath, sitename, picassoSiteDevName);
await this.generateDEBTheme(bundlePath);
await this.generateDEBThemeLayouts(bundlePath);
await this.generateDEBViews(bundlePath);
}
private async generateNetwork(
siteName: string,
siteDevName: string,
picassoSiteDevName: string,
urlPathPrefix: string,
adminEmail: string
): Promise<void> {
const fileName = this.encodeForFileName(siteName);
await this.render(
this.templatePath('_network.xml'),
this.destinationPath(
path.join(this.outputdir, 'networks', `${fileName}.network-meta.xml`)
),
{ siteName, siteDevName, picassoSiteDevName, urlPathPrefix, adminEmail }
);
}
private async generateCustomSite(
siteName: string,
siteDevName: string,
urlPathPrefix: string
): Promise<void> {
await this.render(
this.templatePath('_customSite.xml'),
this.destinationPath(
path.join(this.outputdir, 'sites', `${siteDevName}.site-meta.xml`)
),
{ siteName, siteDevName, urlPathPrefix }
);
}
private async generateDigitalExperienceConfig(
siteName: string,
picassoSiteDevName: string,
urlPathPrefix: string
): Promise<void> {
await this.render(
this.templatePath('_digitalExperienceConfig.xml'),
this.destinationPath(
path.join(
this.outputdir,
'digitalExperienceConfigs',
`${picassoSiteDevName}.digitalExperienceConfig-meta.xml`
)
),
{ siteName, picassoSiteDevName, urlPathPrefix }
);
}
private async generateDEBMeta(
bundlePath: string,
picassoSiteDevName: string
): Promise<void> {
await this.render(
this.templatePath('_digitalExperience.xml'),
this.destinationPath(
path.join(
bundlePath,
`${picassoSiteDevName}.digitalExperience-meta.xml`
)
),
{ picassoSiteDevName }
);
}
private async generateDEBAppPage(bundlePath: string): Promise<void> {
const appPagePath = path.join(
bundlePath,
'sfdc_cms__appPage',
'mainAppPage'
);
await this.render(
this.templatePath('sfdc_cms__appPage', 'mainAppPage', 'content.json'),
this.destinationPath(path.join(appPagePath, 'content.json')),
{}
);
await this.render(
this.templatePath('sfdc_cms__appPage', 'mainAppPage', '_meta.json'),
this.destinationPath(path.join(appPagePath, '_meta.json')),
{}
);
}
private async generateDEBBrandingSet(bundlePath: string): Promise<void> {
const brandingPath = path.join(
bundlePath,
'sfdc_cms__brandingSet',
'Build_Your_Own_LWR'
);
await this.render(
this.templatePath(
'sfdc_cms__brandingSet',
'Build_Your_Own_LWR',
'content.json'
),
this.destinationPath(path.join(brandingPath, 'content.json')),
{}
);
await this.render(
this.templatePath(
'sfdc_cms__brandingSet',
'Build_Your_Own_LWR',
'_meta.json'
),
this.destinationPath(path.join(brandingPath, '_meta.json')),
{}
);
}
private async generateDEBLanguageSettings(bundlePath: string): Promise<void> {
const langPath = path.join(
bundlePath,
'sfdc_cms__languageSettings',
'languages'
);
await this.render(
this.templatePath(
'sfdc_cms__languageSettings',
'languages',
'content.json'
),
this.destinationPath(path.join(langPath, 'content.json')),
{}
);
await this.render(
this.templatePath(
'sfdc_cms__languageSettings',
'languages',
'_meta.json'
),
this.destinationPath(path.join(langPath, '_meta.json')),
{}
);
}
private async generateDEBMobilePublisherConfig(
bundlePath: string
): Promise<void> {
const mobileConfigPath = path.join(
bundlePath,
'sfdc_cms__mobilePublisherConfig',
'mobilePublisherConfig'
);
await this.render(
this.templatePath(
'sfdc_cms__mobilePublisherConfig',
'mobilePublisherConfig',
'content.json'
),
this.destinationPath(path.join(mobileConfigPath, 'content.json')),
{}
);
await this.render(
this.templatePath(
'sfdc_cms__mobilePublisherConfig',
'mobilePublisherConfig',
'_meta.json'
),
this.destinationPath(path.join(mobileConfigPath, '_meta.json')),
{}
);
}
private async generateDEBRoutes(bundlePath: string): Promise<void> {
const routes = [
'Check_Password',
'Error',
'Forgot_Password',
'Home',
'Login',
'News_Detail__c',
'Register',
'Service_Not_Available',
'Too_Many_Requests',
];
for (const route of routes) {
const routePath = path.join(bundlePath, 'sfdc_cms__route', route);
await this.render(
this.templatePath('sfdc_cms__route', route, 'content.json'),
this.destinationPath(path.join(routePath, 'content.json')),
{}
);
await this.render(
this.templatePath('sfdc_cms__route', route, '_meta.json'),
this.destinationPath(path.join(routePath, '_meta.json')),
{}
);
}
}
private async generateDEBSite(
bundlePath: string,
siteName: string,
picassoSiteDevName: string
): Promise<void> {
const urlName = siteName
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-') // keep only alphanumeric characters
.replace(/-$/, ''); // remove trailing hyphen
const sitePath = path.join(
bundlePath,
'sfdc_cms__site',
picassoSiteDevName
);
siteName = JSON.stringify(siteName) // escape special characters since this needs to be a JSON string
.slice(1, -1); // remove quotes added by JSON.stringify
await this.render(
this.templatePath('sfdc_cms__site', 'content.json'),
this.destinationPath(path.join(sitePath, 'content.json')),
{ siteName, urlName }
);
await this.render(
this.templatePath('sfdc_cms__site', '_meta.json'),
this.destinationPath(path.join(sitePath, '_meta.json')),
{ picassoSiteDevName }
);
}
private async generateDEBTheme(bundlePath: string): Promise<void> {
const themePath = path.join(
bundlePath,
'sfdc_cms__theme',
'Build_Your_Own_LWR'
);
await this.render(
this.templatePath(
'sfdc_cms__theme',
'Build_Your_Own_LWR',
'content.json'
),
this.destinationPath(path.join(themePath, 'content.json')),
{}
);
await this.render(
this.templatePath('sfdc_cms__theme', 'Build_Your_Own_LWR', '_meta.json'),
this.destinationPath(path.join(themePath, '_meta.json')),
{}
);
}
private async generateDEBThemeLayouts(bundlePath: string): Promise<void> {
const layouts = ['scopedHeaderAndFooter', 'snaThemeLayout'];
for (const layout of layouts) {
const layoutPath = path.join(bundlePath, 'sfdc_cms__themeLayout', layout);
await this.render(
this.templatePath('sfdc_cms__themeLayout', layout, 'content.json'),
this.destinationPath(path.join(layoutPath, 'content.json')),
{ uuid: this.generateUUID.bind(this, `themeLayout_${layout}`) }
);
await this.render(
this.templatePath('sfdc_cms__themeLayout', layout, '_meta.json'),
this.destinationPath(path.join(layoutPath, '_meta.json')),
{}
);
}
}
private async generateDEBViews(bundlePath: string): Promise<void> {
const views = [
'checkPasswordResetEmail',
'error',
'forgotPassword',
'home',
'login',
'newsDetail',
'register',
'serviceNotAvailable',
'tooManyRequests',
];
for (const view of views) {
const viewPath = path.join(bundlePath, 'sfdc_cms__view', view);
await this.render(
this.templatePath('sfdc_cms__view', view, 'content.json'),
this.destinationPath(path.join(viewPath, 'content.json')),
{ uuid: this.generateUUID.bind(this, `view_${view}`) }
);
await this.render(
this.templatePath('sfdc_cms__view', view, '_meta.json'),
this.destinationPath(path.join(viewPath, '_meta.json')),
{}
);
}
}
private generateUUID(namepsace: string, key: string | undefined): string {
if (!key) {
return crypto.randomUUID();
}
key = `${namepsace}:${key}`;
if (!this.uuidCache[key]) {
this.uuidCache[key] = crypto.randomUUID();
}
return this.uuidCache[key];
}
private toSiteDevName(sitename: string): string {
return sitename
.replace(/[^a-zA-Z0-9]+/g, '_') // remove non-alphanumeric characters
.replace(/_$/, '') // remove trailing underscore
.replace(/^([0-9])/, 'X$1'); // prefix with X if it starts with a number
}
private toPicassoSiteDevName(siteDevName: string): string {
return `${siteDevName}1`;
}
private encodeForFileName(str: string): string {
const charMap = {
'~': '%7E',
'!': '%21',
'.': '%2E',
"'": '%27',
'(': '%28',
')': '%29',
};
// encodeURIComponent is for URL, so we need additional steps to match filename
// encoding from the server
return encodeURIComponent(str)
.replace(/%20/g, ' ')
.replace(/[~!.'()]/g, (char) => charMap[char as keyof typeof charMap]);
}
}