diff --git a/.eslintrc.yml b/.eslintrc.yml new file mode 100644 index 0000000..cc17901 --- /dev/null +++ b/.eslintrc.yml @@ -0,0 +1,9 @@ +extends: eslint:recommended +env: + node: true + commonjs: true + jest: true + es2021: true +parserOptions: + ecmaVersion: latest +rules: {} diff --git a/cds-plugin.js b/cds-plugin.js index d247fed..1580b8f 100644 --- a/cds-plugin.js +++ b/cds-plugin.js @@ -1,24 +1,28 @@ -const cds = require("@sap/cds"); -const build = require('@sap/cds-dk/lib/build') -const { validateNotificationTypes, readFile } = require("./lib/utils"); -const { createNotificationTypesMap } = require("./lib/notificationTypes"); -const { setGlobalLogLevel } = require("@sap-cloud-sdk/util"); +const cds = require("@sap/cds/lib"); -// register build plugin -build.register('notifications', { impl: '@cap-js/notifications/lib/build', description: 'Notifications build plugin', taskDefaults: { src: cds.env.folders.srv } }); +if (cds.cli.command === "build") { + // register build plugin + const build = require('@sap/cds-dk/lib/build') + build.register('notifications', { + impl: '@cap-js/notifications/lib/build', + description: 'Notifications build plugin', + taskDefaults: { src: cds.env.folders.srv } + }) +} -cds.once("served", async () => { - setGlobalLogLevel("error"); - const profiles = cds.env.profiles ?? []; - const production = profiles.includes("production"); +else cds.once("served", async () => { + const { validateNotificationTypes, readFile } = require("./lib/utils"); + const { createNotificationTypesMap } = require("./lib/notificationTypes"); + const production = cds.env.profiles?.includes("production"); // read notification types const notificationTypes = readFile(cds.env.requires?.notifications?.types); - if (validateNotificationTypes(notificationTypes)) { if (!production) { const notificationTypesMap = createNotificationTypesMap(notificationTypes, true); cds.notifications = { local: { types: notificationTypesMap } }; } } -}); \ No newline at end of file + + require("@sap-cloud-sdk/util").setGlobalLogLevel("error") +}) diff --git a/jest.config.js b/jest.config.js index 6cc2dfc..3686759 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,4 @@ // FIXME: should not be necessary -process.env.CDS_ENV = 'better-sqlite' const config = { testTimeout: 42222, diff --git a/lib/notificationTypes.js b/lib/notificationTypes.js index aeac40a..2b00b18 100644 --- a/lib/notificationTypes.js +++ b/lib/notificationTypes.js @@ -1,7 +1,6 @@ const { executeHttpRequest } = require("@sap-cloud-sdk/http-client"); const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity"); const { getNotificationDestination, getPrefix, getNotificationTypesKeyWithPrefix } = require("./utils"); -const _ = require("lodash"); const NOTIFICATION_TYPES_API_ENDPOINT = "v2/NotificationType.svc"; const cds = require("@sap/cds"); const LOG = cds.log('notifications'); @@ -135,14 +134,14 @@ function _createChannelsMap(channels) { } function areDeliveryChannelsEqual(oldChannels, newChannels) { - if (_.size(oldChannels) !== _.size(newChannels)) { + if (oldChannels.length !== newChannels.length) { return false; } const oldChannelsMap = _createChannelsMap(oldChannels); const newChannelsMap = _createChannelsMap(newChannels); - for (type of Object.keys(oldChannelsMap)) { + for (let type of Object.keys(oldChannelsMap)) { if (!(type in newChannelsMap)) return false; const oldChannel = oldChannelsMap[type]; @@ -173,7 +172,7 @@ function isActionEqual(oldAction, newAction) { } function areActionsEqual(oldActions, newActions) { - if (_.size(oldActions) !== _.size(newActions)) { + if (oldActions.length !== newActions.length) { return false; } @@ -209,7 +208,7 @@ function isTemplateEqual(oldTemplate, newTemplate) { } function areTemplatesEqual(oldTemplates, newTemplates) { - if (_.size(oldTemplates) !== _.size(newTemplates)) { + if (oldTemplates.length !== newTemplates.length) { return false; } diff --git a/lib/utils.js b/lib/utils.js index e4c161f..8390937 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -22,7 +22,7 @@ const messages = { }; function validateNotificationTypes(notificationTypes) { - for(notificationType of notificationTypes){ + for(let notificationType of notificationTypes){ if (!("NotificationTypeKey" in notificationType)) { LOG._warn && LOG.warn(messages.INVALID_NOTIFICATION_TYPES); return false; diff --git a/package.json b/package.json index f22a471..c8887bb 100644 --- a/package.json +++ b/package.json @@ -16,10 +16,9 @@ "@sap/cds-dk": "^7.3.1" }, "dependencies": { - "@sap-cloud-sdk/connectivity": "3.5.0", - "@sap-cloud-sdk/http-client": "3.5.0", - "@sap-cloud-sdk/util": "3.5.0", - "lodash": "4.17.21" + "@sap-cloud-sdk/connectivity": "^3.5.0", + "@sap-cloud-sdk/http-client": "^3.5.0", + "@sap-cloud-sdk/util": "^3.5.0" }, "devDependencies": { "jest": "^29.7.0", diff --git a/srv/notifyToRest.js b/srv/notifyToRest.js index dcc5394..aa33316 100644 --- a/srv/notifyToRest.js +++ b/srv/notifyToRest.js @@ -3,6 +3,7 @@ const NotificationService = require("./service") const { buildHeadersForDestination } = require("@sap-cloud-sdk/connectivity"); const { executeHttpRequest } = require("@sap-cloud-sdk/http-client"); const { getNotificationDestination } = require("../lib/utils"); +const cds = require("@sap/cds"); const LOG = cds.log('notifications'); const NOTIFICATIONS_API_ENDPOINT = "v2/Notification.svc"; diff --git a/test/lib/content-deployment.test.js b/test/lib/content-deployment.test.js index fc000da..b893b73 100644 --- a/test/lib/content-deployment.test.js +++ b/test/lib/content-deployment.test.js @@ -1,5 +1,4 @@ // import required modules and functions -const cds = require("@sap/cds"); const { validateNotificationTypes, readFile } = require("../../lib/utils"); const { processNotificationTypes } = require("../../lib/notificationTypes"); const { setGlobalLogLevel } = require("@sap-cloud-sdk/util");