Skip to content

Commit

Permalink
Feature: Add the ability to ignore services
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mohsen-karami committed Jul 2, 2023
1 parent 275eda5 commit b5695ff
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
22 changes: 17 additions & 5 deletions src/service-broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>} [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));

Expand Down
21 changes: 20 additions & 1 deletion test/unit/service-broker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1159,14 +1159,33 @@ 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);
expect(broker.loadService).toHaveBeenCalledTimes(1);
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();
Expand Down

0 comments on commit b5695ff

Please sign in to comment.