forked from forcedotcom/salesforcedx-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuiEmbeddingGenerator.ts
More file actions
125 lines (109 loc) · 3.58 KB
/
Copy pathuiEmbeddingGenerator.ts
File metadata and controls
125 lines (109 loc) · 3.58 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
/*
* 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 { camelCaseToTitleCase } from '@salesforce/kit';
import * as path from 'path';
import { nls } from '../i18n';
import { CreateUtil } from '../utils';
import {
isAllowedUIEmbeddingSrcUrl,
UI_EMBEDDING_SANDBOX_TOKENS,
} from '../utils/uiEmbedding';
import { UIEmbeddingOptions } from '../utils/types';
import { BaseGenerator } from './baseGenerator';
const VALID_SANDBOX_TOKENS: ReadonlySet<string> = new Set(
UI_EMBEDDING_SANDBOX_TOKENS
);
export default class UIEmbeddingGenerator extends BaseGenerator<UIEmbeddingOptions> {
public validateOptions(): void {
CreateUtil.checkInputs(this.options.componentname);
const fileparts = path.resolve(this.outputdir).split(path.sep);
if (!this.options.internal && !fileparts.includes('lwc')) {
throw new Error(nls.localize('MissingLWCDir'));
}
if (!isAllowedUIEmbeddingSrcUrl(this.options.src)) {
throw new Error(nls.localize('InvalidUIEmbeddingSrcUrl'));
}
if (this.options.src.includes("'")) {
throw new Error(nls.localize('InvalidUIEmbeddingSrcChar'));
}
if (!this.options.shellTitle || !this.options.shellTitle.trim()) {
throw new Error(nls.localize('MissingUIEmbeddingShellTitle'));
}
if (this.options.shellTitle.includes('"')) {
throw new Error(nls.localize('InvalidUIEmbeddingShellTitleChar'));
}
const tokens = this.options.sandbox.split(/\s+/).filter(Boolean);
const invalid = tokens.filter((t) => !VALID_SANDBOX_TOKENS.has(t));
if (invalid.length) {
throw new Error(
nls.localize('InvalidUIEmbeddingSandboxToken', [
invalid.join(', '),
[...VALID_SANDBOX_TOKENS].join(', '),
])
);
}
}
public async generate(): Promise<void> {
const { componentname, src, sandbox, shellTitle, internal } = this.options;
const pascalCaseComponentName = `${componentname
.substring(0, 1)
.toUpperCase()}${componentname.substring(1)}`;
const camelCaseComponentName = `${componentname
.substring(0, 1)
.toLowerCase()}${componentname.substring(1)}`;
this.sourceRootWithPartialPath(path.join('uiembedding', 'default'));
await this.render(
this.templatePath('default.html'),
this.destinationPath(
path.join(
this.outputdir,
camelCaseComponentName,
`${camelCaseComponentName}.html`
)
),
{ sandbox, shellTitle }
);
await this.render(
this.templatePath('default.js'),
this.destinationPath(
path.join(
this.outputdir,
camelCaseComponentName,
`${camelCaseComponentName}.js`
)
),
{ pascalCaseComponentName, src }
);
await this.render(
this.templatePath('default.css'),
this.destinationPath(
path.join(
this.outputdir,
camelCaseComponentName,
`${camelCaseComponentName}.css`
)
),
{}
);
if (!internal) {
const masterLabel = camelCaseToTitleCase(componentname)
.replace(/</g, '<')
.replace(/>/g, '>');
await this.render(
this.templatePath('default.js-meta.xml'),
this.destinationPath(
path.join(
this.outputdir,
camelCaseComponentName,
`${camelCaseComponentName}.js-meta.xml`
)
),
{ apiVersion: this.apiversion, masterLabel }
);
}
}
}