forked from forcedotcom/salesforcedx-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuiBundleGenerator.ts
More file actions
177 lines (154 loc) · 5.31 KB
/
Copy pathuiBundleGenerator.ts
File metadata and controls
177 lines (154 loc) · 5.31 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
/*
* Copyright (c) 2025, 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 { CreateUtil } from '../utils';
import { UI_BUNDLES_DIR } from '../utils/constants';
import { UIBundleOptions } from '../utils/types';
import { BaseGenerator } from './baseGenerator';
export default class UIBundleGenerator extends BaseGenerator<UIBundleOptions> {
public validateOptions(): void {
CreateUtil.checkInputs(this.options.bundlename);
CreateUtil.checkInputs(this.options.template);
// Ensure output directory includes 'uiBundles' folder
if (!this.options.internal) {
const fileparts = path
.resolve(this.outputdir)
.split(path.sep)
.filter(Boolean);
const endsWithUiBundles =
fileparts[fileparts.length - 1] === UI_BUNDLES_DIR;
if (!endsWithUiBundles) {
this.outputdir = path.join(this.outputdir, UI_BUNDLES_DIR);
}
}
}
public async generate(): Promise<void> {
const { bundlename } = this.options;
const template = this.options.template.toLowerCase();
const masterLabel =
this.options.masterlabel || camelCaseToTitleCase(bundlename);
const bundleDir = path.join(this.outputdir, bundlename);
switch (template) {
case 'reactbasic':
await this.generateReactBasic(bundleDir, bundlename, masterLabel);
break;
case 'angularbasic':
await this.generateAngularBasic(bundleDir, bundlename, masterLabel);
break;
default:
await this.generateDefault(bundleDir, bundlename, masterLabel);
}
}
private async generateDefault(
bundleDir: string,
bundlename: string,
masterLabel: string
): Promise<void> {
this.sourceRootWithPartialPath(path.join('uiBundles', 'webappbasic'));
await this.render(
this.templatePath('_uibundle.uibundle-meta.xml'),
this.destinationPath(
path.join(bundleDir, `${bundlename}.uibundle-meta.xml`)
),
{ apiVersion: this.apiversion, masterLabel }
);
const templatePath = this.sourceRoot();
await this.copyDirectoryRecursive(
templatePath,
bundleDir,
new Set(['_uibundle.uibundle-meta.xml'])
);
}
private async generateReactBasic(
bundleDir: string,
bundlename: string,
masterLabel: string
): Promise<void> {
this.sourceRootWithPartialPath(path.join('uiBundles', 'reactbasic'));
await this.render(
this.templatePath('_uibundle.uibundle-meta.xml'),
this.destinationPath(
path.join(bundleDir, `${bundlename}.uibundle-meta.xml`)
),
{ apiVersion: this.apiversion, masterLabel }
);
await this.render(
this.templatePath('package.json'),
this.destinationPath(path.join(bundleDir, 'package.json')),
{ bundlename }
);
const templatePath = this.sourceRoot();
await this.copyDirectoryRecursive(
templatePath,
bundleDir,
new Set(['_uibundle.uibundle-meta.xml', 'package.json'])
);
}
private async generateAngularBasic(
bundleDir: string,
bundlename: string,
masterLabel: string
): Promise<void> {
this.sourceRootWithPartialPath(path.join('uiBundles', 'angularbasic'));
await this.render(
this.templatePath('_uibundle.uibundle-meta.xml'),
this.destinationPath(
path.join(bundleDir, `${bundlename}.uibundle-meta.xml`)
),
{ apiVersion: this.apiversion, masterLabel }
);
await this.render(
this.templatePath('package.json'),
this.destinationPath(path.join(bundleDir, 'package.json')),
{ bundlename }
);
await this.render(
this.templatePath('angular.json'),
this.destinationPath(path.join(bundleDir, 'angular.json')),
{ bundlename }
);
const templatePath = this.sourceRoot();
await this.copyDirectoryRecursive(
templatePath,
bundleDir,
new Set(['_uibundle.uibundle-meta.xml', 'package.json', 'angular.json'])
);
}
private async copyDirectoryRecursive(
sourceDir: string,
destDir: string,
excludeFiles: ReadonlySet<string> = new Set()
): Promise<void> {
if (!this._fs.existsSync(sourceDir)) {
return;
}
await this._fs.promises.mkdir(destDir, { recursive: true });
const entries = this._fs.readdirSync(sourceDir, { withFileTypes: true });
for (const entry of entries) {
const sourcePath = path.join(sourceDir, entry.name);
const destPath = path.join(destDir, entry.name);
// Skip template files that were rendered
if (excludeFiles.has(entry.name)) {
continue;
}
if (entry.isDirectory()) {
await this.copyDirectoryRecursive(sourcePath, destPath, excludeFiles);
} else if (!this._fs.existsSync(destPath)) {
// Copy file and track it
const content = await this._fs.promises.readFile(sourcePath);
await this._fs.promises.mkdir(path.dirname(destPath), {
recursive: true,
});
await this._fs.promises.writeFile(destPath, content as Uint8Array);
// Register the created file
const relativePath = path.relative(this._cwd, destPath);
this.changes.created.push(relativePath);
}
}
}
}