cds-plugin-ui5 0.7.5 → 0.8.1

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/CHANGELOG.md CHANGED
@@ -3,6 +3,29 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [0.8.1](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/cds-plugin-ui5@0.8.0...cds-plugin-ui5@0.8.1) (2024-02-18)
7
+
8
+
9
+ ### Bug Fixes
10
+
11
+ * **cds-plugin-ui5:** only start when using "cds serve|watch" ([#951](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/951)) ([7b67bd9](https://github.com/ui5-community/ui5-ecosystem-showcase/commit/7b67bd975b5a082e6a33855b12a058ad53d0a433)), closes [#950](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/950)
12
+ * prefere cds.env._home over just current process.cwd ([#947](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/947)) ([ecb6e69](https://github.com/ui5-community/ui5-ecosystem-showcase/commit/ecb6e69af178f93a3723c005609c7fefb50c8b41))
13
+
14
+
15
+
16
+
17
+
18
+ # [0.8.0](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/cds-plugin-ui5@0.7.5...cds-plugin-ui5@0.8.0) (2024-01-25)
19
+
20
+
21
+ ### Features
22
+
23
+ * **ui5-middleware-ui5:** support nested scenarios in CDS servers ([#938](https://github.com/ui5-community/ui5-ecosystem-showcase/issues/938)) ([5d61f18](https://github.com/ui5-community/ui5-ecosystem-showcase/commit/5d61f18f04a624e8f61ec7fa1e8f32e81c43f6b0))
24
+
25
+
26
+
27
+
28
+
6
29
  ## [0.7.5](https://github.com/ui5-community/ui5-ecosystem-showcase/compare/cds-plugin-ui5@0.7.4...cds-plugin-ui5@0.7.5) (2024-01-17)
7
30
 
8
31
 
package/cds-plugin.js CHANGED
@@ -68,119 +68,125 @@ if (!skip) {
68
68
  cds.on("bootstrap", async function bootstrap(app) {
69
69
  log.debug("bootstrap");
70
70
 
71
- const cwd = process.cwd();
72
- const ui5Modules = await findUI5Modules({ cwd, cds });
73
- const { localApps, config } = ui5Modules;
74
-
75
- const links = [];
76
-
77
- // register the UI5 modules via their own router/middlewares
78
- for await (const ui5Module of ui5Modules) {
79
- const { moduleId, mountPath, modulePath } = ui5Module;
80
-
81
- // mounting the Router for the UI5 application to the CDS server
82
- log.info(`Mounting ${mountPath} to UI5 app ${modulePath} (id=${moduleId})${config[moduleId] ? ` using config=${JSON.stringify(config[moduleId])}` : ""}`);
83
-
84
- // create a patched router
85
- const router = await createPatchedRouter();
86
-
87
- // apply the UI5 middlewares to the router and
88
- // retrieve the available HTML pages
89
- const appInfo = await applyUI5Middleware(router, {
90
- cwd,
91
- basePath: modulePath,
92
- ...(config[moduleId] || {}),
93
- });
94
-
95
- // register the router to the specified mount path
96
- app.use(mountPath, router);
97
-
98
- // append the HTML pages to the links
99
- appInfo.pages.forEach((page) => {
100
- const prefix = mountPath !== "/" ? mountPath : "";
101
- links.push(`${prefix}${page}`);
102
- });
103
- }
71
+ // only for cds serve the cds-plugin-ui5 is active
72
+ if (cds.cli?.command === "serve") {
73
+ const cwd = cds.env?._home || process.cwd();
74
+ const ui5Modules = await findUI5Modules({ cwd, cds });
75
+ const { localApps, config } = ui5Modules;
76
+
77
+ const links = [];
78
+
79
+ // register the UI5 modules via their own router/middlewares
80
+ for await (const ui5Module of ui5Modules) {
81
+ const { moduleId, mountPath, modulePath } = ui5Module;
82
+
83
+ // mounting the Router for the UI5 application to the CDS server
84
+ log.info(`Mounting ${mountPath} to UI5 app ${modulePath} (id=${moduleId})${config[moduleId] ? ` using config=${JSON.stringify(config[moduleId])}` : ""}`);
85
+
86
+ // create a patched router
87
+ const router = await createPatchedRouter();
88
+
89
+ // apply the UI5 middlewares to the router and
90
+ // retrieve the available HTML pages
91
+ const appInfo = await applyUI5Middleware(router, {
92
+ cwd,
93
+ basePath: modulePath,
94
+ ...(config[moduleId] || {}),
95
+ });
96
+
97
+ // register the router to the specified mount path
98
+ app.use(mountPath, router);
99
+
100
+ // append the HTML pages to the links
101
+ appInfo.pages.forEach((page) => {
102
+ const prefix = mountPath !== "/" ? mountPath : "";
103
+ links.push(`${prefix}${page}`);
104
+ });
105
+ }
104
106
 
105
- // identify whether the welcome page should be rewritten
106
- let rewrite = links.length > 0;
107
-
108
- // rewrite the welcome page
109
- if (rewrite) {
110
- // register the custom middleware (similar like in @sap/cds/server.js)
111
- app.get("/", function appendLinksToIndex(req, res, next) {
112
- req._cds_plugin_ui5 = true; // marker for patched router to ignore
113
- rewriteHTML(
114
- req,
115
- res,
116
- () => true,
117
- (doc) => {
118
- // the first <ul> element contains the links to the
119
- // application pages which is fully under control of
120
- // our plugin now and we keep all links to static
121
- // pages to ensure coop with classic apps
122
- const head = doc.getElementsByTagName("head")?.[0];
123
- head?.insertAdjacentHTML(
124
- "beforeend",
125
- `<style>
126
- a.ui5:after {
127
- content: "UI5";
128
- font-weight: bold;
129
- font-size: 0.5rem;
130
- vertical-align: super;
131
- margin: 0.25rem;
132
- color: #1873B4;
133
- }
134
- @media (prefers-color-scheme: dark) {
107
+ // identify whether the welcome page should be rewritten
108
+ let rewrite = links.length > 0;
109
+
110
+ // rewrite the welcome page
111
+ if (rewrite) {
112
+ // register the custom middleware (similar like in @sap/cds/server.js)
113
+ app.get("/", function appendLinksToIndex(req, res, next) {
114
+ req._cds_plugin_ui5 = true; // marker for patched router to ignore
115
+ rewriteHTML(
116
+ req,
117
+ res,
118
+ () => true,
119
+ (doc) => {
120
+ // the first <ul> element contains the links to the
121
+ // application pages which is fully under control of
122
+ // our plugin now and we keep all links to static
123
+ // pages to ensure coop with classic apps
124
+ const head = doc.getElementsByTagName("head")?.[0];
125
+ head?.insertAdjacentHTML(
126
+ "beforeend",
127
+ `<style>
135
128
  a.ui5:after {
136
- color: #FF5A37;
129
+ content: "UI5";
130
+ font-weight: bold;
131
+ font-size: 0.5rem;
132
+ vertical-align: super;
133
+ margin: 0.25rem;
134
+ color: #1873B4;
137
135
  }
138
- }
139
- </style>`
140
- );
141
- const ul = doc.getElementsByTagName("ul")?.[0];
142
- if (ul) {
143
- const newLis = [];
144
- const lis = ul.getElementsByTagName("li");
145
- lis?.forEach((li) => {
146
- const appDir = li.firstChild?.text?.split("/")?.[1];
147
- if (localApps.has(appDir)) {
148
- newLis.push(li.toString());
136
+ @media (prefers-color-scheme: dark) {
137
+ a.ui5:after {
138
+ color: #FF5A37;
149
139
  }
150
- });
151
- newLis.push(
152
- ...links.sort().map((link) => {
153
- // we remove the query parameters from the link text
154
- const linkText = link.indexOf("?") === -1 ? link : link.substr(0, link.indexOf("?"));
155
- // renders a UI5 link ;-)
156
- return `<li><a class="ui5" href="${link}">${linkText}</a></li>`;
157
- })
140
+ }
141
+ </style>`
158
142
  );
159
- ul.innerHTML = newLis.join("\n");
160
- } else {
161
- log.warn(`Failed to inject application links into CDS index page!`);
143
+ const ul = doc.getElementsByTagName("ul")?.[0];
144
+ if (ul) {
145
+ const newLis = [];
146
+ const lis = ul.getElementsByTagName("li");
147
+ lis?.forEach((li) => {
148
+ const appDir = li.firstChild?.text?.split("/")?.[1];
149
+ if (localApps.has(appDir)) {
150
+ newLis.push(li.toString());
151
+ }
152
+ });
153
+ newLis.push(
154
+ ...links.sort().map((link) => {
155
+ // we remove the query parameters from the link text
156
+ const linkText = link.indexOf("?") === -1 ? link : link.substr(0, link.indexOf("?"));
157
+ // renders a UI5 link ;-)
158
+ return `<li><a class="ui5" href="${link}">${linkText}</a></li>`;
159
+ })
160
+ );
161
+ ul.innerHTML = newLis.join("\n");
162
+ } else {
163
+ log.warn(`Failed to inject application links into CDS index page!`);
164
+ }
162
165
  }
166
+ );
167
+ next();
168
+ });
169
+
170
+ // move our middleware before the CDS index serve middleware to
171
+ // allow that we can intercept the response and modify it to
172
+ // inject our application pages an remove the exitsing ones
173
+ const middlewareStack = app?._router?.stack;
174
+ if (Array.isArray(middlewareStack)) {
175
+ const cmw = middlewareStack.pop();
176
+ const idxOfServeStatic = middlewareStack.findIndex((layer) => layer.name === "serveStatic");
177
+ if (idxOfServeStatic !== -1) {
178
+ middlewareStack.splice(idxOfServeStatic, 0, cmw);
179
+ } else {
180
+ log.error(`Failed to determine welcome page middleware! The links of the application pages may not work properly!`);
163
181
  }
164
- );
165
- next();
166
- });
167
-
168
- // move our middleware before the CDS index serve middleware to
169
- // allow that we can intercept the response and modify it to
170
- // inject our application pages an remove the exitsing ones
171
- const middlewareStack = app?._router?.stack;
172
- if (Array.isArray(middlewareStack)) {
173
- const cmw = middlewareStack.pop();
174
- const idxOfServeStatic = middlewareStack.findIndex((layer) => layer.name === "serveStatic");
175
- if (idxOfServeStatic !== -1) {
176
- middlewareStack.splice(idxOfServeStatic, 0, cmw);
177
182
  } else {
178
- log.error(`Failed to determine welcome page middleware! The links of the application pages may not work properly!`);
183
+ log.error(`Failed to inject application pages to welcome page! The links of the application pages may not work properly!`);
179
184
  }
180
- } else {
181
- log.error(`Failed to inject application pages to welcome page! The links of the application pages may not work properly!`);
182
185
  }
186
+ } else {
187
+ log.info("Skip execution of plugin! The plugin is only active for 'cds serve'!");
183
188
  }
189
+
184
190
  // bootstrap completed, unlock the "served" event
185
191
  bootstrapped();
186
192
  });
@@ -16,6 +16,7 @@ const log = require("./log");
16
16
  * Returns all UI5 modules from local apps and the project dependencies.
17
17
  * @param {object} options configuration options
18
18
  * @param {string} options.cwd current working directory
19
+ * @param {string} options.cds reference to cds
19
20
  * @param {string} options.skipLocalApps skip local apps
20
21
  * @param {string} options.skipDeps skip dependencies
21
22
  * @returns {Array<UI5Module>} array of UI5 module
@@ -52,7 +53,7 @@ module.exports = async function findUI5Modules({ cwd, cds, skipLocalApps, skipDe
52
53
  const localApps = new Set();
53
54
  const appDirs = [];
54
55
  if (!skipLocalApps) {
55
- const appDir = path.join(cwd, cds?.env?.folders?.app || "app");
56
+ const appDir = path.join(cwd, cds.env?.folders?.app || "app");
56
57
  if (fs.existsSync(appDir)) {
57
58
  // is the UI5 app directly in the app directory?
58
59
  if (!fs.existsSync(path.join(appDir, determineUI5Yaml(appDir)))) {
@@ -61,15 +61,22 @@ module.exports = async function rewriteHTML(req, res, rewriteCondition, rewriteC
61
61
  // store the last chunk in the content buffer
62
62
  contentBuffer.push(content instanceof Buffer ? content.toString(encoding) : content);
63
63
 
64
- // create the html content and parse it
64
+ // create the html content
65
65
  let htmlContent = contentBuffer.join("");
66
- const doc = HTMLParser.parse(htmlContent);
67
66
 
68
- // now run the callback
69
- rewriteContent(doc);
67
+ // ensure that the request is rewritten only once and not also by the cds-plugin-ui5
68
+ // therefore we add a marker to the request so that the middlewares can identify this
69
+ if (!req["ui5-rewriteHTML"]) {
70
+ // parse the html content
71
+ const doc = HTMLParser.parse(htmlContent);
70
72
 
71
- // update the html content
72
- htmlContent = doc.toString();
73
+ // now run the callback
74
+ rewriteContent(doc);
75
+
76
+ // update the html content
77
+ htmlContent = doc.toString();
78
+ }
79
+ req["ui5-rewriteHTML"] = true;
73
80
 
74
81
  // the rest is on express
75
82
  end.call(res, htmlContent, encoding);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cds-plugin-ui5",
3
- "version": "0.7.5",
3
+ "version": "0.8.1",
4
4
  "description": "A CDS server plugin to inject the middlewares of all related UI5 tooling based projects.",
5
5
  "author": "Peter Muessig",
6
6
  "license": "Apache-2.0",
@@ -25,5 +25,5 @@
25
25
  "@sap/cds": ">=6.8.2",
26
26
  "express": ">=4.18.2"
27
27
  },
28
- "gitHead": "1f9cb2f35ac5cfbfeab842150d710e7edafe9b2f"
28
+ "gitHead": "c982231d4c48e09b2694a1124f032bb62267cd7c"
29
29
  }