Imports core services and app services separately (#19)

This commit is contained in:
Sebastian Rindom
2020-03-24 14:24:23 +01:00
committed by GitHub
parent f50de7fac3
commit 763ae82aff
+66 -25
View File
@@ -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
}