@mercurjs/dashboard-sdk 2.0.0-canary.66 → 2.0.0-canary.68

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -36,6 +36,7 @@ module.exports = __toCommonJS(index_exports);
36
36
 
37
37
  // src/plugin.ts
38
38
  var import_path5 = __toESM(require("path"), 1);
39
+ var import_fs4 = __toESM(require("fs"), 1);
39
40
 
40
41
  // src/utils.ts
41
42
  function resolveExports(moduleExports) {
@@ -256,21 +257,26 @@ function buildRouteTree(results) {
256
257
  }
257
258
  return Array.from(routeMap.values());
258
259
  }
259
- function generateRoutes({ srcDir }) {
260
+ function generateRoutes({ srcDir, pluginDirs }) {
260
261
  const pagesDir = import_path.default.join(srcDir, "pages");
261
- const files = crawlPages(pagesDir);
262
- if (files.length === 0) {
263
- return `export const customRoutes = []`;
264
- }
265
262
  let index = 0;
266
263
  const results = [];
267
- for (const file of files) {
264
+ for (const file of crawlPages(pagesDir)) {
268
265
  const result = parseFile(file, pagesDir, index);
269
266
  if (result) {
270
267
  results.push(result);
271
268
  index++;
272
269
  }
273
270
  }
271
+ for (const pluginDir of pluginDirs) {
272
+ for (const file of crawlPages(pluginDir)) {
273
+ const result = parseFile(file, pluginDir, index);
274
+ if (result) {
275
+ results.push(result);
276
+ index++;
277
+ }
278
+ }
279
+ }
274
280
  if (results.length === 0) {
275
281
  return `export const customRoutes = []`;
276
282
  }
@@ -388,21 +394,26 @@ function parseFile2(file, pagesDir, index) {
388
394
  menuItem: generateMenuItem(config, file, pagesDir, index)
389
395
  };
390
396
  }
391
- function generateMenuItems({ srcDir }) {
397
+ function generateMenuItems({ srcDir, pluginDirs }) {
392
398
  const pagesDir = import_path2.default.join(srcDir, "pages");
393
- const files = crawlPages2(pagesDir);
394
- if (files.length === 0) {
395
- return `export default { menuItems: [] }`;
396
- }
397
399
  let index = 0;
398
400
  const results = [];
399
- for (const file of files) {
401
+ for (const file of crawlPages2(pagesDir)) {
400
402
  const result = parseFile2(file, pagesDir, index);
401
403
  if (result) {
402
404
  results.push(result);
403
405
  index++;
404
406
  }
405
407
  }
408
+ for (const pluginDir of pluginDirs) {
409
+ for (const file of crawlPages2(pluginDir)) {
410
+ const result = parseFile2(file, pluginDir, index);
411
+ if (result) {
412
+ results.push(result);
413
+ index++;
414
+ }
415
+ }
416
+ }
406
417
  if (results.length === 0) {
407
418
  return `export default { menuItems: [] }`;
408
419
  }
@@ -512,24 +523,56 @@ var UI_MODULE_KEYS = [
512
523
  "admin_ui",
513
524
  "vendor_ui"
514
525
  ];
526
+ function resolvePluginRoot(resolve, configDir) {
527
+ try {
528
+ if (resolve.startsWith(".")) {
529
+ return import_path5.default.resolve(configDir, resolve);
530
+ }
531
+ const pkgJsonPath = require.resolve(import_path5.default.join(resolve, "package.json"), {
532
+ paths: [configDir]
533
+ });
534
+ return import_path5.default.dirname(pkgJsonPath);
535
+ } catch {
536
+ return null;
537
+ }
538
+ }
539
+ function resolvePluginDirs(plugins, configDir, appType) {
540
+ const dirs = [];
541
+ for (const plugin of plugins) {
542
+ const resolve = typeof plugin === "string" ? plugin : plugin?.resolve;
543
+ if (!resolve || typeof resolve !== "string") continue;
544
+ const pluginRoot = resolvePluginRoot(resolve, configDir);
545
+ if (!pluginRoot) continue;
546
+ const extDir = import_path5.default.join(pluginRoot, appType);
547
+ if (import_fs4.default.existsSync(extDir) && import_fs4.default.statSync(extDir).isDirectory()) {
548
+ dirs.push(extDir);
549
+ }
550
+ }
551
+ return dirs;
552
+ }
515
553
  async function loadMedusaConfig(medusaConfigPath, root) {
516
554
  try {
517
555
  const mod = await getFileExports(medusaConfigPath);
518
556
  const medusaConfig = mod.default ?? mod;
519
557
  const modules = medusaConfig?.modules ?? {};
558
+ const configDir = import_path5.default.dirname(medusaConfigPath);
559
+ let base;
560
+ let appType;
520
561
  for (const key of UI_MODULE_KEYS) {
521
562
  const value = modules[key];
522
563
  if (!value || typeof value !== "object" || !value.options?.appDir) continue;
523
- const appDir = import_path5.default.resolve(import_path5.default.dirname(medusaConfigPath), value.options.appDir);
564
+ const appDir = import_path5.default.resolve(configDir, value.options.appDir);
524
565
  if (appDir === root) {
525
- return {
526
- base: value.options.path
527
- };
566
+ base = value.options.path;
567
+ appType = key === "admin_ui" ? "admin" : "vendor";
568
+ break;
528
569
  }
529
570
  }
530
- return {};
571
+ const plugins = medusaConfig?.plugins ?? [];
572
+ const pluginDirs = appType ? resolvePluginDirs(plugins, configDir, appType) : [];
573
+ return { base, pluginDirs };
531
574
  } catch {
532
- return {};
575
+ return { pluginDirs: [] };
533
576
  }
534
577
  }
535
578
  function mercurDashboardPlugin(pluginConfig) {
@@ -540,7 +583,7 @@ function mercurDashboardPlugin(pluginConfig) {
540
583
  async config(viteConfig) {
541
584
  root = viteConfig.root || process.cwd();
542
585
  const medusaConfigPath = import_path5.default.resolve(root, pluginConfig.medusaConfigPath);
543
- const { base } = await loadMedusaConfig(medusaConfigPath, root);
586
+ const { base, pluginDirs } = await loadMedusaConfig(medusaConfigPath, root);
544
587
  const srcDir = import_path5.default.join(root, "src");
545
588
  const backendUrl = pluginConfig.backendUrl ?? "http://localhost:9000";
546
589
  config = {
@@ -548,7 +591,8 @@ function mercurDashboardPlugin(pluginConfig) {
548
591
  backendUrl,
549
592
  base,
550
593
  root,
551
- srcDir
594
+ srcDir,
595
+ pluginDirs
552
596
  };
553
597
  return {
554
598
  base: config.base,
package/dist/index.d.cts CHANGED
@@ -21,6 +21,7 @@ interface BuiltMercurConfig extends MercurConfig {
21
21
  base?: string;
22
22
  root: string;
23
23
  srcDir: string;
24
+ pluginDirs: string[];
24
25
  }
25
26
  type RouteConfig = {
26
27
  label: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mercurjs/dashboard-sdk",
3
- "version": "2.0.0-canary.66",
3
+ "version": "2.0.0-canary.68",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/mercurjs/mercur",