From b5695ff9c63ab7e0d1cd7244b4c0248dbcf7c558 Mon Sep 17 00:00:00 2001 From: Mohsen Karami Date: Sun, 2 Jul 2023 15:26:39 +0330 Subject: [PATCH] Feature: Add the ability to ignore services There are situations in that the user may temporarily want to ignore some services; this feature allows them to do this by simply adding them as an array and putting it as the third parameter of the loadServices method. --- src/service-broker.js | 22 +++++++++++++++++----- test/unit/service-broker.spec.js | 21 ++++++++++++++++++++- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/src/service-broker.js b/src/service-broker.js index 3777c6b9e..f784dd56e 100644 --- a/src/service-broker.js +++ b/src/service-broker.js @@ -740,19 +740,31 @@ class ServiceBroker { /** * Load services from a folder * - * @param {string} [folder="./services"] Folder of services + * @param {string} [folder="./services"] Folder of services * @param {string} [fileMask="**\/*.service.js"] Service filename mask - * @returns {Number} Number of found services + * @param {array} [excludedFiles] Excluded files + * @returns {Number} Number of found services * * @memberof ServiceBroker */ - loadServices(folder = "./services", fileMask = "**/*.service.js") { + loadServices(folder = "./services", fileMask = "**/*.service.js", excludedFiles) { this.logger.debug(`Search services in '${folder}/${fileMask}'...`); let serviceFiles; - if (Array.isArray(fileMask)) serviceFiles = fileMask.map(f => path.join(folder, f)); - else serviceFiles = glob.sync(path.join(folder, fileMask)); + if (Array.isArray(fileMask)) { + serviceFiles = fileMask.map(f => path.join(folder, f)); + } else { + if (excludedFiles) { + let ignoredFiles = []; + excludedFiles.forEach(excludedFile => { + ignoredFiles.push(path.join(folder, excludedFile)); + }); + serviceFiles = glob.sync(path.join(folder, fileMask), { ignore: ignoredFiles }); + } else { + serviceFiles = glob.sync(path.join(folder, fileMask)); + } + } if (serviceFiles) serviceFiles.forEach(filename => this.loadService(filename)); diff --git a/test/unit/service-broker.spec.js b/test/unit/service-broker.spec.js index a51e041d1..05d2cb089 100644 --- a/test/unit/service-broker.spec.js +++ b/test/unit/service-broker.spec.js @@ -1159,7 +1159,7 @@ describe("Test loadServices", () => { expect(broker.loadService).toHaveBeenCalledWith("test/services/greeter.es6.service.js"); }); - it("should load 1 services", () => { + it("should load 1 service", () => { broker.loadService.mockClear(); const count = broker.loadServices("./test/services", "users.*.js"); expect(count).toBe(1); @@ -1167,6 +1167,25 @@ describe("Test loadServices", () => { expect(broker.loadService).toHaveBeenCalledWith("test/services/users.service.js"); }); + it("should ignore 1 service and load 3 services", () => { + broker.loadService.mockClear(); + const count = broker.loadServices("./test/services", "*.service.js", ["math.service.js"]); + expect(count).toBe(3); + expect(broker.loadService).toHaveBeenCalledTimes(3); + expect(broker.loadService).toHaveBeenCalledWith("test/services/users.service.js"); + expect(broker.loadService).toHaveBeenCalledWith("test/services/posts.service.js"); + expect(broker.loadService).toHaveBeenCalledWith("test/services/greeter.es6.service.js"); + }); + + it("should ignore 2 services and load 2 services", () => { + broker.loadService.mockClear(); + const count = broker.loadServices("./test/services", "*.service.js", ["math.service.js", "users.service.js"]); + expect(count).toBe(2); + expect(broker.loadService).toHaveBeenCalledTimes(2); + expect(broker.loadService).toHaveBeenCalledWith("test/services/posts.service.js"); + expect(broker.loadService).toHaveBeenCalledWith("test/services/greeter.es6.service.js"); + }); + it("should load 0 services", () => { broker.loadService.mockClear(); const count = broker.loadServices();