diff --git a/packages/medusa/src/loaders/models.js b/packages/medusa/src/loaders/models.js index 9f69983091..14113fec16 100644 --- a/packages/medusa/src/loaders/models.js +++ b/packages/medusa/src/loaders/models.js @@ -3,6 +3,8 @@ import path from "path" import { EntitySchema } from "typeorm" import { asClass, asValue } from "awilix" +import formatRegistrationName from "../utils/format-registration-name" + /** * Registers all models in the model directory */ @@ -34,23 +36,3 @@ export default ({ container }, config = { register: true }) => { 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 -} diff --git a/packages/medusa/src/loaders/plugins.js b/packages/medusa/src/loaders/plugins.js index f2d907135e..fb9f1aa7e0 100644 --- a/packages/medusa/src/loaders/plugins.js +++ b/packages/medusa/src/loaders/plugins.js @@ -16,6 +16,8 @@ import fs from "fs" import { asValue, asClass, asFunction, aliasTo } from "awilix" import { sync as existsSync } from "fs-exists-cached" +import formatRegistrationName from "../utils/format-registration-name" + /** * 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 function createPluginId(name) { return name diff --git a/packages/medusa/src/loaders/repositories.js b/packages/medusa/src/loaders/repositories.js index c66507c144..72c7fadd52 100644 --- a/packages/medusa/src/loaders/repositories.js +++ b/packages/medusa/src/loaders/repositories.js @@ -2,6 +2,8 @@ import glob from "glob" import path from "path" import { Lifetime, asClass, asValue } from "awilix" +import formatRegistrationName from "../utils/format-registration-name" + /** * 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 -} diff --git a/packages/medusa/src/loaders/services.js b/packages/medusa/src/loaders/services.js index b654d7254b..8401371b32 100644 --- a/packages/medusa/src/loaders/services.js +++ b/packages/medusa/src/loaders/services.js @@ -3,6 +3,8 @@ import glob from "glob" import path from "path" import { Lifetime, asFunction } from "awilix" +import formatRegistrationName from "../utils/format-registration-name" + /** * 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 -} diff --git a/packages/medusa/src/utils/__tests__/format-registration-name.js b/packages/medusa/src/utils/__tests__/format-registration-name.js new file mode 100644 index 0000000000..0403c36fb0 --- /dev/null +++ b/packages/medusa/src/utils/__tests__/format-registration-name.js @@ -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) + }) +}) diff --git a/packages/medusa/src/utils/format-registration-name.js b/packages/medusa/src/utils/format-registration-name.js new file mode 100644 index 0000000000..1830c0c3f3 --- /dev/null +++ b/packages/medusa/src/utils/format-registration-name.js @@ -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