From 763ae82affecba946ce89b2ad992596317fd53dd Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Tue, 24 Mar 2020 14:24:23 +0100 Subject: [PATCH] Imports core services and app services separately (#19) --- packages/medusa/src/loaders/services.js | 91 ++++++++++++++++++------- 1 file changed, 66 insertions(+), 25 deletions(-) diff --git a/packages/medusa/src/loaders/services.js b/packages/medusa/src/loaders/services.js index 5bd290af58..b67183d3b7 100644 --- a/packages/medusa/src/loaders/services.js +++ b/packages/medusa/src/loaders/services.js @@ -1,40 +1,81 @@ +import { BaseService, PaymentService } from "medusa-interfaces" +import glob from "glob" +import path from "path" import { Lifetime } from "awilix" +import { asFunction } from "awilix" /** * Registers all services in the services directory */ export default ({ container }) => { - let loadPath = "src/services/*.js" + let corePath = "../services/*.js" + let appPath = "src/services/*.js" if (process.env.NODE_ENV === "test") { - loadPath = "src/services/__mocks__/*.js" + corePath = "../services/__mocks__/*.js" + appPath = "src/services/__mocks__/*.js" } - // service/auth.js -> authService - container.loadModules([loadPath], { - resolverOptions: { - lifetime: Lifetime.SINGLETON, - }, - formatName: (rawname, descriptor) => { - const parts = rawname.split("-").map((n, index) => { - if (index !== 0) { - return n.charAt(0).toUpperCase() + n.slice(1) - } - return n - }) - const name = parts.join("") + const corefull = path.resolve(corePath) + const appfull = path.resolve(corePath) - const splat = descriptor.path.split("/") + const core = glob.sync(corePath, { cwd: __dirname }) + core.forEach(fn => { + const loaded = require(fn).default + const name = formatRegistrationName(fn) + container.register({ + [name]: asFunction(cradle => new loaded(cradle)), + }) + }) - let offset = 2 - if (process.env.NODE_ENV === "test") { - offset = 3 + if (corefull !== appfull) { + const files = glob.sync(appPath) + files.forEach(fn => { + const loaded = require(fn).default + + if (!(loaded.prototype instanceof BaseService)) { + const logger = container.resolve("logger") + const message = `Models must inherit from BaseModel, please check ${fn}` + logger.error(message) + throw new Error(message) } - const namespace = splat[splat.length - offset] - const upperNamespace = - namespace.charAt(0).toUpperCase() + namespace.slice(1, -1) - return name + upperNamespace - }, - }) + if (loaded.prototype instanceof PaymentService) { + // Register our payment providers to paymentProviders + container.registerAdd( + "paymentProviders", + asFunction(cradle => new loaded(cradle)) + ) + + // Add the service directly to the container in order to make simple + // resolution if we already know which payment provider we need to use + container.register({ + [`pp_${loaded.identifier}`]: asFunction(cradle => new loaded(cradle)), + }) + } else { + const name = formatRegistrationName(fn) + container.register({ + [name]: asFunction(cradle => new loaded(cradle)), + }) + } + }) + } +} + +function formatRegistrationName(fn) { + const descriptorIndex = fn.split(".").length - 2 + const descriptor = fn.split(".")[descriptorIndex] + const splat = descriptor.split("/") + const rawname = splat[splat.length - 1] + const namespace = splat[splat.length - 2] + const upperNamespace = + namespace.charAt(0).toUpperCase() + namespace.slice(1, -1) + + const parts = rawname.split("-").map((n, index) => { + if (index !== 0) { + return n.charAt(0).toUpperCase() + n.slice(1) + } + return n + }) + return parts.join("") + upperNamespace }