feat(admin, admin-ui, medusa-js, medusa-react, medusa): Support Admin Extensions (#4761)

Co-authored-by: Rares Stefan <948623+StephixOne@users.noreply.github.com>
Co-authored-by: Oli Juhl <59018053+olivermrbl@users.noreply.github.com>
This commit is contained in:
Kasper Fabricius Kristensen
2023-08-17 14:14:45 +02:00
committed by GitHub
parent 26c78bbc03
commit f1a05f4725
189 changed files with 14570 additions and 12773 deletions

View File

@@ -1,92 +1,57 @@
import { build } from "@medusajs/admin-ui"
import fse from "fs-extra"
import ora from "ora"
import { EOL } from "os"
import { resolve } from "path"
import { loadConfig, reporter, validatePath } from "../utils"
import { build, clean, logger } from "@medusajs/admin-ui"
import { getPluginPaths, loadConfig } from "../utils"
import { createBuildManifest, shouldBuild } from "../utils/build-manifest"
export default async function setupAdmin() {
const { path, outDir, serve, autoRebuild } = loadConfig()
const { autoRebuild, serve, backend, outDir, path } = loadConfig()
// If the user has not specified that the admin UI should be served,
// we should not build it. Furthermore, if the user has not specified that they want
// the admin UI to be rebuilt on changes, we should not build it here.
if (!serve || !autoRebuild) {
if (process.env.COMMAND_INITIATED_BY === "develop") {
logger.info("Running in development mode. Skipping setup.")
return
}
try {
validatePath(path)
} catch (err) {
reporter.panic(err)
if (!serve) {
return
}
let dir: string
let shouldBuild = false
/**
* If no outDir is provided we default to "build".
*/
if (outDir) {
dir = resolve(process.cwd(), outDir)
} else {
dir = resolve(process.cwd(), "build")
if (!autoRebuild) {
return
}
try {
await fse.ensureDir(dir)
} catch (_e) {
shouldBuild = true
const appDir = process.cwd()
const shouldContinue = await shouldBuild(appDir, {
backend,
path,
outDir,
})
if (!shouldContinue) {
return
}
const manifestPath = resolve(dir, "build-manifest.json")
const plugins = await getPluginPaths()
const buildOptions = {
build: {
await clean({
appDir,
outDir: outDir,
})
await build({
appDir,
buildDir: outDir,
options: {
backend,
path,
outDir,
},
globals: {
base: path,
/**
* We only build the admin UI as part of the Medusa startup process if
* the user has specified that they want to serve the admin UI. When this
* is the case, we should always set the backend to `undefined`.
*/
backend: undefined,
},
}
plugins,
reporting: "minimal", // The fancy reporting does not work well when run as part of the setup script
})
try {
const manifest = await fse.readJSON(manifestPath)
/**
* If the manifest is not the same as the current build options,
* we should rebuild the admin UI.
*/
if (JSON.stringify(manifest) !== JSON.stringify(buildOptions)) {
shouldBuild = true
}
} catch (_e) {
/**
* If the manifest file does not exist, we should rebuild the admin UI.
* This is the case when the admin UI is built for the first time.
*/
shouldBuild = true
}
if (shouldBuild) {
const time = Date.now()
const spinner = ora().start(
`Admin build is out of sync with the current configuration. Rebuild initialized${EOL}`
)
await build({
...buildOptions,
}).catch((err) => {
spinner.fail(`Failed to build Admin UI${EOL}`)
reporter.panic(err)
})
spinner.succeed(`Admin UI build - ${Date.now() - time}ms`)
}
await createBuildManifest(appDir, {
backend,
path,
outDir,
})
}