Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add the ability to ignore services #1223

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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