chore: added missing withTransacton, create-variant using TO (#3047)
Create variant integrated with Inventory modules
This commit is contained in:
committed by
GitHub
parent
150696de99
commit
aa54d902e5
@@ -1,53 +1,68 @@
|
||||
if (process.env.NODE_ENV !== "development") {
|
||||
return
|
||||
}
|
||||
|
||||
const path = require("path")
|
||||
|
||||
const Module = require("module")
|
||||
const originalRequire = Module.prototype.require
|
||||
const medusaCore = path.resolve(path.join(__dirname, "../../packages"))
|
||||
|
||||
function replacePath(requirePath, package, concatPackage = true) {
|
||||
const idx = requirePath.indexOf(package)
|
||||
const packPath = requirePath.substring(idx + package.length)
|
||||
function replacePath(requirePath, pack, concatPackage = true) {
|
||||
const idx = requirePath.indexOf(pack)
|
||||
const packPath = requirePath.substring(idx + pack.length).replace(/\\/g, "/")
|
||||
|
||||
let newPath =
|
||||
medusaCore +
|
||||
"/" +
|
||||
(concatPackage ? package + "/" : "") +
|
||||
(concatPackage ? pack + "/" : "") +
|
||||
packPath.replace("/dist", "/src").replace(".js", "")
|
||||
|
||||
if (!newPath.includes("/src")) {
|
||||
newPath += "/src"
|
||||
}
|
||||
|
||||
return path.resolve(newPath)
|
||||
}
|
||||
|
||||
Module.prototype.require = function (...args) {
|
||||
function checkAndReplacePaths(path) {
|
||||
const interfaces = "medusa-interfaces"
|
||||
const utils = "medusa-core-utils"
|
||||
const base = "@medusajs"
|
||||
|
||||
if (args[0].includes(base)) {
|
||||
args[0] = replacePath(args[0], base, false)
|
||||
} else if (args[0].includes(interfaces)) {
|
||||
args[0] = replacePath(args[0], interfaces)
|
||||
} else if (args[0].includes(utils)) {
|
||||
args[0] = replacePath(args[0], utils)
|
||||
if (path.includes(base)) {
|
||||
path = replacePath(path, base, false)
|
||||
} else if (path.includes(interfaces)) {
|
||||
path = replacePath(path, interfaces)
|
||||
} else if (path.includes(utils)) {
|
||||
path = replacePath(path, utils)
|
||||
}
|
||||
|
||||
return path
|
||||
}
|
||||
|
||||
Module.prototype.require = function (...args) {
|
||||
args[0] = checkAndReplacePaths(args[0])
|
||||
|
||||
if (args[0] === "glob") {
|
||||
const glob = originalRequire.apply(this, args)
|
||||
const originalGlobSync = glob.sync
|
||||
glob.GlobSync = glob.sync = (pattern, options) => {
|
||||
if (pattern.endsWith(".js") || pattern.endsWith(".ts")) {
|
||||
pattern = checkAndReplacePaths(pattern)
|
||||
pattern = pattern.replace(".js", ".{j,t}s").replace("/dist/", "/src/")
|
||||
}
|
||||
|
||||
return originalGlobSync.apply(this, [pattern, options])
|
||||
}
|
||||
return glob
|
||||
} else if (args[0] === "resolve-cwd") {
|
||||
const resolveCwd = originalRequire.apply(this, args)
|
||||
const newResolveCwd = (pattern) => {
|
||||
pattern = checkAndReplacePaths(pattern)
|
||||
|
||||
return resolveCwd.apply(this, [pattern])
|
||||
}
|
||||
return newResolveCwd
|
||||
}
|
||||
|
||||
return originalRequire.apply(this, args)
|
||||
|
||||
@@ -14,6 +14,22 @@ const medusaCore = path
|
||||
.replace(/\\/g, "/")
|
||||
|
||||
let WATCHING = false
|
||||
let IS_RELOADING = false
|
||||
|
||||
function getParentModulesIds(element) {
|
||||
if (!element) {
|
||||
return []
|
||||
}
|
||||
|
||||
const ids = [element.id]
|
||||
let parent = element.parent
|
||||
while (parent && parent.id.replace(/\\/g, "/").includes(medusaCore)) {
|
||||
ids.push(parent.id)
|
||||
parent = parent.parent
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
const watchFiles = () => {
|
||||
if (WATCHING) {
|
||||
return
|
||||
@@ -42,7 +58,12 @@ const watchFiles = () => {
|
||||
})
|
||||
|
||||
watcher.on("change", async function (rawFile) {
|
||||
if (IS_RELOADING) {
|
||||
return
|
||||
}
|
||||
|
||||
console.log("Reloading server...")
|
||||
IS_RELOADING = true
|
||||
const start = Date.now()
|
||||
|
||||
const file = rawFile.replace(/\\/g, "/")
|
||||
@@ -75,12 +96,18 @@ const watchFiles = () => {
|
||||
next.endsWith(".ts") ||
|
||||
name.startsWith(next)
|
||||
) {
|
||||
delete module.constructor._cache[rawName]
|
||||
const cacheToClean = getParentModulesIds(
|
||||
module.constructor._cache[rawName]
|
||||
)
|
||||
for (const id of cacheToClean) {
|
||||
delete module.constructor._cache[id]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await bootstrapApp()
|
||||
IS_RELOADING = false
|
||||
|
||||
console.log("Server reloaded in", Date.now() - start, "ms")
|
||||
})
|
||||
@@ -119,8 +146,6 @@ const bootstrapApp = async () => {
|
||||
watchFiles()
|
||||
console.log(`Server Running at localhost:${port}`)
|
||||
})
|
||||
|
||||
database = dbConnection
|
||||
}
|
||||
|
||||
bootstrapApp()
|
||||
void bootstrapApp()
|
||||
|
||||
Reference in New Issue
Block a user