Skip to content

Commit

Permalink
move fiori updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kjose90 committed Sep 4, 2024
1 parent 5a6b177 commit dc197e8
Show file tree
Hide file tree
Showing 23 changed files with 252 additions and 15 deletions.
11 changes: 8 additions & 3 deletions packages/fiori-elements-writer/src/data/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,15 @@ export function getManifestLibs(type: TemplateType, version: OdataVersion, libs?
export function setAppDefaults<T>(feApp: FioriElementsApp<T>): FioriElementsApp<T> {
// Add template information
if (!feApp.app.sourceTemplate?.version || !feApp.app.sourceTemplate?.id) {
const packageInfo = readPkgUp.sync({ cwd: __dirname });
// const packageInfo = readPkgUp.sync({ cwd: __dirname });
// feApp.app.sourceTemplate = {
// id: `${packageInfo?.packageJson.name}:${feApp.template.type}`,
// version: packageInfo?.packageJson.version,
// toolsId: feApp.app.sourceTemplate?.toolsId
// };
feApp.app.sourceTemplate = {
id: `${packageInfo?.packageJson.name}:${feApp.template.type}`,
version: packageInfo?.packageJson.version,
id: `@sap/generator-fiori:${feApp.template.type}`,
version: '1.13.5',
toolsId: feApp.app.sourceTemplate?.toolsId
};
}
Expand Down
5 changes: 5 additions & 0 deletions packages/fiori-elements-writer/src/packageConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { t } from './i18n';
import type { PackageJsonScripts } from './types';
import { getVariantPreviewAppScript } from '@sap-ux/fiori-generator-shared';

/**
* Get an object reflecting the scripts that need to be added to the package.json.
Expand Down Expand Up @@ -76,5 +77,9 @@ export function getPackageJsonTasks({
scripts['int-test'] = 'fiori run --config ./ui5-mock.yaml --open "test/integration/opaTests.qunit.html"';
}

scripts['start-variants-management'] = localOnly
? `echo \\"${t('info.mockOnlyWarning')}\\"`
: getVariantPreviewAppScript(sapClient);

return scripts;
}
1 change: 1 addition & 0 deletions packages/fiori-elements-writer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,5 @@ export interface PackageJsonScripts {
'start-noflp'?: string;
'start-mock'?: string;
'int-test'?: string;
'start-variants-management'?: string;
}
5 changes: 5 additions & 0 deletions packages/fiori-freestyle-writer/src/packageConfig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { t } from './i18n';
import type { PackageJsonScripts } from './types';
import { getVariantPreviewAppScript } from '@sap-ux/fiori-generator-shared';

/**
* Get an object reflecting the scripts that need to be added to the package.json.
Expand Down Expand Up @@ -68,5 +69,9 @@ export function getPackageJsonTasks({
scripts['start-mock'] = `fiori run --config ./ui5-mock.yaml --open "test/flpSandbox.html${params}"`;
}

scripts['start-variants-management'] = localOnly
? `echo \\"${t('info.mockOnlyWarning')}\\"`
: getVariantPreviewAppScript(sapClient);

return scripts;
}
1 change: 1 addition & 0 deletions packages/fiori-freestyle-writer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,5 @@ export interface PackageJsonScripts {
'start-local': string;
'start-noflp'?: string;
'start-mock'?: string;
'start-variants-management'?: string;
}
20 changes: 20 additions & 0 deletions packages/fiori-generator-shared/src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@ export function getBootstrapResourceUrls(

return { uShellBootstrapResourceUrl, uiBootstrapResourceUrl };
}

/**
* Generates a variant management script in preview mode.
*
* @param {string} sapClient - The SAP client parameter to include in the URL. If not provided, the URL will not include the `sap-client` parameter.
* @returns {string} A variant management script to run the application in preview mode.
*/
export function getVariantPreviewAppScript(sapClient?: string): string {
const previewAppAnchor = '#preview-app';
const DisableCacheParam = 'sap-ui-xx-viewCache=false';
const sapClientParam = sapClient ? `sap-client=${sapClient}` : '';
const urlParam = `?${[
sapClientParam,
DisableCacheParam,
'fiori-tools-rta-mode=true',
'sap-ui-rta-skip-flex-validation=true'
].filter(Boolean)
.join('&')}`;
return `fiori run --open \"preview.html${urlParam}${previewAppAnchor}\"`;
}
2 changes: 1 addition & 1 deletion packages/fiori-generator-shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from './cap';
export * from './environment';
export { getBootstrapResourceUrls } from './helpers';
export { getBootstrapResourceUrls, getVariantPreviewAppScript } from './helpers';
export { generateReadMe } from './read-me';
export * from './system-utils';
27 changes: 26 additions & 1 deletion packages/fiori-generator-shared/test/helpers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getBootstrapResourceUrls } from '../src/index';
import { getBootstrapResourceUrls, getVariantPreviewAppScript } from '../src/index';

describe('getResourceUrlsForUi5Bootstrap', () => {
it('should return relative paths for Edmx projects', () => {
Expand Down Expand Up @@ -42,3 +42,28 @@ describe('getResourceUrlsForUi5Bootstrap', () => {
});
});
});

describe('getVariantPreviewAppScript', () => {
it('should return the correct command with a given SAP client', () => {
const sapClient = '100';
const expectedCommand = 'fiori run --open "preview.html?sap-client=100&sap-ui-xx-viewCache=false&fiori-tools-rta-mode=true&sap-ui-rta-skip-flex-validation=true#preview-app"';
expect(getVariantPreviewAppScript(sapClient)).toBe(expectedCommand);
});

it('should return the correct command with an empty SAP client', () => {
const sapClient = '';
const expectedCommand = 'fiori run --open "preview.html?sap-ui-xx-viewCache=false&fiori-tools-rta-mode=true&sap-ui-rta-skip-flex-validation=true#preview-app"';
expect(getVariantPreviewAppScript(sapClient)).toBe(expectedCommand);
});

it('should return the correct command with no SAP client argument', () => {
const sapClient = undefined;
const expectedCommand = 'fiori run --open "preview.html?sap-ui-xx-viewCache=false&fiori-tools-rta-mode=true&sap-ui-rta-skip-flex-validation=true#preview-app"';
expect(getVariantPreviewAppScript(sapClient)).toBe(expectedCommand);
});

it('should handle default parameter value correctly', () => {
const expectedCommand = 'fiori run --open "preview.html?sap-ui-xx-viewCache=false&fiori-tools-rta-mode=true&sap-ui-rta-skip-flex-validation=true#preview-app"';
expect(getVariantPreviewAppScript()).toBe(expectedCommand);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ async function generateUi5MockYamlBasedOnUi5Yaml(
): Promise<UI5Config> {
const ui5MockYamlConfig = await UI5Config.newInstance(fs.read(join(basePath, 'ui5.yaml')));
ui5MockYamlConfig.updateCustomMiddleware(await getNewMockserverMiddleware(path, annotationsConfig));
ui5MockYamlConfig.removeFioriToolsPreviewMiddleware();
return ui5MockYamlConfig;
}

Expand Down
10 changes: 8 additions & 2 deletions packages/odata-service-writer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ async function generate(basePath: string, service: OdataService, fs?: Editor): P
ui5Config.addBackendToFioriToolsProxydMiddleware(service.previewSettings as ProxyBackend);
} catch (error: any) {
if (error instanceof YAMLError && error.code === yamlErrorCode.nodeNotFound) {
ui5Config.addFioriToolsProxydMiddleware({ backend: [service.previewSettings as ProxyBackend] });
ui5Config.addFioriToolsProxydMiddleware({
backend: [service.previewSettings as ProxyBackend],
ignoreCertError: service.ignoreCertError
});
} else {
throw error;
}
Expand All @@ -106,7 +109,10 @@ async function generate(basePath: string, service: OdataService, fs?: Editor): P
ui5LocalConfigPath = join(dirname(paths.ui5Yaml), 'ui5-local.yaml');
if (fs.exists(ui5LocalConfigPath)) {
ui5LocalConfig = await UI5Config.newInstance(fs.read(ui5LocalConfigPath));
ui5LocalConfig.addFioriToolsProxydMiddleware({ backend: [service.previewSettings as ProxyBackend] });
ui5LocalConfig.addFioriToolsProxydMiddleware({
backend: [service.previewSettings as ProxyBackend],
ignoreCertError: service.ignoreCertError
});
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/odata-service-writer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,5 @@ export interface OdataService {
annotations?: EdmxAnnotationsInfo | CdsAnnotationsInfo;
localAnnotationsName?: string; // The name used in the manifest.json and as the filename for local annotations
previewSettings?: Partial<ProxyBackend>;
ignoreCertError?: boolean;
}
1 change: 1 addition & 0 deletions packages/ui5-application-writer/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"devDependencies": {
"@sap-ux/eslint-plugin-fiori-tools": "workspace:*",
"@sap-ux/project-access": "workspace:*",
"@sap-ux/feature-toggle": "workspace:*",
"@types/ejs": "3.1.2",
"@types/fs-extra": "9.0.13",
"@types/lodash": "4.14.202",
Expand Down
5 changes: 4 additions & 1 deletion packages/ui5-application-writer/src/data/defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import semVer from 'semver';
import type { SemVer } from 'semver';
import { t } from '../i18n';
import merge from 'lodash/mergeWith';
import { isInternalFeaturesSettingEnabled } from '@sap-ux/feature-toggle';
import { SapUxLayer } from '../types';

/**
* Returns a package instance with default properties.
Expand All @@ -32,7 +34,8 @@ export function packageDefaults(version?: string, description?: string, isEdmxPr
start: 'ui5 serve --config=ui5.yaml --open index.html',
'start-local': 'ui5 serve --config=ui5-local.yaml --open index.html',
build: 'ui5 build --config=ui5.yaml --clean-dest --dest dist'
}
},
sapuxLayer: isInternalFeaturesSettingEnabled() ? SapUxLayer.VENDOR : SapUxLayer.CUSTOMER_BASE
};
}
return {
Expand Down
18 changes: 15 additions & 3 deletions packages/ui5-application-writer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { join } from 'path';
import { create as createStorage } from 'mem-fs';
import type { Editor } from 'mem-fs-editor';
import { create } from 'mem-fs-editor';
import type { App, AppOptions, Package, UI5 } from './types';
import type { App, AppOptions, Package, UI5, ApiHubConfig } from './types';
import { UI5Config, getEsmTypesVersion, getTypesPackage } from '@sap-ux/ui5-config';
import { getMinimumUI5Version, type Manifest } from '@sap-ux/project-access';
import { mergeWithDefaults } from './data';
import { ui5TSSupport } from './data/ui5Libs';
import { applyOptionalFeatures, enableTypescript as enableTypescriptOption, getTemplateOptions } from './options';
import { Ui5App } from './types';
import { Ui5App, API_HUB_API_KEY, API_HUB_TYPE } from './types';

/**
* Writes the template to the memfs editor instance.
Expand All @@ -19,6 +19,7 @@ import { Ui5App } from './types';
* @returns the updated memfs editor instance
*/
async function generate(basePath: string, ui5AppConfig: Ui5App, fs?: Editor): Promise<Editor> {
console.log("---- basePath ----", basePath);
if (!fs) {
fs = create(createStorage());
}
Expand Down Expand Up @@ -58,7 +59,10 @@ async function generate(basePath: string, ui5AppConfig: Ui5App, fs?: Editor): Pr
}
});
ui5Config.addFioriToolsAppReloadMiddleware();

if (isEdmxProjectType) {
// add preview middleware to ui5Config
ui5Config.addFioriToolsPreviewMiddleware(ui5App.app.id, ui5App.ui5?.ui5Theme);
const ui5LocalConfigPath = join(basePath, 'ui5-local.yaml');
// write ui5-local.yaml only for non-CAP applications
const ui5LocalConfig = await UI5Config.newInstance(fs.read(ui5LocalConfigPath));
Expand All @@ -71,6 +75,8 @@ async function generate(basePath: string, ui5AppConfig: Ui5App, fs?: Editor): Pr
ui5LocalConfig.addFioriToolsAppReloadMiddleware();
// Add optional features
await applyOptionalFeatures(ui5App, fs, basePath, tmplPath, [ui5Config, ui5LocalConfig]);
// add preview middleware to ui5LocalConfig
ui5LocalConfig.addFioriToolsPreviewMiddleware(ui5App.app.id, ui5App.ui5?.ui5Theme);
// write ui5 local yaml
fs.write(ui5LocalConfigPath, ui5LocalConfig.toString());
} else {
Expand All @@ -80,6 +86,12 @@ async function generate(basePath: string, ui5AppConfig: Ui5App, fs?: Editor): Pr
// write ui5 yaml
fs.write(ui5ConfigPath, ui5Config.toString());

// Create the files for apiHub integration.
fs.write(
`${basePath}/.env`,
`${API_HUB_API_KEY}=${ui5App.appOptions.apiHubConfig?.apiHubKey}\n${API_HUB_TYPE}=${ui5App.appOptions.apiHubConfig?.apiHubType}`
);

return fs;
}

Expand Down Expand Up @@ -147,5 +159,5 @@ async function enableTypescript(basePath: string, fs?: Editor): Promise<Editor>
return fs;
}

export { Ui5App, generate, enableTypescript, isTypescriptEnabled };
export { Ui5App, generate, enableTypescript, isTypescriptEnabled, type ApiHubConfig };
export { App, Package, UI5, AppOptions };
29 changes: 29 additions & 0 deletions packages/ui5-application-writer/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Package {
ui5?: {
dependencies?: string[];
};
sapuxLayer?: SapUxLayer;
}

export interface App {
Expand Down Expand Up @@ -145,6 +146,30 @@ export interface UI5 {
customUi5Libs?: string[];
}

export const API_HUB_API_KEY = 'API_HUB_API_KEY';
export const API_HUB_TYPE = 'API_HUB_TYPE';

const enum ApiHubType {
apiHub = 'API_HUB',
apiHubEnterprise = 'API_HUB_ENTERPRISE'
}

/**
* SAP UX Layer
*/
export enum SapUxLayer {
VENDOR = 'VENDOR',
CUSTOMER_BASE = 'CUSTOMER_BASE'
}

/**
* Defines the api hub service properties or enterprise and non-enterprise versions
*/
export interface ApiHubConfig {
apiHubKey: string;
apiHubType: ApiHubType;
}

// Additional configurable features
export interface AppOptions {
codeAssist: boolean; // Enables code assist
Expand All @@ -171,6 +196,10 @@ export interface AppOptions {
* Excludes the index.html from the template and does not add the `start-noflp` script in package.json
*/
generateIndex?: boolean;
/**
* Api Hub configuration
*/
apiHubConfig?: ApiHubConfig;
}

export interface Ui5App {
Expand Down
3 changes: 3 additions & 0 deletions packages/ui5-application-writer/templates/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@
"dependencies": {},
"devDependencies": <%- JSON.stringify(package.devDependencies, null, 8) -%>,
"scripts": <%- JSON.stringify(package.scripts, null, 4) -%>
<% if (package.sapuxLayer) { %>
,"sapuxLayer": "<%= package.sapuxLayer %>"
<% } %>
}
3 changes: 3 additions & 0 deletions packages/ui5-application-writer/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
{
"path": "../eslint-plugin-fiori-tools"
},
{
"path": "../feature-toggle"
},
{
"path": "../project-access"
},
Expand Down
27 changes: 24 additions & 3 deletions packages/ui5-config/src/middlewares.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type {
FioriAppReloadConfig,
FioriToolsProxyConfig,
MockserverConfig,
FioriToolsProxyConfigUI5
FioriToolsProxyConfigUI5,
FioriPreviewConfig
} from './types';
import type { NodeComment } from '@sap-ux/yaml';

Expand All @@ -25,6 +26,24 @@ export function getAppReloadMiddlewareConfig(): CustomMiddleware<FioriAppReloadC
};
}

/**
* Generates the configuration for a Fiori preview middleware.
*
* @param {string} appId - The ID of the application for which the preview middleware is being configured.
* @param {string} ui5Theme - The theme to be used for the application.
* @returns {CustomMiddleware<FioriPreviewConfig>} The configuration object for the middleware.
*/
export function getPreviewMiddlewareConfig(appId: string, ui5Theme: string): CustomMiddleware<FioriPreviewConfig> {
return {
name: 'fiori-tools-preview',
afterMiddleware: 'fiori-tools-appreload',
configuration: {
component: appId,
ui5Theme: ui5Theme
}
};
}

/**
* Returns default comments for the given backend configuration values.
*
Expand Down Expand Up @@ -54,12 +73,14 @@ export function getBackendComments(
* @param backends configuration of backends
* @param ui5 UI5 configuration
* @param afterMiddleware middleware after which fiori-tools-proxy middleware will be started
* @param ignoreCertError
* @returns {{config, comments}} configuration and comments
*/
export function getFioriToolsProxyMiddlewareConfig(
backends?: FioriToolsProxyConfigBackend[],
ui5?: Partial<FioriToolsProxyConfigUI5>,
afterMiddleware = 'compression'
afterMiddleware = 'compression',
ignoreCertError = false
): {
config: CustomMiddleware<FioriToolsProxyConfig>;
comments: NodeComment<CustomMiddleware<FioriToolsProxyConfig>>[];
Expand All @@ -68,7 +89,7 @@ export function getFioriToolsProxyMiddlewareConfig(
name: 'fiori-tools-proxy',
afterMiddleware,
configuration: {
ignoreCertError: false
ignoreCertError: ignoreCertError
}
};
let comments: NodeComment<CustomMiddleware<FioriToolsProxyConfig>>[] = [
Expand Down
5 changes: 5 additions & 0 deletions packages/ui5-config/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ export interface FioriAppReloadConfig {
delay: number;
}

export interface FioriPreviewConfig {
component: string;
ui5Theme: string;
}

export interface ServeStaticPath {
path: string;
src: string;
Expand Down
Loading

0 comments on commit dc197e8

Please sign in to comment.