forked from forcedotcom/salesforcedx-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflexipageGenerator.ts
More file actions
169 lines (151 loc) · 4.96 KB
/
Copy pathflexipageGenerator.ts
File metadata and controls
169 lines (151 loc) · 4.96 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
/*
* Copyright (c) 2020, 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 fs from 'fs';
import { readdir } from 'node:fs/promises';
import * as path from 'path';
import { CreateUtil } from '../utils';
import { FlexipageOptions } from '../utils/types';
import {
BaseGenerator,
setCustomTemplatesRootPathOrGitRepo,
} from './baseGenerator';
import { nls } from '../i18n';
const VALID_TEMPLATES = ['RecordPage', 'AppPage', 'HomePage'] as const;
const MAX_SECONDARY_FIELDS = 11;
export default class FlexipageGenerator extends BaseGenerator<FlexipageOptions> {
constructor(options: FlexipageOptions) {
super(options);
}
public validateOptions(): void {
CreateUtil.checkInputs(this.options.flexipagename);
CreateUtil.checkInputs(this.options.template);
if (!VALID_TEMPLATES.includes(this.options.template as any)) {
throw new Error(
nls.localize('InvalidFlexipageTemplate', [
this.options.template,
VALID_TEMPLATES.join(', '),
])
);
}
if (this.options.template === 'RecordPage' && !this.options.entityName) {
throw new Error(nls.localize('RecordPageRequiresEntityName'));
}
// Validate secondary fields limit (dynamicHighlights supports max 11 secondary fields)
const secondaryFieldsCount = this.options.secondaryFields?.length ?? 0;
if (secondaryFieldsCount > MAX_SECONDARY_FIELDS) {
throw new Error(
nls.localize('TooManySecondaryFields', [
secondaryFieldsCount.toString(),
MAX_SECONDARY_FIELDS.toString(),
])
);
}
// Ensure output directory includes 'flexipages' folder
if (!this.options.internal && !this.outputdir.includes('flexipages')) {
this.outputdir = path.join(this.outputdir, 'flexipages');
}
}
public async generate(): Promise<void> {
const {
template,
flexipagename,
masterlabel,
description,
entityName,
primaryField,
secondaryFields = [],
detailFields = [],
flexipageTemplatesGitRepo,
forceLoadingRemoteRepo,
} = this.options;
// Set up custom templates if provided
let customTemplatesRootPath: string | undefined;
if (flexipageTemplatesGitRepo) {
customTemplatesRootPath = await setCustomTemplatesRootPathOrGitRepo(
flexipageTemplatesGitRepo,
forceLoadingRemoteRepo
);
}
// Determine template root path
let templateRootPath: string;
if (customTemplatesRootPath) {
templateRootPath = path.join(
customTemplatesRootPath,
'flexipage',
template
);
if (!fs.existsSync(templateRootPath)) {
throw new Error(
nls.localize('MissingFlexipageTemplate', [
template,
customTemplatesRootPath,
])
);
}
} else {
// Use built-in templates
this.sourceRootWithPartialPath('flexipage');
templateRootPath = this.templatePath(template);
}
// Prepare EJS template variables
const templateVars = {
flexipagename,
masterlabel: masterlabel || flexipagename,
description: description || '',
apiVersion: this.apiversion,
entityName: entityName || '',
primaryField: primaryField || '',
secondaryFields: Array.isArray(secondaryFields) ? secondaryFields : [],
detailFields: Array.isArray(detailFields) ? detailFields : [],
};
// Recursively process template directory
await this.generateFlexipageFromTemplate(
templateRootPath,
this.outputdir,
templateVars
);
}
/**
* Recursively process template directory and generate files
*/
private async generateFlexipageFromTemplate(
sourceDir: string,
destDir: string,
templateVars: Record<string, unknown>
): Promise<void> {
const entries = await readdir(sourceDir, { withFileTypes: true });
for (const entry of entries) {
const sourcePath = path.join(sourceDir, entry.name);
let destName = entry.name;
// Replace _flexipage placeholder with actual flexipage name
if (destName.includes('_flexipage')) {
destName = destName.replace(
/_flexipage/g,
templateVars.flexipagename as string
);
}
const destPath = path.join(destDir, destName);
if (entry.isDirectory()) {
// Recursively process subdirectories
await this.generateFlexipageFromTemplate(
sourcePath,
destPath,
templateVars
);
} else if (entry.isFile() && this.isTemplateFile(entry.name)) {
// Render template files only (skip non-template files)
await this.render(sourcePath, destPath, templateVars);
}
}
}
/**
* Check if file should be rendered as EJS template
*/
private isTemplateFile(filename: string): boolean {
return filename.endsWith('.flexipage-meta.xml');
}
}