fix: common registration name formatter
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
38
packages/medusa/src/utils/format-registration-name.js
Normal file
38
packages/medusa/src/utils/format-registration-name.js
Normal file
@@ -0,0 +1,38 @@
|
||||
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
|
||||
|
||||
switch (namespace) {
|
||||
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