Skip to content

Commit

Permalink
replace console.error with Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
aramovic79 committed Nov 13, 2024
1 parent 1bfdd74 commit 41e2e50
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
3 changes: 2 additions & 1 deletion __tests__/bookshop/.cdsrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"description": "this is my custom description",
"policyLevel": "sap:core:v1",
"customOrdContentFile": "./ord/custom.ord.json"
}
},
"DEBUG": false
}
2 changes: 1 addition & 1 deletion __tests__/unittest/extendOrdWithCustom.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ jest.mock("@sap/cds", () => {
...actualCds,
env: {},
log: jest.fn(() => ({
levels: { DEBUG: 0, WARN: 1 },
warn: jest.fn(() => console.warn('Mocked warning')),
})),
};
});

describe('extendOrdWithCustom', () => {

let appConfig = {};

beforeEach(() => {
Expand Down
10 changes: 6 additions & 4 deletions lib/extendOrdWithCustom.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { CONTENT_MERGE_KEY } = require('./constants');
const cds = require("@sap/cds");
const { Logger } = require("./logger");
const path = require("path");
const _ = require('lodash');

Expand Down Expand Up @@ -29,12 +30,13 @@ function patchGeneratedOrdResources(destinationObj, sourceObj) {
return cleanNullProperties(Object.values(destObj));
}

function compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent, logger) {
function compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent) {
const clonedOrdContent = structuredClone(ordContent);
for (const key in customORDContent) {
const propertyType = typeof customORDContent[key];
if (propertyType !== 'object' && propertyType !== 'array') {
logger.warn('Found ord top level primitive ord property in customOrdFile:', key, ', please define it in .cdsrc.json.');
Logger.warn(`Found ord top level primitive ord property in customOrdFile: ${key}. Please define it in .cdsrc.json.`);

continue;
}
if (key in ordContent) {
Expand All @@ -54,11 +56,11 @@ function getCustomORDContent(appConfig) {
return {};
}

function extendCustomORDContentIfExists(appConfig, ordContent, logger) {
function extendCustomORDContentIfExists(appConfig, ordContent) {
const customORDContent = getCustomORDContent(appConfig);

if (customORDContent) {
ordContent = compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent, logger);
ordContent = compareAndHandleCustomORDContentWithExistingContent(ordContent, customORDContent);
}
return ordContent;
}
Expand Down
11 changes: 11 additions & 0 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const cds = require("@sap/cds");

const Logger = cds.log("ord-plugin", {
level: cds.env.DEBUG || process.env.DEBUG
? cds.log.levels?.DEBUG
: cds.log.levels?.WARN,
});

module.exports = {
Logger,
};
7 changes: 4 additions & 3 deletions lib/metaData.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const cds = require('@sap/cds/lib');
const { compile: openapi } = require('@cap-js/openapi')
const { compile: asyncapi } = require('@cap-js/asyncapi');
const { COMPILER_TYPES } = require('./constants');
const { Logger } = require('./logger');

module.exports = async (data) => {
const parts = data?.split("/").pop().replace(/\.json$/, '').split(".");
Expand All @@ -16,23 +17,23 @@ module.exports = async (data) => {
try {
responseFile = openapi(csn, options);
} catch (error) {
console.error("OpenApi error:", error.message);
Logger.error("OpenApi error:", error.message);
throw error;
}
break;
case COMPILER_TYPES.asyncapi2:
try {
responseFile = asyncapi(csn, options);
} catch (error) {
console.error("AsyncApi error:", error.message);
Logger.error("AsyncApi error:", error.message);
throw error;
}
break;
case COMPILER_TYPES.edmx:
try {
responseFile = await cds.compile(csn).to["edmx"](options);
} catch (error) {
console.error("Edmx error:", error.message);
Logger.error("Edmx error:", error.message);
throw error;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/ord.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const {
} = require('./templates');
const { extendCustomORDContentIfExists } = require('./extendOrdWithCustom');
const { getRFC3339Date } = require('./date');
const { Logger } = require('./logger');
const _ = require("lodash");
const cds = require("@sap/cds");
const defaults = require("./defaults");
const logger = cds.log('ord-plugin'); // TODO
const path = require("path");

const initializeAppConfig = (csn) => {
Expand All @@ -34,7 +34,7 @@ const initializeAppConfig = (csn) => {
const eventApplicationNamespace = cds.env?.export?.asyncapi?.applicationNamespace;

if (eventApplicationNamespace && ordNamespace !== eventApplicationNamespace) {
console.warn("ORD and AsyncAPI namespaces should be the same.");
Logger.warn('ORD and AsyncAPI namespaces should be the same.');
}

for (const key of modelKeys) {
Expand Down Expand Up @@ -123,7 +123,7 @@ function validateNamespace(appConfig) {
let error = new Error(
`Namespace is not defined in cdsrc.json or it is not in the format of ${appConfig.eventApplicationNamespace}.<appName>.<service>`
);
console.error("Namespace error:", error.message);
Logger.error("Namespace error:", error.message);
throw error;
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ module.exports = (csn) => {
ordDocument.eventResources = eventResources;
}

ordDocument = extendCustomORDContentIfExists(appConfig, ordDocument, logger);
ordDocument = extendCustomORDContentIfExists(appConfig, ordDocument);

return ordDocument;
};
10 changes: 5 additions & 5 deletions lib/plugin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const { ord, getMetadata, defaults } = require("./");
const cds = require("@sap/cds");
const { Logger } = require("./logger");
const { ord, getMetadata, defaults } = require("./");


cds.on("bootstrap", (app) => {
app.use("/.well-known/open-resource-discovery", async (req, res) => {
Expand All @@ -10,8 +12,7 @@ cds.on("bootstrap", (app) => {
const { contentType, response } = await getMetadata(req.url);
res.status(200).contentType(contentType).send(response);
} catch (error) {
console.log(error);
console.log('Error while generating metadata');
Logger.error(error, 'Error while generating metadata');
res.status(500).send(error.message);
}
}
Expand All @@ -23,8 +24,7 @@ cds.on("bootstrap", (app) => {
const data = ord(csn);
return res.status(200).send(data);
} catch (error) {
console.log(error);
console.log('Error while creating ORD document');
Logger.error(error, 'Error while creating ORD document');
return res.status(500).send(error.message);
}
});
Expand Down
5 changes: 3 additions & 2 deletions lib/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {
RESOURCE_VISIBILITY,
SHORT_DESCRIPTION_PREFIX
} = require("./constants");
const { Logger } = require("./logger");

function unflatten(flattedObject) {
let result = {}
Expand Down Expand Up @@ -46,7 +47,7 @@ const _generatePaths = (srv, srvDefinition) => {
//removing instances of graphql protocol from paths
for (var index = paths.length - 1; index >= 0; index--) {
if (paths[index].kind === "graphql") {
console.warn("Graphql protocol is not supported.");
Logger.warn('Graphql protocol is not supported.');
paths.splice(index, 1);
}
}
Expand Down Expand Up @@ -106,7 +107,7 @@ const createGroupsTemplateForService = (serviceName, serviceDefinition, appConfi
const ordExtensions = readORDExtensions(serviceDefinition);

if (!serviceDefinition) {
console.warn("Unable to find service definition:", serviceName)
Logger.warn('Unable to find service definition:', serviceName)
return undefined
}

Expand Down

0 comments on commit 41e2e50

Please sign in to comment.