Merge pull request #269 from medusajs/fix/registration-name
fix(medusa): registration names
This commit is contained in:
@@ -3,6 +3,8 @@ import path from "path"
|
|||||||
import { EntitySchema } from "typeorm"
|
import { EntitySchema } from "typeorm"
|
||||||
import { asClass, asValue } from "awilix"
|
import { asClass, asValue } from "awilix"
|
||||||
|
|
||||||
|
import formatRegistrationName from "../utils/format-registration-name"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers all models in the model directory
|
* Registers all models in the model directory
|
||||||
*/
|
*/
|
||||||
@@ -34,23 +36,3 @@ export default ({ container }, config = { register: true }) => {
|
|||||||
|
|
||||||
return toReturn
|
return toReturn
|
||||||
}
|
}
|
||||||
|
|
||||||
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 - offset]
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ import fs from "fs"
|
|||||||
import { asValue, asClass, asFunction, aliasTo } from "awilix"
|
import { asValue, asClass, asFunction, aliasTo } from "awilix"
|
||||||
import { sync as existsSync } from "fs-exists-cached"
|
import { sync as existsSync } from "fs-exists-cached"
|
||||||
|
|
||||||
|
import formatRegistrationName from "../utils/format-registration-name"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers all services in the services directory
|
* Registers all services in the services directory
|
||||||
*/
|
*/
|
||||||
@@ -360,29 +362,6 @@ function registerModels(pluginDetails, container) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Formats a filename into the correct container resolution name.
|
|
||||||
* Names are camelCase formatted and namespaced by the folder i.e:
|
|
||||||
* models/example-person -> examplePersonModel
|
|
||||||
* @param {string} fn - the full path of the file
|
|
||||||
* @return {string} the formatted name
|
|
||||||
*/
|
|
||||||
function formatRegistrationName(fn) {
|
|
||||||
const descriptor = fn.split(".")[0]
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: Create unique id for each plugin
|
// TODO: Create unique id for each plugin
|
||||||
function createPluginId(name) {
|
function createPluginId(name) {
|
||||||
return name
|
return name
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ import glob from "glob"
|
|||||||
import path from "path"
|
import path from "path"
|
||||||
import { Lifetime, asClass, asValue } from "awilix"
|
import { Lifetime, asClass, asValue } from "awilix"
|
||||||
|
|
||||||
|
import formatRegistrationName from "../utils/format-registration-name"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers all models in the model directory
|
* Registers all models in the model directory
|
||||||
*/
|
*/
|
||||||
@@ -23,23 +25,3 @@ 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 - offset]
|
|
||||||
const upperNamespace = "Repository"
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,6 +3,8 @@ import glob from "glob"
|
|||||||
import path from "path"
|
import path from "path"
|
||||||
import { Lifetime, asFunction } from "awilix"
|
import { Lifetime, asFunction } from "awilix"
|
||||||
|
|
||||||
|
import formatRegistrationName from "../utils/format-registration-name"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Registers all services in the services directory
|
* Registers all services in the services directory
|
||||||
*/
|
*/
|
||||||
@@ -23,23 +25,3 @@ export default ({ container, configModule }) => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
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 - offset]
|
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import path from "path"
|
||||||
|
import formatRegistrationName from "../format-registration-name"
|
||||||
|
|
||||||
|
describe("formatRegistrationName", () => {
|
||||||
|
const tests = [
|
||||||
|
[["medusa-test-dir", "dist", "services", "my-test.js"], "myTestService"],
|
||||||
|
[["medusa-test-dir", "dist", "services", "my.js"], "myService"],
|
||||||
|
[["services", "my-quite-long-file.js"], "myQuiteLongFileService"],
|
||||||
|
[
|
||||||
|
["/", "Users", "seb", "com.medusa.js", "services", "dot.js"],
|
||||||
|
"dotService",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
["/", "Users", "seb.rin", "com.medusa.js", "services", "dot.js"],
|
||||||
|
"dotService",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
["/", "Users", "seb.rin", "com.medusa.js", "repositories", "dot.js"],
|
||||||
|
"dotRepository",
|
||||||
|
],
|
||||||
|
[
|
||||||
|
["/", "Users", "seb.rin", "com.medusa.js", "models", "dot.js"],
|
||||||
|
"dotModel",
|
||||||
|
],
|
||||||
|
[["C:", "server", "services", "dot.js"], "dotService"],
|
||||||
|
]
|
||||||
|
|
||||||
|
test.each(
|
||||||
|
tests.map(([pathParts, expected]) => [path.join(...pathParts), expected])
|
||||||
|
)("Service %s -> %s", (fn, expected) => {
|
||||||
|
const res = formatRegistrationName(fn)
|
||||||
|
expect(res).toEqual(expected)
|
||||||
|
})
|
||||||
|
})
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
import path from "path"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Formats a filename into the correct container resolution name.
|
||||||
|
* Names are camelCase formatted and namespaced by the folder i.e:
|
||||||
|
* models/example-person -> examplePersonModel
|
||||||
|
* @param {string} fn - the full path of the file
|
||||||
|
* @return {string} the formatted name
|
||||||
|
*/
|
||||||
|
function formatRegistrationName(fn) {
|
||||||
|
const parsed = path.parse(fn)
|
||||||
|
const parsedDir = path.parse(parsed.dir)
|
||||||
|
|
||||||
|
const rawname = parsed.name
|
||||||
|
let namespace = parsedDir.name
|
||||||
|
if (namespace.startsWith("__")) {
|
||||||
|
const parsedCoreDir = path.parse(parsedDir.dir)
|
||||||
|
namespace = parsedCoreDir.name
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (namespace) {
|
||||||
|
// We strip the last character when adding the type of registration
|
||||||
|
// this is a trick for plural "ies"
|
||||||
|
case "repositories":
|
||||||
|
namespace = "repositorys"
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
export default formatRegistrationName
|
||||||
Reference in New Issue
Block a user