@amaster.ai/cli 1.1.30 → 1.1.31

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/README.md CHANGED
@@ -54,6 +54,26 @@ amaster addon upgrade crm-pages
54
54
  amaster addon list
55
55
  ```
56
56
 
57
+ Addon 可把市场展示主图和截图放在源码包内,例如:
58
+
59
+ ```text
60
+ marketplace/cover.png
61
+ marketplace/screenshots/preview.png
62
+ ```
63
+
64
+ 并在 `addon.json` 中声明:
65
+
66
+ ```json
67
+ {
68
+ "display": {
69
+ "image": "marketplace/cover.png",
70
+ "screenshots": ["marketplace/screenshots/preview.png"]
71
+ }
72
+ }
73
+ ```
74
+
75
+ 这些文件只用于发布到 Addon Marketplace,不会被 `addon install` 安装到应用工程。
76
+
57
77
  `addon search` 在未显式传 `--application-type` 时,会从当前目录向上查找最近的 `package.json`,读取同级 `app.settings.yml` 的 `type`,并自动按 `web` 或 `mini` 过滤。
58
78
 
59
79
  常用参数:
@@ -121,8 +141,8 @@ Addon 可在 `addon.json` 中声明安装后的运行配置要求:
121
141
 
122
142
  页面注册方式:
123
143
 
124
- - `web`:生成 `src/amaster/registry/addon-routes.generated.ts`,并 patch `src/routes.tsx`
125
- - `mini`:生成 `src/amaster/registry/addon-pages.generated.ts`,并 patch `src/app.config.ts`
144
+ - `web`:生成 `src/routes-addon.ts`,并 patch `src/routes.tsx`
145
+ - `mini`:生成 `src/app-addon.config.ts`,并 patch `src/app.config.ts`
126
146
 
127
147
  升级策略:
128
148
 
package/dist/cli.cjs CHANGED
@@ -564,6 +564,7 @@ async function loadAddonManifestFromDirectory(directory) {
564
564
  const manifestText = await promises$1.readFile(manifestPath, "utf8");
565
565
  const manifest = JSON.parse(manifestText);
566
566
  validateAddonManifest(manifest);
567
+ await assertDisplayAssetPathsExist(directory, manifest);
567
568
  return manifest;
568
569
  }
569
570
  function validateAddonManifest(manifest) {
@@ -582,9 +583,40 @@ function validateAddonManifest(manifest) {
582
583
  if (!Array.isArray(manifest.members)) {
583
584
  throw new Error("addon.json missing members");
584
585
  }
586
+ validateDisplayAssets(manifest);
585
587
  validateInstallRequirements(manifest);
586
588
  validateInstallNpm(manifest);
587
589
  }
590
+ function validateDisplayAssets(manifest) {
591
+ const image = manifest.display.image;
592
+ if (image !== void 0) {
593
+ if (typeof image !== "string") {
594
+ throw new Error("addon.json display.image must be a string");
595
+ }
596
+ normalizeRelativePath(image, "display.image");
597
+ }
598
+ const screenshots = manifest.display.screenshots;
599
+ if (screenshots !== void 0) {
600
+ if (!Array.isArray(screenshots)) {
601
+ throw new Error("addon.json display.screenshots must be an array");
602
+ }
603
+ screenshots.forEach((item, index) => {
604
+ if (typeof item !== "string") {
605
+ throw new Error(`addon.json display.screenshots[${index}] must be a string`);
606
+ }
607
+ normalizeRelativePath(item, `display.screenshots[${index}]`);
608
+ });
609
+ }
610
+ }
611
+ async function assertDisplayAssetPathsExist(directory, manifest) {
612
+ const paths = [
613
+ ...manifest.display.image ? [manifest.display.image] : [],
614
+ ...manifest.display.screenshots ?? []
615
+ ];
616
+ for (const assetPath of paths) {
617
+ await assertPathExists(directory, normalizeRelativePath(assetPath, "display asset path"));
618
+ }
619
+ }
588
620
  function validateInstallRequirements(manifest) {
589
621
  const requirements = manifest.install?.requirements;
590
622
  if (!requirements) {
@@ -651,21 +683,21 @@ function validateInstallNpm(manifest) {
651
683
  function resolveMemberSourcePath(member) {
652
684
  const sourcePath = member.source?.path;
653
685
  if (typeof sourcePath === "string" && sourcePath.trim()) {
654
- return normalizeRelativePath(sourcePath);
686
+ return normalizeRelativePath(sourcePath, "member source path");
655
687
  }
656
688
  const fallbackKeys = ["entry", "path", "contentPath"];
657
689
  for (const key of fallbackKeys) {
658
690
  const value = member[key];
659
691
  if (typeof value === "string" && value.trim()) {
660
- return normalizeRelativePath(value);
692
+ return normalizeRelativePath(value, "member source path");
661
693
  }
662
694
  }
663
695
  return null;
664
696
  }
665
- function normalizeRelativePath(value) {
697
+ function normalizeRelativePath(value, fieldName = "path") {
666
698
  const normalized = value.replace(/^\.?\//, "").replace(/\\/g, "/");
667
699
  if (!normalized || normalized.startsWith("../") || normalized.includes("/../")) {
668
- throw new Error(`Invalid member source path: ${value}`);
700
+ throw new Error(`Invalid addon.json ${fieldName}: ${value}`);
669
701
  }
670
702
  return normalized;
671
703
  }
@@ -733,6 +765,14 @@ async function tryDetectNearestApplicationType(startDir = process.cwd()) {
733
765
 
734
766
  // src/marketplace/install.ts
735
767
  var execFileAsync = util.promisify(child_process.execFile);
768
+ var ADDON_ROUTES_FILE = "src/routes-addon.ts";
769
+ var ADDON_ROUTES_IMPORT = 'import { addonRoutes } from "./routes-addon";';
770
+ var LEGACY_ADDON_ROUTES_IMPORT = 'import { addonRoutes } from "./amaster/registry/addon-routes.generated";';
771
+ var LEGACY_ADDON_ROUTES_FILE = "src/amaster/registry/addon-routes.generated.ts";
772
+ var ADDON_PAGES_FILE = "src/app-addon.config.ts";
773
+ var ADDON_PAGES_IMPORT = 'import { addonPages, addonTabBarItems } from "./app-addon.config";';
774
+ var LEGACY_ADDON_PAGES_IMPORT = 'import { addonPages, addonTabBarItems } from "./amaster/registry/addon-pages.generated";';
775
+ var LEGACY_ADDON_PAGES_FILE = "src/amaster/registry/addon-pages.generated.ts";
736
776
  var AddonInstallConflictError = class extends Error {
737
777
  constructor(input) {
738
778
  super(`Addon ${input.operation} has ${input.conflicts.length} conflict(s)`);
@@ -1641,10 +1681,14 @@ async function ensureWebRoutesPatched(targetDir) {
1641
1681
  const routesFile = path.join(targetDir, "src", "routes.tsx");
1642
1682
  const original = await promises$1.readFile(routesFile, "utf8");
1643
1683
  let next = original;
1644
- if (!next.includes("./amaster/registry/addon-routes.generated")) {
1684
+ if (next.includes(LEGACY_ADDON_ROUTES_IMPORT)) {
1685
+ next = next.replace(LEGACY_ADDON_ROUTES_IMPORT, ADDON_ROUTES_IMPORT);
1686
+ }
1687
+ if (!next.includes(ADDON_ROUTES_IMPORT)) {
1645
1688
  next = next.replace(
1646
1689
  'import type { ComponentType } from "react";',
1647
- 'import type { ComponentType } from "react";\nimport { addonRoutes } from "./amaster/registry/addon-routes.generated";'
1690
+ `import type { ComponentType } from "react";
1691
+ ${ADDON_ROUTES_IMPORT}`
1648
1692
  );
1649
1693
  }
1650
1694
  if (!next.includes("...addonRoutes")) {
@@ -1668,8 +1712,11 @@ async function ensureMiniAppConfigPatched(targetDir, needsTabBarItems) {
1668
1712
  const configFile = path.join(targetDir, "src", "app.config.ts");
1669
1713
  const original = await promises$1.readFile(configFile, "utf8");
1670
1714
  let next = original;
1671
- if (!next.includes("./amaster/registry/addon-pages.generated")) {
1672
- next = `import { addonPages, addonTabBarItems } from "./amaster/registry/addon-pages.generated";
1715
+ if (next.includes(LEGACY_ADDON_PAGES_IMPORT)) {
1716
+ next = next.replace(LEGACY_ADDON_PAGES_IMPORT, ADDON_PAGES_IMPORT);
1717
+ }
1718
+ if (!next.includes(ADDON_PAGES_IMPORT)) {
1719
+ next = `${ADDON_PAGES_IMPORT}
1673
1720
  ${next}`;
1674
1721
  }
1675
1722
  if (!next.includes("...addonPages")) {
@@ -1694,8 +1741,6 @@ ${next}`;
1694
1741
  }
1695
1742
  }
1696
1743
  async function rebuildRegistryFiles(targetDir, applicationType, metas) {
1697
- const registryDir = path.join(targetDir, "src", "amaster", "registry");
1698
- await promises$1.mkdir(registryDir, { recursive: true });
1699
1744
  if (applicationType === "web") {
1700
1745
  const pageMembers2 = metas.flatMap(
1701
1746
  (meta) => meta.members.filter((member) => member.type === "page" && member.page?.web).map((member) => ({ addonName: meta.name, member }))
@@ -1705,32 +1750,38 @@ async function rebuildRegistryFiles(targetDir, applicationType, metas) {
1705
1750
  const entry = member.page.web.entry;
1706
1751
  return {
1707
1752
  identifier,
1708
- importPath: `../../${removeFileExtension(entry).replace(/\\/g, "/")}`,
1753
+ importPath: `./${removeFileExtension(entry).replace(/\\/g, "/")}`,
1709
1754
  route: member.page.web.route,
1710
1755
  visible: member.page.web.visible,
1711
1756
  name: member.name
1712
1757
  };
1713
1758
  });
1714
- const fileContent2 = [
1715
- 'import type { ComponentType } from "react";',
1716
- ...imports.map((item) => `import ${item.identifier} from "${item.importPath}";`),
1717
- "",
1718
- "type AddonRouteConfig = {",
1719
- " name: string;",
1720
- " path: string;",
1721
- " Component: ComponentType<Record<string, unknown>>;",
1722
- " visible?: boolean;",
1723
- "};",
1724
- "",
1725
- "export const addonRoutes: AddonRouteConfig[] = [",
1726
- ...imports.map(
1727
- (item) => ` { name: ${JSON.stringify(item.name)}, path: ${JSON.stringify(item.route)}, Component: ${item.identifier},${item.visible === void 0 ? "" : ` visible: ${item.visible},`} },`
1728
- ),
1729
- "];",
1730
- ""
1731
- ].join("\n");
1732
- await promises$1.writeFile(path.join(registryDir, "addon-routes.generated.ts"), fileContent2);
1733
- await ensureWebRoutesPatched(targetDir);
1759
+ const routesFile = path.join(targetDir, "src", "routes.tsx");
1760
+ const routesContent = fs.existsSync(routesFile) ? await promises$1.readFile(routesFile, "utf8") : "";
1761
+ const alreadyPatched2 = routesContent.includes(ADDON_ROUTES_IMPORT) || routesContent.includes(LEGACY_ADDON_ROUTES_IMPORT) || routesContent.includes("...addonRoutes");
1762
+ await promises$1.unlink(path.join(targetDir, LEGACY_ADDON_ROUTES_FILE)).catch(() => void 0);
1763
+ if (imports.length > 0 || alreadyPatched2) {
1764
+ const fileContent = [
1765
+ 'import type { ComponentType } from "react";',
1766
+ ...imports.map((item) => `import ${item.identifier} from "${item.importPath}";`),
1767
+ "",
1768
+ "type AddonRouteConfig = {",
1769
+ " name: string;",
1770
+ " path: string;",
1771
+ " Component: ComponentType<Record<string, unknown>>;",
1772
+ " visible?: boolean;",
1773
+ "};",
1774
+ "",
1775
+ "export const addonRoutes: AddonRouteConfig[] = [",
1776
+ ...imports.map(
1777
+ (item) => ` { name: ${JSON.stringify(item.name)}, path: ${JSON.stringify(item.route)}, Component: ${item.identifier},${item.visible === void 0 ? "" : ` visible: ${item.visible},`} },`
1778
+ ),
1779
+ "];",
1780
+ ""
1781
+ ].join("\n");
1782
+ await promises$1.writeFile(path.join(targetDir, ADDON_ROUTES_FILE), fileContent);
1783
+ await ensureWebRoutesPatched(targetDir);
1784
+ }
1734
1785
  return;
1735
1786
  }
1736
1787
  const pageMembers = metas.flatMap(
@@ -1741,14 +1792,20 @@ async function rebuildRegistryFiles(targetDir, applicationType, metas) {
1741
1792
  pagePath: page.pagePath,
1742
1793
  ...page.tabBar
1743
1794
  }));
1744
- const fileContent = [
1745
- `export const addonPages = ${JSON.stringify(addonPages, null, 2)};`,
1746
- "",
1747
- `export const addonTabBarItems = ${JSON.stringify(addonTabBarItems, null, 2)};`,
1748
- ""
1749
- ].join("\n");
1750
- await promises$1.writeFile(path.join(registryDir, "addon-pages.generated.ts"), fileContent);
1751
- await ensureMiniAppConfigPatched(targetDir, addonTabBarItems.length > 0);
1795
+ const appConfigFile = path.join(targetDir, "src", "app.config.ts");
1796
+ const appConfigContent = fs.existsSync(appConfigFile) ? await promises$1.readFile(appConfigFile, "utf8") : "";
1797
+ const alreadyPatched = appConfigContent.includes(ADDON_PAGES_IMPORT) || appConfigContent.includes(LEGACY_ADDON_PAGES_IMPORT) || appConfigContent.includes("...addonPages") || appConfigContent.includes("...addonTabBarItems");
1798
+ await promises$1.unlink(path.join(targetDir, LEGACY_ADDON_PAGES_FILE)).catch(() => void 0);
1799
+ if (addonPages.length > 0 || alreadyPatched) {
1800
+ const fileContent = [
1801
+ `export const addonPages = ${JSON.stringify(addonPages, null, 2)};`,
1802
+ "",
1803
+ `export const addonTabBarItems = ${JSON.stringify(addonTabBarItems, null, 2)};`,
1804
+ ""
1805
+ ].join("\n");
1806
+ await promises$1.writeFile(path.join(targetDir, ADDON_PAGES_FILE), fileContent);
1807
+ await ensureMiniAppConfigPatched(targetDir, addonTabBarItems.length > 0);
1808
+ }
1752
1809
  }
1753
1810
  async function materializeAddon(options) {
1754
1811
  const targetDir = path.resolve(options.targetDir);