Load Services, Models and API from Core, Plugins and Project root (#23)

This commit is contained in:
Sebastian Rindom
2020-03-30 16:52:20 +02:00
committed by GitHub
parent 9740e9faac
commit 808d0912e3
19 changed files with 1356 additions and 359 deletions

View File

@@ -4,5 +4,5 @@
"packages/*"
],
"registry": "https://registry.npmjs.org/",
"version": "0.1.12-alpha.0"
"version": "0.1.26"
}

View File

@@ -1,6 +1,6 @@
{
"name": "medusa-core-utils",
"version": "0.1.6-alpha.0",
"version": "0.1.16",
"description": "Core utils for Medusa",
"main": "dist/index.js",
"repository": {
@@ -21,7 +21,8 @@
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"cross-env": "^5.2.1"
"cross-env": "^5.2.1",
"eslint": "^6.8.0"
},
"dependencies": {
"@hapi/joi": "^16.1.8",

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "medusa-interfaces",
"version": "0.1.6-alpha.0",
"version": "0.1.16",
"description": "Core interfaces for Medusa",
"main": "dist/index.js",
"repository": {
@@ -21,7 +21,8 @@
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"cross-env": "^5.2.1"
"cross-env": "^5.2.1",
"eslint": "^6.8.0"
},
"dependencies": {
"mongoose": "^5.8.0"

View File

@@ -6,6 +6,7 @@ node_modules
!index.js
yarn.lock
/api
/services
/models
/subscribers

View File

@@ -0,0 +1,9 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
yarn.lock

View File

@@ -1,6 +1,6 @@
{
"name": "medusa-payment-stripe",
"version": "0.1.6-alpha.0",
"version": "0.1.26",
"description": "Stripe Payment provider for Meduas Commerce",
"main": "index.js",
"repository": {
@@ -17,7 +17,8 @@
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"client-sessions": "^0.8.0",
"cross-env": "^5.2.1"
"cross-env": "^5.2.1",
"eslint": "^6.8.0"
},
"scripts": {
"build": "babel src --out-dir . --ignore **/__tests__",
@@ -26,7 +27,8 @@
},
"dependencies": {
"@babel/runtime": "^7.7.6",
"medusa-interfaces": "^0.1.6-alpha.0"
"express": "^4.17.1",
"medusa-interfaces": "^0.1.16"
},
"gitHead": "d3ae95ffe086bf40ffb92724afaefa38e4def25e"
"gitHead": "46685ed4c84f313ead741820cda9089465024344"
}

View File

@@ -0,0 +1,14 @@
import { Router } from "express"
export default () => {
const app = Router()
app.get("/stripe", (req, res) => {
console.log("hi")
res.json({
success: true
})
})
return app
}

View File

@@ -1,6 +1,6 @@
{
"name": "medusa-test-utils",
"version": "0.1.6-alpha.0",
"version": "0.1.16",
"description": "Test utils for Medusa",
"main": "dist/index.js",
"repository": {
@@ -21,7 +21,8 @@
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"cross-env": "^5.2.1"
"cross-env": "^5.2.1",
"eslint": "^6.8.0"
},
"dependencies": {
"mongoose": "^5.8.0"

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
{
"name": "@medusajs/medusa",
"version": "0.1.12-alpha.0",
"version": "0.1.26",
"description": "E-commerce for JAMstack",
"main": "dist/app.js",
"repository": {
@@ -23,7 +23,7 @@
"@babel/register": "^7.7.4",
"@babel/runtime": "^7.7.6",
"cross-env": "^5.2.1",
"eslint": "^6.7.2",
"eslint": "^6.8.0",
"jest": "^24.9.0",
"nodemon": "^2.0.1",
"prettier": "^1.19.1",
@@ -53,9 +53,9 @@
"glob": "^7.1.6",
"joi-objectid": "^3.0.1",
"jsonwebtoken": "^8.5.1",
"medusa-core-utils": "^0.1.6-alpha.0",
"medusa-interfaces": "^0.1.6-alpha.0",
"medusa-test-utils": "^0.1.6-alpha.0",
"medusa-core-utils": "^0.1.16",
"medusa-interfaces": "^0.1.16",
"medusa-test-utils": "^0.1.16",
"mongoose": "^5.8.0",
"morgan": "^1.9.1",
"passport": "^0.4.0",

View File

@@ -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))

View File

@@ -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
}

View File

@@ -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")
}

View File

@@ -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)

View File

@@ -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.

View File

@@ -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)

View File

@@ -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
}

View File

@@ -2222,10 +2222,10 @@ eslint-visitor-keys@^1.1.0:
resolved "https://registry.yarnpkg.com/eslint-visitor-keys/-/eslint-visitor-keys-1.1.0.tgz#e2a82cea84ff246ad6fb57f9bde5b46621459ec2"
integrity sha512-8y9YjtM1JBJU/A9Kc+SbaOV4y29sSWckBwMHa+FGtVj5gN/sbnKDf6xJUl+8g7FAij9LVaP8C24DUiH/f/2Z9A==
eslint@^6.7.2:
version "6.7.2"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-6.7.2.tgz#c17707ca4ad7b2d8af986a33feba71e18a9fecd1"
integrity sha512-qMlSWJaCSxDFr8fBPvJM9kJwbazrhNcBU3+DszDW1OlEwKBBRWsJc7NJFelvwQpanHCR14cOLD41x8Eqvo3Nng==
eslint@^6.8.0:
version "6.8.0"
resolved "https://registry.yarnpkg.com/eslint/-/eslint-6.8.0.tgz#62262d6729739f9275723824302fb227c8c93ffb"
integrity sha512-K+Iayyo2LtyYhDSYwz5D5QdWw0hCacNzyq1Y821Xna2xSJj7cijoLLYmLxTQgcgZ9mC61nryMy9S7GRbYpI5Ig==
dependencies:
"@babel/code-frame" "^7.0.0"
ajv "^6.10.0"
@@ -4089,6 +4089,28 @@ media-typer@0.3.0:
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
medusa-core-utils@^0.1.6-alpha.0:
version "0.1.6-alpha.0"
resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-0.1.6-alpha.0.tgz#393a0cdcfdd7833a1c0c5ddcee5fe71578563a6f"
integrity sha512-1/8LsCvVnNwv95J5Y2OcEoWP6IkgpqIQi8fQk7igS3frWDUi2sU0MuSQUqiOLrkEqVCNkBs8s0j/NKAu1Ld40A==
dependencies:
"@hapi/joi" "^16.1.8"
joi-objectid "^3.0.1"
medusa-interfaces@^0.1.6-alpha.0:
version "0.1.6-alpha.0"
resolved "https://registry.yarnpkg.com/medusa-interfaces/-/medusa-interfaces-0.1.6-alpha.0.tgz#cc6357e5605382775aa22e10b318cde966259c9e"
integrity sha512-sVrR/FnaA+OCcJrVvfdrBuyBOs/Y01vbftqldM76WwIptkzcl6O+w2jGLn/YcMj4wswUYnhnPSc8YfmYjEgBMg==
dependencies:
mongoose "^5.8.0"
medusa-test-utils@^0.1.6-alpha.0:
version "0.1.6-alpha.0"
resolved "https://registry.yarnpkg.com/medusa-test-utils/-/medusa-test-utils-0.1.6-alpha.0.tgz#bfdf4a456904a7a3d4d78da0bb126c91d51ee61f"
integrity sha512-yizJyQF+xJSHtD+BgyGiZFv0EE3+TQDtIZJZEjisIsN0nzzZTOb/VYY8dgQWLFZdkumruiJwq4NCx7Dxp6CfNA==
dependencies:
mongoose "^5.8.0"
memory-pager@^1.0.2:
version "1.5.0"
resolved "https://registry.yarnpkg.com/memory-pager/-/memory-pager-1.5.0.tgz#d8751655d22d384682741c972f2c3d6dfa3e66b5"