Load Services, Models and API from Core, Plugins and Project root (#23)
This commit is contained in:
1 parent
9740e9faac
commit
808d0912e3
19 files changed
+1356
-359
No files matched your search
@@ -4,6 +4,11 @@ import middlewares from "../../../middlewares"
|
||||
const route = Router()
|
||||
|
||||
export default app => {
|
||||
// Inject <rootDir>/api/routes/admin/products/router.js
|
||||
// Inject <rootDir>/plugins/*/api/routes/admin/products/router.js
|
||||
// Inject <rootDir>/node_modules/*/api/routes/admin/products/router.js
|
||||
|
||||
|
||||
app.use("/products", route)
|
||||
|
||||
route.post("/", middlewares.wrap(require("./create-product").default))
|
||||
|
||||
@@ -1,6 +1,20 @@
|
||||
import routes from "../api"
|
||||
|
||||
import glob from "glob"
|
||||
import path from "path"
|
||||
|
||||
export default async ({ app }) => {
|
||||
const corePath = path.join(__dirname, "../api")
|
||||
const appPath = path.resolve("src/api")
|
||||
|
||||
if (corePath !== appPath) {
|
||||
const appRoutes = require(appPath).default
|
||||
if (appRoutes) {
|
||||
app.use("/", appRoutes())
|
||||
}
|
||||
}
|
||||
|
||||
app.use("/", routes())
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -36,9 +36,6 @@ export default async ({ expressApp }) => {
|
||||
await servicesLoader({ container })
|
||||
Logger.info("Services initialized")
|
||||
|
||||
await pluginsLoader({ container })
|
||||
Logger.info("Plugins Intialized")
|
||||
|
||||
await mongooseLoader()
|
||||
Logger.info("MongoDB Intialized")
|
||||
|
||||
@@ -54,6 +51,9 @@ export default async ({ expressApp }) => {
|
||||
next()
|
||||
})
|
||||
|
||||
await pluginsLoader({ container, app: expressApp })
|
||||
Logger.info("Plugins Intialized")
|
||||
|
||||
await apiLoader({ app: expressApp })
|
||||
Logger.info("API initialized")
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ export default ({ container }) => {
|
||||
let corePath = "../models/*.js"
|
||||
let appPath = "src/models/*.js"
|
||||
|
||||
const corefull = path.resolve(corePath)
|
||||
const appfull = path.resolve(corePath)
|
||||
const coreFull = path.join(__dirname, corePath)
|
||||
const appFull = path.resolve(appPath)
|
||||
|
||||
const core = glob.sync(corePath, { cwd: __dirname })
|
||||
const core = glob.sync(coreFull, { cwd: __dirname })
|
||||
core.forEach(fn => {
|
||||
const loaded = require(fn).default
|
||||
const name = formatRegistrationName(fn)
|
||||
@@ -23,8 +23,8 @@ export default ({ container }) => {
|
||||
})
|
||||
})
|
||||
|
||||
if (corefull !== appfull) {
|
||||
const files = glob.sync(appPath)
|
||||
if (coreFull !== appFull) {
|
||||
const files = glob.sync(appFull)
|
||||
files.forEach(fn => {
|
||||
const loaded = require(fn).default
|
||||
|
||||
@@ -44,11 +44,13 @@ export default ({ container }) => {
|
||||
}
|
||||
|
||||
function formatRegistrationName(fn) {
|
||||
const offset = process.env.NODE_ENV === "test" ? 3 : 2
|
||||
|
||||
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 namespace = splat[splat.length - offset]
|
||||
const upperNamespace =
|
||||
namespace.charAt(0).toUpperCase() + namespace.slice(1, -1)
|
||||
|
||||
|
||||
@@ -5,12 +5,14 @@ import path from "path"
|
||||
import fs from "fs"
|
||||
import { asFunction } from "awilix"
|
||||
import { sync as existsSync } from "fs-exists-cached"
|
||||
import { plugins } from "../../medusa-config.js"
|
||||
|
||||
/**
|
||||
* Registers all services in the services directory
|
||||
*/
|
||||
export default ({ container }) => {
|
||||
export default ({ container, app }) => {
|
||||
const configPath = path.resolve("./medusa-config")
|
||||
const { plugins } = require(configPath)
|
||||
|
||||
const resolved = plugins.map(plugin => {
|
||||
if (_.isString(plugin)) {
|
||||
return resolvePlugin(plugin)
|
||||
@@ -25,9 +27,20 @@ export default ({ container }) => {
|
||||
resolved.forEach(pluginDetails => {
|
||||
registerServices(pluginDetails, container)
|
||||
registerModels(pluginDetails, container)
|
||||
registerApi(pluginDetails, app)
|
||||
})
|
||||
}
|
||||
|
||||
function registerApi(pluginDetails, app) {
|
||||
try {
|
||||
const routes = require(`${pluginDetails.resolve}/api`).default
|
||||
app.use("/", routes())
|
||||
return app
|
||||
} catch (err) {
|
||||
return app
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a service at the right location in our container. If the service is
|
||||
* a BaseService instance it will be available directly from the container.
|
||||
|
||||
@@ -16,10 +16,10 @@ export default ({ container }) => {
|
||||
appPath = "src/services/__mocks__/*.js"
|
||||
}
|
||||
|
||||
const corefull = path.resolve(corePath)
|
||||
const appfull = path.resolve(corePath)
|
||||
const coreFull = path.join(__dirname, corePath)
|
||||
const appFull = path.resolve(appPath)
|
||||
|
||||
const core = glob.sync(corePath, { cwd: __dirname })
|
||||
const core = glob.sync(coreFull, { cwd: __dirname })
|
||||
core.forEach(fn => {
|
||||
const loaded = require(fn).default
|
||||
const name = formatRegistrationName(fn)
|
||||
@@ -28,8 +28,8 @@ export default ({ container }) => {
|
||||
})
|
||||
})
|
||||
|
||||
if (corefull !== appfull) {
|
||||
const files = glob.sync(appPath)
|
||||
if (coreFull !== appFull) {
|
||||
const files = glob.sync(appFull)
|
||||
files.forEach(fn => {
|
||||
const loaded = require(fn).default
|
||||
|
||||
@@ -63,11 +63,13 @@ export default ({ container }) => {
|
||||
}
|
||||
|
||||
function formatRegistrationName(fn) {
|
||||
const offset = process.env.NODE_ENV === "test" ? 3 : 2
|
||||
|
||||
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 namespace = splat[splat.length - offset]
|
||||
const upperNamespace =
|
||||
namespace.charAt(0).toUpperCase() + namespace.slice(1, -1)
|
||||
|
||||
|
||||
@@ -8,12 +8,7 @@ import { Validator, MedusaError } from "medusa-core-utils"
|
||||
*/
|
||||
class ProductVariantService extends BaseService {
|
||||
/** @param { productVariantModel: (ProductVariantModel) } */
|
||||
constructor({
|
||||
productVariantModel,
|
||||
eventBusService,
|
||||
productService,
|
||||
regionService,
|
||||
}) {
|
||||
constructor({ productVariantModel, eventBusService, regionService }) {
|
||||
super()
|
||||
|
||||
/** @private @const {ProductVariantModel} */
|
||||
@@ -22,9 +17,6 @@ class ProductVariantService extends BaseService {
|
||||
/** @private @const {EventBus} */
|
||||
this.eventBus_ = eventBusService
|
||||
|
||||
/** @private @const {ProductService} */
|
||||
this.productService_ = productService
|
||||
|
||||
/** @private @const {RegionService} */
|
||||
this.regionService_ = regionService
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user