Skip to content

Commit

Permalink
dev-approuter: improve routes injection and logging (#1113)
Browse files Browse the repository at this point in the history
  • Loading branch information
vobu authored Nov 3, 2024
1 parent c6f7ce2 commit a43e17d
Show file tree
Hide file tree
Showing 5 changed files with 1,468 additions and 901 deletions.
13 changes: 8 additions & 5 deletions packages/dev-approuter/lib/devApprouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ const approuter = require("@sap/approuter");
const express = require("express");
const xsenv = require("@sap/xsenv");

const LOG = require("@sap/cds").log("dev-approuter") || console;

const findUI5Modules = require("cds-plugin-ui5/lib/findUI5Modules");
const createPatchedRouter = require("cds-plugin-ui5/lib/createPatchedRouter");
const applyUI5Middleware = require("cds-plugin-ui5/lib/applyUI5Middleware");

const findCDSModules = require("ui5-middleware-cap/lib/findCDSModules");
const applyCDSMiddleware = require("ui5-middleware-cap/lib/applyCDSMiddleware");

Expand Down Expand Up @@ -91,7 +92,7 @@ class DevApprouter {
});

// mounting the router for the UI5 application to the CDS server
console.log(`Mounting ${mountPath} to UI5 app ${modulePath}`);
LOG.info(`Mounting ${mountPath} to UI5 app ${modulePath}`);

let middlewareMountPath;
// define middlewareMountPath as `/_${mountPath}` if ui5 module is referenced as "dependency" in xs-dev.json or xs-app.json
Expand Down Expand Up @@ -126,7 +127,7 @@ class DevApprouter {
app._listen = app.listen;
app.listen = function (port, callback) {
return this._listen(cdsPort, function () {
console.log(`CDS server started at: http://localhost:${cdsPort}`);
LOG.info(`CDS server started at: http://localhost:${cdsPort}`);
callback?.apply(callback, arguments);
});
};
Expand All @@ -144,7 +145,9 @@ class DevApprouter {
dependency: moduleId,
authenticationType: "none",
};
config.routes.unshift(Object.assign({}, route));
// respect already declared routes in xs-dev.json
// so inserting these auto-generated ones after
config.routes.push(Object.assign({}, route));
config.dependencyRoutes[moduleId] = configureCDSRoute(moduleId, servicesPaths, route);
} else {
config.dependencyRoutes[moduleId] = configureCDSRoute(moduleId, servicesPaths, config.dependencyRoutes[moduleId]);
Expand All @@ -167,7 +170,7 @@ class DevApprouter {
},
].concat(extensions),
});
console.log(`Approuter started at: http://localhost:${arPort}`);
LOG.info(`Approuter started at: http://localhost:${arPort}`);
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/dev-approuter/lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const path = require("path");
const fs = require("fs");

const LOG = require("@sap/cds").log("dev-approuter") || console;

/**
* Parses the approuter configuration from an `xs-dev.json` file.
* If that file doesn't exist, it looks for an `xs-app.json` file.
Expand All @@ -16,6 +18,7 @@ const parseConfig = () => {
if (fs.existsSync(path.join(process.cwd(), file))) {
config = JSON.parse(fs.readFileSync(path.join(process.cwd(), file), { encoding: "utf8" }));
configFile = file;
LOG.info(`Using configuration from ${path.join(process.cwd(), configFile)}`);
break;
}
}
Expand Down Expand Up @@ -47,6 +50,7 @@ const applyDependencyConfig = (config) => {
}
});
delete config.dependencyRoutes;
LOG.info("running these routes:", config.routes);
return config;
};

Expand Down Expand Up @@ -83,6 +87,7 @@ const addDestination = (moduleId, port, mountPath) => {
});
process.env.destinations = JSON.stringify(destinations);
}
LOG.info("running these destinations:", JSON.parse(process.env.destinations));
};

/**
Expand Down
8 changes: 8 additions & 0 deletions packages/dev-approuter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@
"cds-plugin-ui5": "workspace:^",
"express": "^4.21.1",
"ui5-middleware-cap": "workspace:^"
},
"peerDependencies": {
"@sap/cds": "*"
},
"peerDependenciesMeta": {
"@sap/cds": {
"optional": true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19444,7 +19444,7 @@ sap.ui.define((function () { 'use strict';
}

// https://github.com/felixge/node-form-data/issues/38
if (Array.isArray(value)) {
if (util.isArray(value)) {
// Please convert your array into string
// the way web server expects it
this._error(new Error('Arrays are not supported.'));
Expand Down
Loading

0 comments on commit a43e17d

Please sign in to comment.