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 +23 -0
- package/cds-plugin.js +108 -102
- package/lib/findUI5Modules.js +2 -1
- package/lib/rewriteHTML.js +13 -6
- package/package.json +2 -2
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
160
|
-
|
|
161
|
-
|
|
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
|
|
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
|
});
|
package/lib/findUI5Modules.js
CHANGED
|
@@ -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
|
|
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)))) {
|
package/lib/rewriteHTML.js
CHANGED
|
@@ -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
|
|
64
|
+
// create the html content
|
|
65
65
|
let htmlContent = contentBuffer.join("");
|
|
66
|
-
const doc = HTMLParser.parse(htmlContent);
|
|
67
66
|
|
|
68
|
-
//
|
|
69
|
-
|
|
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
|
-
|
|
72
|
-
|
|
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.
|
|
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": "
|
|
28
|
+
"gitHead": "c982231d4c48e09b2694a1124f032bb62267cd7c"
|
|
29
29
|
}
|