generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.ts
208 lines (187 loc) · 8.16 KB
/
index.ts
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
import { join } from 'path';
import type { Editor } from 'mem-fs-editor';
import { render } from 'ejs';
import { generateCustomPage } from '@sap-ux/fe-fpm-writer';
import type { App, Package } from '@sap-ux/ui5-application-writer';
import { generate as generateUi5Project } from '@sap-ux/ui5-application-writer';
import { generate as addOdataService, OdataVersion, ServiceType } from '@sap-ux/odata-service-writer';
import { generateOPAFiles } from '@sap-ux/ui5-test-writer';
import { getPackageJsonTasks } from './packageConfig';
import cloneDeep from 'lodash/cloneDeep';
import type { FioriElementsApp, FPMSettings } from './types';
import { TemplateType } from './types';
import { validateApp, validateRequiredProperties } from './validate';
import { setAppDefaults, setDefaultTemplateSettings } from './data/defaults';
import {
type TemplateOptions,
TemplateTypeAttributes,
minSupportedUI5Version,
minSupportedUI5VersionV4
} from './data/templateAttributes';
import { changesPreviewToVersion, escapeFLPText } from './data/templateAttributes';
import { extendManifestJson } from './data/manifestSettings';
import semVer from 'semver';
import { UI5Config } from '@sap-ux/ui5-config';
export const V2_FE_TYPES_AVAILABLE = '1.108.0';
/**
* Get TypeScript Ignore Glob Pattern.
*
* @param feApp to generate the Fiori elements application
* @param coercedUI5Version the coerced UI5 version
* @returns ignore pattern
*/
function getTypeScriptIgnoreGlob<T extends {}>(feApp: FioriElementsApp<T>, coercedUI5Version: semVer.SemVer): string[] {
let ignore = [];
// isV2FETypesAvailable - Boolean to indicate if V2 Fiori Element types were available in the UI5 version
const isV2FETypesAvailable = feApp.ui5?.version ? semVer.gte(coercedUI5Version, V2_FE_TYPES_AVAILABLE) : false;
const tsIgnoreGlob = ['**/*.js'];
ignore = tsIgnoreGlob;
// Add local ui5.d.ts if types are missing in UI5 version for V2 Odata services
// OR template is OVP
if (feApp.service.version === OdataVersion.v2) {
if (isV2FETypesAvailable) {
ignore.push('**/ui5.d.ts');
} else {
// do nothing
}
} else if (feApp.template.type !== TemplateType.OverviewPage) {
ignore.push('**/ui5.d.ts');
}
return ignore;
}
/**
* Generate a UI5 application based on the specified Fiori Elements floorplan template.
*
* @param basePath - the absolute target path where the application will be generated
* @param data - configuration to generate the Fiori elements application
* @param fs - an optional reference to a mem-fs editor
* @returns Reference to a mem-fs-editor
*/
async function generate<T extends {}>(basePath: string, data: FioriElementsApp<T>, fs?: Editor): Promise<Editor> {
// Clone rather than modifying callers refs
const feApp: FioriElementsApp<T> = cloneDeep(data);
// Ensure input data contains at least the mandatory properties required for app generation
validateRequiredProperties(feApp);
setAppDefaults(feApp);
fs = await generateUi5Project(basePath, feApp, fs);
feApp.template.settings = setDefaultTemplateSettings(feApp.template, feApp.service.version);
// This is done after `generateUi5Project` since defaults are set if values are not provided
validateApp(feApp);
await addOdataService(basePath, feApp.service, fs);
const coercedUI5Version = semVer.coerce(feApp.ui5?.version)!;
const templateOptions: TemplateOptions = {
changesPreview: feApp.ui5?.version ? semVer.lt(coercedUI5Version, changesPreviewToVersion) : false,
changesLoader: feApp.service.version === OdataVersion.v2
};
// Add new files from templates e.g.
const rootTemplatesPath = join(__dirname, '..', 'templates');
// Add templates common to all template types
const jsIgnoreGlob = ['**/*.ts'];
let ignore = jsIgnoreGlob;
if (feApp.appOptions?.typescript === true) {
ignore = getTypeScriptIgnoreGlob(feApp, coercedUI5Version);
}
// Check if sap.ushell is already in the ui5Libs array
const ushellLib = 'sap.ushell';
const ui5Libs = Array.isArray(feApp.ui5?.ui5Libs) ? feApp.ui5.ui5Libs : [feApp.ui5?.ui5Libs];
if (!ui5Libs.includes(ushellLib)) {
ui5Libs.push(ushellLib);
}
fs.copyTpl(
join(rootTemplatesPath, 'common', 'add', '**/*.*'),
basePath,
{
...feApp,
ui5: { ...feApp.ui5, ui5Libs },
templateOptions,
escapeFLPText
},
undefined,
{
globOptions: { ignore, dot: true }
}
);
// Extend ui5-local.yaml
const ui5LocalConfigPath = join(basePath, 'ui5-local.yaml');
const ui5LocalConfig = await UI5Config.newInstance(fs.read(ui5LocalConfigPath));
ui5LocalConfig.addUI5Libs([ushellLib]);
fs.write(ui5LocalConfigPath, ui5LocalConfig.toString());
// Extend common files
const packagePath = join(basePath, 'package.json');
// Extend package.json
fs.extendJSON(
packagePath,
JSON.parse(render(fs.read(join(rootTemplatesPath, 'common', 'extend', 'package.json')), feApp, {}))
);
// Special handling for FPM because it is not based on template files but used the fpm writer
if (feApp.template.type === TemplateType.FlexibleProgrammingModel) {
const config: FPMSettings = feApp.template.settings as unknown as FPMSettings;
generateCustomPage(
basePath,
{
entity: config.entityConfig.mainEntityName,
name: config.pageName,
minUI5Version: feApp.ui5?.minUI5Version,
typescript: feApp.appOptions?.typescript
},
fs
);
// Updating ui5-local for V4 FPM specific libs
if (feApp.service.version === OdataVersion.v4) {
const ui5LocalConfigPath = join(basePath, 'ui5-local.yaml');
const ui5LocalConfig = await UI5Config.newInstance(fs.read(ui5LocalConfigPath));
const ui5Libs = ['sap.fe.templates'];
ui5LocalConfig.addUI5Libs(ui5Libs);
fs.write(ui5LocalConfigPath, ui5LocalConfig.toString());
}
} else {
// Copy odata version specific common templates and version specific, floorplan specific templates
const templateVersionPath = join(rootTemplatesPath, `v${feApp.service?.version}`);
[join(templateVersionPath, 'common', 'add'), join(templateVersionPath, feApp.template.type, 'add')].forEach(
(templatePath) => {
fs!.copyTpl(
join(templatePath, '**/*.*'),
basePath,
feApp,
{},
{ ignoreNoMatch: true, globOptions: { ignore, dot: true } }
);
}
);
}
// Update manifest.json with template specific settings
extendManifestJson(fs, basePath, rootTemplatesPath, feApp);
const packageJson: Package = JSON.parse(fs.read(packagePath));
// Add tests only if v4, for now, and we have metadata (and therefore a mock server config) or has a cds service
const addTest =
!!feApp.appOptions.addTests &&
feApp.service?.version === OdataVersion.v4 &&
(!!feApp.service?.metadata || feApp.service.type === ServiceType.CDS);
packageJson.scripts = Object.assign(packageJson.scripts ?? {}, {
...getPackageJsonTasks({
localOnly: !feApp.service?.url,
addMock: !!feApp.service?.metadata,
addTest,
sapClient: feApp.service?.client,
flpAppId: feApp.app.flpAppId,
startFile: data?.app?.startFile,
localStartFile: data?.app?.localStartFile,
generateIndex: feApp.appOptions?.generateIndex
})
});
fs.writeJSON(packagePath, packageJson);
if (addTest) {
generateOPAFiles(
basePath,
{
htmlTarget: feApp.appOptions?.generateIndex
? 'index.html'
: `test/flpSandbox.html?sap-ui-xx-viewCache=false#${feApp.app.flpAppId}`
},
fs
);
}
return fs;
}
export { generate, FioriElementsApp, App, TemplateTypeAttributes, minSupportedUI5Version, minSupportedUI5VersionV4 };
export * from './types';