@sap-ux/preview-middleware 0.3.1 → 0.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -11,9 +11,10 @@ The `@sap-ux/preview-middleware` is a [Custom UI5 Server Middleware](https://sap
11
11
  | `flp.intent.object` | `string` | `app` | Optional intent object |
12
12
  | `flp.intent.action` | `string` | `preview` | Optional intent action |
13
13
  | `flp.apps` | `array` | `[]` | Optional additional local apps that are available in local Fiori launchpad |
14
+ | `flp.libs` | `boolean` | `false` | Optional flag to add a generic script fetching the paths of used libraries not available in UI5 |
14
15
  | `adp.target` | | | Required configuration for adaptation projects defining the connected backend |
15
16
  | `adp.ignoreCertErrors` | `boolean` | `false` | Optional setting to ignore certification validation errors when working with e.g. development systems with self signed certificates |
16
- | `debug` | `boolean` | false | Enables debug output |
17
+ | `debug` | `boolean` | `false` | Enables debug output |
17
18
 
18
19
  ### `flp.apps`
19
20
  Array of additional application configurations:
@@ -7,6 +7,7 @@ import type { Manifest, UI5FlexLayer } from '@sap-ux/project-access';
7
7
  * Internal structure used to fill the sandbox.html template
8
8
  */
9
9
  export interface TemplateConfig {
10
+ basePath: string;
10
11
  apps: Record<string, {
11
12
  title: string;
12
13
  description: string;
@@ -58,11 +59,18 @@ export declare class FlpSandbox {
58
59
  */
59
60
  init(manifest: Manifest, componentId?: string, resources?: Record<string, string>): Promise<void>;
60
61
  /**
61
- * Try finding the locate-reuse-libs script.
62
+ * Add routes for html and scripts required for a local FLP.
63
+ */
64
+ private addStandardRoutes;
65
+ /**
66
+ * Try finding a locate-reuse-libs script in the project.
62
67
  *
63
68
  * @returns the location of the locate-reuse-libs script or undefined.
64
69
  */
65
70
  private findLocateReuseLibsScript;
71
+ /**
72
+ * Add additional routes for apps also to be shown in the local FLP.
73
+ */
66
74
  private addRoutesForAdditionalApps;
67
75
  /**
68
76
  * Create required routes for flex.
package/dist/base/flp.js CHANGED
@@ -23,6 +23,10 @@ const DEFAULT_THEME = 'sap_horizon';
23
23
  * Default path for mounting the local FLP.
24
24
  */
25
25
  const DEFAULT_PATH = '/test/flp.html';
26
+ /**
27
+ * Default name of the locate reuse libs script.
28
+ */
29
+ const DEFAULT_LOCATE_LIBS_FILENAME = 'locate-reuse-libs.js';
26
30
  /**
27
31
  * Default intent
28
32
  */
@@ -51,8 +55,12 @@ class FlpSandbox {
51
55
  path: (_a = config.path) !== null && _a !== void 0 ? _a : DEFAULT_PATH,
52
56
  intent: (_b = config.intent) !== null && _b !== void 0 ? _b : DEFAULT_INTENT,
53
57
  apps: (_c = config.apps) !== null && _c !== void 0 ? _c : [],
54
- rta: config.rta
58
+ rta: config.rta,
59
+ libs: config.libs
55
60
  };
61
+ if (!this.config.path.startsWith('/')) {
62
+ this.config.path = `/${this.config.path}`;
63
+ }
56
64
  logger.debug(`Config: ${JSON.stringify(this.config)}`);
57
65
  this.router = (0, express_1.Router)();
58
66
  }
@@ -64,60 +72,79 @@ class FlpSandbox {
64
72
  * @param resources optional additional resource mappings
65
73
  */
66
74
  init(manifest, componentId, resources = {}) {
67
- var _a, _b, _c, _d, _e, _f;
75
+ var _a, _b, _c, _d, _e, _f, _g;
68
76
  return __awaiter(this, void 0, void 0, function* () {
69
77
  const flex = this.createFlexHandler();
70
78
  const supportedThemes = (_b = (_a = manifest['sap.ui5']) === null || _a === void 0 ? void 0 : _a.supportedThemes) !== null && _b !== void 0 ? _b : [DEFAULT_THEME];
71
79
  this.templateConfig = {
80
+ basePath: (_c = (0, path_1.relative)((0, path_1.dirname)(this.config.path), '/')) !== null && _c !== void 0 ? _c : '.',
72
81
  apps: {},
73
82
  ui5: {
74
- libs: Object.keys((_e = (_d = (_c = manifest['sap.ui5']) === null || _c === void 0 ? void 0 : _c.dependencies) === null || _d === void 0 ? void 0 : _d.libs) !== null && _e !== void 0 ? _e : {}).join(','),
83
+ libs: Object.keys((_f = (_e = (_d = manifest['sap.ui5']) === null || _d === void 0 ? void 0 : _d.dependencies) === null || _e === void 0 ? void 0 : _e.libs) !== null && _f !== void 0 ? _f : {}).join(','),
75
84
  theme: supportedThemes.includes(DEFAULT_THEME) ? DEFAULT_THEME : supportedThemes[0],
76
85
  flex,
77
86
  resources: Object.assign({}, resources)
78
87
  },
79
- locateReuseLibsScript: yield this.findLocateReuseLibsScript()
88
+ locateReuseLibsScript: this.config.libs
89
+ ? `./${DEFAULT_LOCATE_LIBS_FILENAME}`
90
+ : yield this.findLocateReuseLibsScript()
80
91
  };
81
92
  this.addApp(manifest, {
82
93
  componentId,
83
- target: (_f = resources[componentId !== null && componentId !== void 0 ? componentId : manifest['sap.app'].id]) !== null && _f !== void 0 ? _f : '/',
94
+ target: (_g = resources[componentId !== null && componentId !== void 0 ? componentId : manifest['sap.app'].id]) !== null && _g !== void 0 ? _g : this.templateConfig.basePath,
84
95
  local: '.',
85
96
  intent: this.config.intent
86
97
  });
87
- // add route for the sandbox.html
88
- this.router.get(this.config.path, (req, res) => {
89
- var _a, _b;
90
- const config = Object.assign({}, this.templateConfig);
91
- const fioriToolsRtaMode = req.query['fiori-tools-rta-mode'];
92
- if (fioriToolsRtaMode) {
93
- if ((_a = this.config.rta) === null || _a === void 0 ? void 0 : _a.layer) {
94
- config.flex = {
95
- layer: (_b = this.config.rta) === null || _b === void 0 ? void 0 : _b.layer,
96
- developerMode: fioriToolsRtaMode === 'forAdaptation'
97
- };
98
- }
99
- else {
100
- this.logger.error('Fiori tools RTA mode could not be started because the RTA layer is missing.');
101
- }
102
- }
103
- const template = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../templates/flp/sandbox.html'), 'utf-8');
104
- const html = (0, ejs_1.render)(template, config);
105
- // if livereload is enabled, don't send it but let other middleware modify the content
106
- if (res._livereload) {
107
- res.write(html);
108
- res.end();
109
- }
110
- else {
111
- res.status(200).contentType('html').send(html);
112
- }
113
- });
98
+ this.addStandardRoutes();
114
99
  this.addRoutesForAdditionalApps();
115
100
  this.logger.info(`Initialized for app ${manifest['sap.app'].id}`);
116
101
  this.logger.debug(`Configured apps: ${JSON.stringify(this.templateConfig.apps)}`);
117
102
  });
118
103
  }
119
104
  /**
120
- * Try finding the locate-reuse-libs script.
105
+ * Add routes for html and scripts required for a local FLP.
106
+ */
107
+ addStandardRoutes() {
108
+ // add route for the sandbox.html
109
+ this.router.get(this.config.path, (req, res) => {
110
+ var _a, _b;
111
+ const config = Object.assign({}, this.templateConfig);
112
+ const fioriToolsRtaMode = req.query['fiori-tools-rta-mode'];
113
+ if (fioriToolsRtaMode) {
114
+ if ((_a = this.config.rta) === null || _a === void 0 ? void 0 : _a.layer) {
115
+ config.flex = {
116
+ layer: (_b = this.config.rta) === null || _b === void 0 ? void 0 : _b.layer,
117
+ developerMode: fioriToolsRtaMode === 'forAdaptation'
118
+ };
119
+ }
120
+ else {
121
+ this.logger.error('Fiori tools RTA mode could not be started because the RTA layer is missing.');
122
+ }
123
+ }
124
+ const template = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../templates/flp/sandbox.html'), 'utf-8');
125
+ const html = (0, ejs_1.render)(template, config);
126
+ // if livereload is enabled, don't send it but let other middleware modify the content
127
+ if (res._livereload) {
128
+ res.write(html);
129
+ res.end();
130
+ }
131
+ else {
132
+ res.status(200).contentType('html').send(html);
133
+ }
134
+ });
135
+ // add route for locate-reuse-libs if requested
136
+ if (this.config.libs && this.templateConfig.locateReuseLibsScript) {
137
+ const pathParts = this.config.path.split('/');
138
+ pathParts.pop();
139
+ pathParts.push(DEFAULT_LOCATE_LIBS_FILENAME);
140
+ this.router.get(pathParts.join('/'), (_req, res) => {
141
+ const script = (0, fs_1.readFileSync)((0, path_1.join)(__dirname, '../../templates/flp/locate-reuse-libs.js'), 'utf-8');
142
+ res.status(200).contentType('text/javascript').send(script);
143
+ });
144
+ }
145
+ }
146
+ /**
147
+ * Try finding a locate-reuse-libs script in the project.
121
148
  *
122
149
  * @returns the location of the locate-reuse-libs script or undefined.
123
150
  */
@@ -132,6 +159,9 @@ class FlpSandbox {
132
159
  }
133
160
  });
134
161
  }
162
+ /**
163
+ * Add additional routes for apps also to be shown in the local FLP.
164
+ */
135
165
  addRoutesForAdditionalApps() {
136
166
  for (const app of this.config.apps) {
137
167
  if (app.local) {
@@ -28,6 +28,10 @@ export interface FlpConfig {
28
28
  rta?: {
29
29
  layer?: UI5FlexLayer;
30
30
  };
31
+ /**
32
+ * Optional: if set to true then a locate-reuse-libs script will be attached to the html
33
+ */
34
+ libs?: boolean;
31
35
  apps: App[];
32
36
  }
33
37
  /**
package/package.json CHANGED
@@ -9,7 +9,7 @@
9
9
  "bugs": {
10
10
  "url": "https://github.com/SAP/open-ux-tools/issues?q=is%3Aopen+is%3Aissue+label%3Abug+label%3Apreview-middleware"
11
11
  },
12
- "version": "0.3.1",
12
+ "version": "0.3.3",
13
13
  "license": "Apache-2.0",
14
14
  "author": "@SAP/ux-tools-team",
15
15
  "main": "dist/index.js",
@@ -24,7 +24,7 @@
24
24
  "dependencies": {
25
25
  "ejs": "3.1.7",
26
26
  "@sap-ux/logger": "0.3.7",
27
- "@sap-ux/adp-tooling": "0.1.1"
27
+ "@sap-ux/adp-tooling": "0.1.2"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@types/ejs": "3.1.0",
@@ -0,0 +1,195 @@
1
+ (function (sap) {
2
+ var fioriToolsGetManifestLibs = function (manifestPath) {
3
+ var url = manifestPath;
4
+ var result = "";
5
+ // SAPUI5 delivered namespaces from https://ui5.sap.com/#/api/sap
6
+ var ui5Libs = [
7
+ "sap.apf",
8
+ "sap.base",
9
+ "sap.chart",
10
+ "sap.collaboration",
11
+ "sap.f",
12
+ "sap.fe",
13
+ "sap.fileviewer",
14
+ "sap.gantt",
15
+ "sap.landvisz",
16
+ "sap.m",
17
+ "sap.ndc",
18
+ "sap.ovp",
19
+ "sap.rules",
20
+ "sap.suite",
21
+ "sap.tnt",
22
+ "sap.ui",
23
+ "sap.uiext",
24
+ "sap.ushell",
25
+ "sap.uxap",
26
+ "sap.viz",
27
+ "sap.webanalytics",
28
+ "sap.zen"
29
+ ];
30
+ function getKeys(libOrComp, libOrCompKeysString) {
31
+ var libOrCompKeysStringTmp = libOrCompKeysString;
32
+ Object.keys(libOrComp).forEach(function (libOrCompKey) {
33
+ // ignore libs or Components that start with SAPUI5 delivered namespaces
34
+ if (!ui5Libs.some(function (substring) { return libOrCompKey === substring || libOrCompKey.startsWith(substring + "."); })) {
35
+ if (libOrCompKeysStringTmp.length > 0) {
36
+ libOrCompKeysStringTmp = libOrCompKeysStringTmp + "," + libOrCompKey;
37
+ } else {
38
+ libOrCompKeysStringTmp = libOrCompKey;
39
+ }
40
+ }
41
+ });
42
+ return libOrCompKeysStringTmp;
43
+ }
44
+ return new Promise(function (resolve, reject) {
45
+ $.ajax(url)
46
+ .done(function (manifest) {
47
+ if (manifest) {
48
+ if (
49
+ manifest["sap.ui5"] &&
50
+ manifest["sap.ui5"].dependencies
51
+ ) {
52
+ if (manifest["sap.ui5"].dependencies.libs) {
53
+ result = getKeys(manifest["sap.ui5"].dependencies.libs, result);
54
+ }
55
+ if (manifest["sap.ui5"].dependencies.components) {
56
+ result = getKeys(manifest["sap.ui5"].dependencies.components, result);
57
+ }
58
+ }
59
+ if (
60
+ manifest["sap.ui5"] &&
61
+ manifest["sap.ui5"].componentUsages
62
+ ) {
63
+ result = getKeys(manifest["sap.ui5"].componentUsages, result);
64
+ }
65
+ }
66
+ resolve(result);
67
+ })
68
+ .fail(function () {
69
+ reject(new Error("Could not fetch manifest at '" + manifestPath));
70
+ });
71
+ });
72
+ };
73
+ function registerModules(dataFromAppIndex) {
74
+ Object.keys(dataFromAppIndex).forEach(function (moduleDefinitionKey) {
75
+ var moduleDefinition = dataFromAppIndex[moduleDefinitionKey];
76
+ if (moduleDefinition && moduleDefinition.dependencies) {
77
+ moduleDefinition.dependencies.forEach(function (dependency) {
78
+ if (dependency.url && dependency.url.length > 0 && dependency.type === "UI5LIB") {
79
+ sap.ui.require(["sap/base/Log"], function (Log) {
80
+ Log.info("Registering Library " +
81
+ dependency.componentId +
82
+ " from server " +
83
+ dependency.url);
84
+ });
85
+ var compId = dependency.componentId.replace(/\./g, "/");
86
+ var config = {
87
+ paths: {
88
+ }
89
+ };
90
+ config.paths[compId] = dependency.url;
91
+ sap.ui.loader.config(config);
92
+ }
93
+ });
94
+ }
95
+ });
96
+ }
97
+ /**
98
+ * Registers the module paths for dependencies of the given component.
99
+ * @param {string} manifestPath The the path to the app manifest path
100
+ * for which the dependencies should be registered.
101
+ * @returns {Promise} A promise which is resolved when the ajax request for
102
+ * the app-index was successful and the module paths were registered.
103
+ */
104
+ sap.registerComponentDependencyPaths = function (manifestPath) {
105
+
106
+ return fioriToolsGetManifestLibs(manifestPath).then(function (libs) {
107
+ if (libs && libs.length > 0) {
108
+ var url = "/sap/bc/ui2/app_index/ui5_app_info?id=" + libs;
109
+ var sapClient = "";
110
+
111
+ return new Promise(
112
+ function (resolve) {
113
+ sap.ui.require(["sap/base/util/UriParameters"], function (UriParameters) {
114
+ sapClient = UriParameters.fromQuery(window.location.search).get("sap-client");
115
+ if (sapClient && sapClient.length === 3) {
116
+ url = url + "&sap-client=" + sapClient;
117
+ }
118
+ resolve(url);
119
+ });
120
+ }).then(function (url2) {
121
+ return $.ajax(url2).done(function (data) {
122
+ if (data) {
123
+ registerModules(data);
124
+ }
125
+ });
126
+ });
127
+ } else {
128
+ return undefined;
129
+ }
130
+ });
131
+ };
132
+ })(sap);
133
+
134
+ /*eslint-disable fiori-custom/sap-browser-api-warning, fiori-custom/sap-no-dom-access*/
135
+ var currentScript = document.getElementById("locate-reuse-libs");
136
+ if (!currentScript) {
137
+ currentScript = document.currentScript;
138
+ }
139
+ var manifestUri = currentScript.getAttribute("data-sap-ui-manifest-uri");
140
+ var componentName = currentScript.getAttribute("data-sap-ui-componentName");
141
+ var useMockserver = currentScript.getAttribute("data-sap-ui-use-mockserver");
142
+
143
+ sap.registerComponentDependencyPaths(manifestUri)
144
+ .catch(function (error) {
145
+ sap.ui.require(["sap/base/Log"], function (Log) {
146
+ Log.error(error);
147
+ });
148
+ })
149
+ .finally(function () {
150
+
151
+ // setting the app title with internationalization
152
+ sap.ui.getCore().attachInit(function () {
153
+ var sLocale = sap.ui.getCore().getConfiguration().getLanguage();
154
+ sap.ui.require(["sap/base/i18n/ResourceBundle"], function (ResourceBundle) {
155
+ var oResourceBundle = ResourceBundle.create({
156
+ url: "i18n/i18n.properties",
157
+ locale: sLocale
158
+ });
159
+ document.title = oResourceBundle.getText("appTitle");
160
+ });
161
+ });
162
+
163
+ if (componentName && componentName.length > 0) {
164
+ if (useMockserver && useMockserver === "true") {
165
+ sap.ui.getCore().attachInit(function () {
166
+ sap.ui.require([componentName.replace(/\./g, "/") + "/localService/mockserver"], function (server) {
167
+ // set up test service for local testing
168
+ server.init();
169
+ // initialize the ushell sandbox component
170
+ sap.ushell.Container.createRenderer().placeAt("content");
171
+ });
172
+ });
173
+ } else {
174
+ // Requiring the ComponentSupport module automatically executes the component initialisation for all declaratively defined components
175
+ sap.ui.require(["sap/ui/core/ComponentSupport"]);
176
+
177
+ // setting the app title with the i18n text
178
+ sap.ui.getCore().attachInit(function () {
179
+ var sLocale = sap.ui.getCore().getConfiguration().getLanguage();
180
+ sap.ui.require(["sap/base/i18n/ResourceBundle"], function (ResourceBundle) {
181
+ var oResourceBundle = ResourceBundle.create({
182
+ url: "i18n/i18n.properties",
183
+ locale: sLocale
184
+ });
185
+ document.title = oResourceBundle.getText("appTitle");
186
+ });
187
+ });
188
+ }
189
+ } else {
190
+ sap.ui.getCore().attachInit(function () {
191
+ // initialize the ushell sandbox component
192
+ sap.ushell.Container.createRenderer().placeAt("content");
193
+ });
194
+ }
195
+ });
@@ -40,10 +40,10 @@
40
40
  };
41
41
  </script>
42
42
 
43
- <script src="/test-resources/sap/ushell/bootstrap/sandbox.js" id="sap-ushell-bootstrap"></script>
43
+ <script src="<%- basePath %>/test-resources/sap/ushell/bootstrap/sandbox.js" id="sap-ushell-bootstrap"></script>
44
44
  <!-- Bootstrap the UI5 core library. 'data-sap-ui-frameOptions="allow"'' is a NON-SECURE setting for test environments -->
45
45
  <script id="sap-ui-bootstrap"
46
- src="/resources/sap-ui-core.js"
46
+ src="<%- basePath %>/resources/sap-ui-core.js"
47
47
  data-sap-ui-libs="<%- ui5.libs %>"
48
48
  data-sap-ui-async="true"
49
49
  data-sap-ui-preload="async"