@astryxdesign/cli 0.7.2-canary.86e8392 → 0.7.2-canary.917864d

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.
Files changed (283) hide show
  1. package/README.md +74 -73
  2. package/api/build/help/help.mjs +1 -1
  3. package/api/component/_adapter.d.mts +41 -1
  4. package/api/component/_adapter.mjs +93 -7
  5. package/api/component/component.mjs +22 -5
  6. package/api/component/component.test.mjs +80 -0
  7. package/api/component/integration-export-name.test.mjs +644 -0
  8. package/api/component/list/list.mjs +42 -4
  9. package/api/discover/_adapter.d.mts +119 -12
  10. package/api/discover/_adapter.mjs +449 -10
  11. package/api/discover/_adapter.test.mjs +599 -17
  12. package/api/discover/_catalog-view.d.mts +16 -5
  13. package/api/discover/_catalog-view.mjs +32 -8
  14. package/api/discover/discover.d.mts +11 -0
  15. package/api/discover/discover.doc.mjs +7 -6
  16. package/api/discover/discover.mjs +107 -19
  17. package/api/discover/discover.type.d.mts +18 -7
  18. package/api/discover/discover.type.mjs +15 -7
  19. package/api/discover/list/list.d.mts +4 -3
  20. package/api/discover/list/list.mjs +17 -10
  21. package/api/docs/_adapter.d.mts +11 -0
  22. package/api/docs/_adapter.mjs +244 -1
  23. package/api/docs/integrationDocs.test.mjs +124 -0
  24. package/api/doctor/doctor.mjs +27 -3
  25. package/api/doctor/doctor.test.mjs +43 -0
  26. package/api/doctor/theme-checks.mjs +1 -1
  27. package/api/doctor/theme-checks.test.mjs +136 -0
  28. package/api/gap-report/gap-report.d.mts +3 -0
  29. package/api/gap-report/gap-report.doc.mjs +11 -3
  30. package/api/gap-report/gap-report.mjs +219 -52
  31. package/api/gap-report/gap-report.test.mjs +233 -0
  32. package/api/gap-report/gap-report.type.d.mts +41 -0
  33. package/api/gap-report/gap-report.type.mjs +18 -0
  34. package/api/index.d.mts +2 -0
  35. package/api/index.mjs +2 -0
  36. package/api/init/run/run.mjs +2 -2
  37. package/api/integration/_adapter.d.mts +32 -0
  38. package/api/integration/_adapter.mjs +80 -0
  39. package/api/integration/add-contribution.component-names.test.mjs +66 -0
  40. package/api/integration/add-contribution.mjs +8 -4
  41. package/api/integration/add-contribution.test.mjs +4 -2
  42. package/api/integration/authoring-checks.mjs +44 -4
  43. package/api/integration/authoring-checks.test.mjs +515 -0
  44. package/api/integration/authoring-checks.type.d.mts +95 -0
  45. package/api/integration/authoring-checks.type.mjs +53 -2
  46. package/api/integration/check/check.d.mts +96 -0
  47. package/api/integration/check/check.mjs +357 -0
  48. package/api/integration/check/check.test.mjs +489 -0
  49. package/api/integration/integration-check.type.d.mts +118 -0
  50. package/api/integration/integration-check.type.mjs +84 -0
  51. package/api/integration/integrationCheck.doc.d.mts +10 -0
  52. package/api/integration/integrationCheck.doc.mjs +73 -0
  53. package/api/integration/integrationComponentConflicts.doc.mjs +43 -6
  54. package/api/integration/integrationDocConflicts.doc.mjs +19 -5
  55. package/api/integration/pack-check.mjs +353 -114
  56. package/api/integration/pack-check.test.mjs +726 -0
  57. package/api/integration/validate-integration-fixes.test.mjs +67 -0
  58. package/api/integration/validate-integration.mjs +8 -0
  59. package/api/integration/validate-integration.test.mjs +41 -0
  60. package/api/integration/validate-integration.type.d.mts +8 -0
  61. package/api/integration/validate-integration.type.mjs +12 -1
  62. package/api/integration/validateIntegration.doc.mjs +19 -3
  63. package/api/search/search.d.mts +81 -12
  64. package/api/search/search.domain-priority.test.mjs +108 -18
  65. package/api/search/search.integration-check.test.mjs +24 -0
  66. package/api/search/search.mjs +609 -53
  67. package/api/search/search.test.mjs +175 -0
  68. package/api/swizzle/copy/copy.mjs +28 -14
  69. package/api/template/template.test.mjs +120 -3
  70. package/api/theme/build/build.family.test.mjs +64 -0
  71. package/api/theme/build/build.mjs +180 -24
  72. package/api/theme/themeBuild.doc.mjs +4 -4
  73. package/api/upgrade/_adapter.d.mts +40 -2
  74. package/api/upgrade/_adapter.mjs +117 -4
  75. package/api/upgrade/run/cli-behind-core.test.mjs +438 -0
  76. package/api/upgrade/run/run.mjs +30 -1
  77. package/api/upgrade/status/status.d.mts +21 -5
  78. package/api/upgrade/status/status.mjs +45 -8
  79. package/api/upgrade/upgrade.doc.mjs +5 -3
  80. package/api/upgrade/upgrade.type.d.mts +23 -0
  81. package/api/upgrade/upgrade.type.mjs +16 -1
  82. package/assets/codemods/runner.mjs +7 -1
  83. package/assets/codemods/transforms/next/__tests__/migrate-list-edge-compensation-to-edge.test.mjs +120 -0
  84. package/assets/codemods/transforms/next/index.mjs +11 -1
  85. package/assets/codemods/transforms/next/migrate-list-edge-compensation-to-edge.mjs +198 -0
  86. package/assets/discover/official-packages.json +367 -0
  87. package/assets/docs/README.md +10 -0
  88. package/assets/docs/author-a-theme.doc.mjs +3 -3
  89. package/assets/docs/browser-support.doc.mjs +1 -1
  90. package/assets/docs/getting-started.doc.mjs +3 -3
  91. package/assets/docs/styling-overview.doc.mjs +1 -1
  92. package/assets/docs/theme.doc.dense.mjs +1 -1
  93. package/assets/docs/theme.doc.mjs +2 -2
  94. package/assets/docs/theme.doc.zh.mjs +1 -1
  95. package/assets/docs/tree/add-a-component.doc.mjs +11 -1
  96. package/assets/docs/tree/add-a-theme.doc.mjs +1 -1
  97. package/assets/docs/tree/add-a-topic.doc.mjs +11 -8
  98. package/assets/docs/tree/agent-guidance.doc.mjs +2 -2
  99. package/assets/docs/tree/block-template.doc.mjs +8 -20
  100. package/assets/docs/tree/build-the-template.doc.mjs +1 -1
  101. package/assets/docs/tree/check-the-props.doc.mjs +196 -0
  102. package/assets/docs/tree/check-your-docs.doc.mjs +61 -7
  103. package/assets/docs/tree/checks.doc.mjs +26 -18
  104. package/assets/docs/tree/codemods.doc.mjs +4 -4
  105. package/assets/docs/tree/component-family.doc.mjs +8 -6
  106. package/assets/docs/tree/component-imports.doc.mjs +10 -2
  107. package/assets/docs/tree/components.doc.mjs +1 -1
  108. package/assets/docs/tree/debug-and-gap-reports.doc.mjs +30 -21
  109. package/assets/docs/tree/define-the-theme.doc.mjs +6 -5
  110. package/assets/docs/tree/describe-the-component.doc.mjs +2 -2
  111. package/assets/docs/tree/docs.doc.mjs +3 -3
  112. package/assets/docs/tree/document-the-template.doc.mjs +1 -1
  113. package/assets/docs/tree/document-the-theme.doc.mjs +1 -1
  114. package/assets/docs/tree/export-template-assets.doc.mjs +1 -1
  115. package/assets/docs/tree/extend-or-replace.doc.mjs +6 -6
  116. package/assets/docs/tree/fonts-and-assets.doc.mjs +2 -2
  117. package/assets/docs/tree/generate-a-palette.doc.mjs +5 -3
  118. package/assets/docs/tree/grade-template-with-agent.doc.mjs +1 -1
  119. package/assets/docs/tree/integrations.doc.mjs +8 -0
  120. package/assets/docs/tree/layout-spacing.doc.mjs +2 -3
  121. package/assets/docs/tree/layout.doc.mjs +6 -6
  122. package/assets/docs/tree/links.doc.mjs +6 -5
  123. package/assets/docs/tree/migration-setup.doc.mjs +3 -2
  124. package/assets/docs/tree/migration-verification.doc.mjs +0 -1
  125. package/assets/docs/tree/migration.doc.mjs +0 -1
  126. package/assets/docs/tree/package-and-test.doc.mjs +1 -1
  127. package/assets/docs/tree/page-template.doc.mjs +4 -8
  128. package/assets/docs/tree/publishing.doc.mjs +4 -4
  129. package/assets/docs/tree/quick-start.doc.mjs +38 -22
  130. package/assets/docs/tree/replace-a-core-component.doc.mjs +4 -6
  131. package/assets/docs/tree/replace-a-core-template.doc.mjs +6 -8
  132. package/assets/docs/tree/sections-and-placement.doc.mjs +6 -8
  133. package/assets/docs/tree/see-it-in-an-app.doc.mjs +4 -2
  134. package/assets/docs/tree/short-and-findable.doc.mjs +6 -5
  135. package/assets/docs/tree/single-component.doc.mjs +15 -6
  136. package/assets/docs/tree/start-a-template.doc.mjs +3 -3
  137. package/assets/docs/tree/subcomponent.doc.mjs +6 -6
  138. package/assets/docs/tree/template-assets.doc.mjs +2 -2
  139. package/assets/docs/tree/template-doc-overview.doc.mjs +5 -16
  140. package/assets/docs/tree/template-fonts.doc.mjs +1 -1
  141. package/assets/docs/tree/template-icons.doc.mjs +1 -1
  142. package/assets/docs/tree/template-images-media.doc.mjs +1 -1
  143. package/assets/docs/tree/template-styles.doc.mjs +2 -2
  144. package/assets/docs/tree/templates.doc.mjs +1 -1
  145. package/assets/docs/tree/test-in-an-app.doc.mjs +3 -3
  146. package/assets/docs/tree/test-template-in-app.doc.mjs +1 -1
  147. package/assets/docs/tree/tokens-and-setup.doc.mjs +19 -15
  148. package/assets/docs/tree/troubleshooting.doc.mjs +16 -8
  149. package/assets/docs/tree/type-scale.doc.mjs +3 -3
  150. package/assets/docs/tree/upgrading.doc.mjs +4 -4
  151. package/assets/docs/tree/use-a-theme-in-an-app.doc.mjs +3 -1
  152. package/assets/docs/tree/verify-packed-template.doc.mjs +1 -1
  153. package/assets/docs/tree/versioning.doc.mjs +7 -6
  154. package/assets/docs/tree/write-the-template-file.doc.mjs +4 -3
  155. package/assets/docs/use-a-theme.doc.mjs +2 -2
  156. package/assets/docs/working-with-ai.doc.mjs +43 -4
  157. package/assets/theme.template.ts +1 -1
  158. package/authoring/codemod/codemod.doc.mjs +7 -3
  159. package/authoring/config/config.doc.mjs +9 -6
  160. package/authoring/config/type.ts +4 -2
  161. package/authoring/discover/discover.doc.mjs +1 -1
  162. package/authoring/doctypes/base/graph-fields.doc.mjs +6 -4
  163. package/authoring/doctypes/command/command.doc.mjs +14 -12
  164. package/authoring/doctypes/component/component.doc.mjs +30 -27
  165. package/authoring/doctypes/component/type.ts +13 -8
  166. package/authoring/doctypes/enum/enum.doc.mjs +7 -4
  167. package/authoring/doctypes/function/function.doc.mjs +9 -8
  168. package/authoring/doctypes/hook/hook.doc.mjs +5 -4
  169. package/authoring/doctypes/namespace/namespace.doc.mjs +7 -6
  170. package/authoring/doctypes/reference/reference.doc.mjs +11 -10
  171. package/authoring/doctypes/schema/schema.doc.mjs +9 -9
  172. package/authoring/doctypes/template/integration-template.doc.d.mts +11 -0
  173. package/authoring/doctypes/template/integration-template.doc.mjs +49 -0
  174. package/authoring/doctypes/template/parse.d.mts +6 -2
  175. package/authoring/doctypes/template/parse.mjs +3 -1
  176. package/authoring/doctypes/template/template.doc.mjs +3 -3
  177. package/authoring/doctypes/template/type.ts +22 -0
  178. package/authoring/doctypes/theme/theme.doc.mjs +2 -2
  179. package/authoring/gap-report/gap-report.doc.mjs +2 -2
  180. package/authoring/identity/identity.doc.mjs +3 -3
  181. package/authoring/index.d.ts +1 -0
  182. package/authoring/integration/integration.doc.mjs +12 -4
  183. package/authoring/integration/parse.test.mjs +10 -0
  184. package/authoring/integration/schema.d.mts +2 -0
  185. package/authoring/integration/schema.mjs +1 -0
  186. package/authoring/integration/type.ts +4 -0
  187. package/clients/cli/commands/build-theme.batch-report.test.mjs +3 -1
  188. package/clients/cli/commands/build-theme.mjs +1 -1
  189. package/clients/cli/commands/build-theme.path-safety.test.mjs +224 -6
  190. package/clients/cli/commands/build-theme.registry.test.mjs +5 -2
  191. package/clients/cli/commands/cli-postinstall.test.mjs +49 -1
  192. package/clients/cli/commands/discover.broken-integration.test.mjs +4 -1
  193. package/clients/cli/commands/discover.doc.mjs +1 -1
  194. package/clients/cli/commands/discover.mjs +144 -93
  195. package/clients/cli/commands/discover.no-source.test.mjs +447 -33
  196. package/clients/cli/commands/discover.sources.test.mjs +20 -1
  197. package/clients/cli/commands/doctor-integration-components.doc.mjs +17 -4
  198. package/clients/cli/commands/doctor-integration-docs.doc.mjs +18 -5
  199. package/clients/cli/commands/doctor-integration-validate.doc.mjs +13 -2
  200. package/clients/cli/commands/doctor-integration.test.mjs +837 -0
  201. package/clients/cli/commands/doctor.mjs +250 -3
  202. package/clients/cli/commands/gap-report.doc.mjs +14 -2
  203. package/clients/cli/commands/gap-report.mjs +24 -2
  204. package/clients/cli/commands/gap-report.test.mjs +79 -0
  205. package/clients/cli/commands/integration-check.doc.mjs +85 -0
  206. package/clients/cli/commands/integration-check.test.mjs +370 -0
  207. package/clients/cli/commands/integration.doc.mjs +6 -2
  208. package/clients/cli/commands/integration.mjs +106 -1
  209. package/clients/cli/commands/json-contract.test.mjs +5 -1
  210. package/clients/cli/commands/setup-nudge.test.mjs +79 -1
  211. package/clients/cli/commands/swizzle.mjs +5 -3
  212. package/clients/cli/commands/swizzle.routing.test.mjs +18 -1
  213. package/clients/cli/commands/text-json-parity.test.mjs +5 -0
  214. package/clients/cli/commands/theme-build.doc.mjs +2 -2
  215. package/clients/cli/commands/upgrade.doc.mjs +14 -0
  216. package/clients/cli/commands/upgrade.older-cli.test.mjs +160 -0
  217. package/clients/cli/index.mjs +30 -5
  218. package/foundation/agent-docs/agent-doc-state.d.mts +14 -0
  219. package/foundation/agent-docs/agent-doc-state.mjs +38 -0
  220. package/foundation/agent-docs/agent-docs.d.mts +2 -1
  221. package/foundation/agent-docs/agent-docs.mjs +8 -6
  222. package/foundation/config/project.mjs +82 -0
  223. package/foundation/discovery/authoring-self-docs.mjs +2 -1
  224. package/foundation/discovery/component-discovery.d.mts +190 -10
  225. package/foundation/discovery/component-discovery.export-names.test.mjs +760 -0
  226. package/foundation/discovery/component-discovery.mjs +864 -56
  227. package/foundation/discovery/component-discovery.test.mjs +96 -0
  228. package/foundation/discovery/component-replacement.mjs +18 -0
  229. package/foundation/discovery/component-replacement.test.mjs +40 -0
  230. package/foundation/discovery/copied-passages.d.mts +216 -0
  231. package/foundation/discovery/copied-passages.mjs +720 -0
  232. package/foundation/discovery/copied-passages.test.mjs +124 -0
  233. package/foundation/discovery/doc-fluff.d.mts +112 -0
  234. package/foundation/discovery/doc-fluff.mjs +328 -0
  235. package/foundation/discovery/doc-fluff.test.mjs +142 -0
  236. package/foundation/discovery/doc-types.d.mts +157 -0
  237. package/foundation/discovery/doc-types.mjs +1224 -0
  238. package/foundation/discovery/doc-types.test.mjs +700 -0
  239. package/foundation/discovery/docs-discovery.d.mts +88 -0
  240. package/foundation/discovery/docs-discovery.mjs +83 -2
  241. package/foundation/discovery/integration-doc-files.d.mts +22 -0
  242. package/foundation/discovery/integration-doc-files.mjs +231 -0
  243. package/foundation/discovery/integration-doc-files.test.mjs +246 -0
  244. package/foundation/discovery/module-exports.d.mts +27 -0
  245. package/foundation/discovery/module-exports.mjs +356 -31
  246. package/foundation/discovery/module-exports.test.mjs +172 -0
  247. package/foundation/discovery/package-tsconfig.d.mts +14 -0
  248. package/foundation/discovery/package-tsconfig.mjs +228 -0
  249. package/foundation/discovery/package-tsconfig.test.mjs +210 -0
  250. package/foundation/discovery/project-typescript.d.mts +32 -0
  251. package/foundation/discovery/project-typescript.mjs +89 -0
  252. package/foundation/discovery/prop-drift.d.mts +278 -0
  253. package/foundation/discovery/prop-drift.mjs +2263 -0
  254. package/foundation/discovery/prop-drift.test.mjs +2272 -0
  255. package/foundation/discovery/template-adapter.mjs +74 -5
  256. package/foundation/discovery/type-match.d.mts +53 -0
  257. package/foundation/discovery/type-match.mjs +660 -0
  258. package/foundation/discovery/type-match.test.mjs +169 -0
  259. package/foundation/discovery/type-text.d.mts +68 -0
  260. package/foundation/discovery/type-text.mjs +658 -0
  261. package/foundation/discovery/type-text.test.mjs +196 -0
  262. package/foundation/discovery/unresolved-references.d.mts +62 -0
  263. package/foundation/discovery/unresolved-references.mjs +790 -0
  264. package/foundation/discovery/unresolved-references.test.mjs +567 -0
  265. package/foundation/doc-compiler/doc-loads.test.mjs +23 -1
  266. package/foundation/doc-compiler/inputs.mjs +23 -0
  267. package/foundation/doc-compiler/inputs.test.mjs +0 -1
  268. package/foundation/doc-compiler/links.d.mts +9 -5
  269. package/foundation/doc-compiler/links.mjs +4 -2
  270. package/foundation/env/package-manager.d.mts +32 -0
  271. package/foundation/env/package-manager.mjs +47 -0
  272. package/foundation/env/package-manager.test.mjs +194 -7
  273. package/foundation/fs/module-loader.d.mts +2 -1
  274. package/foundation/fs/module-loader.mjs +60 -4
  275. package/foundation/fs/module-loader.test.mjs +194 -0
  276. package/foundation/integrations/contribution-inventory.d.mts +11 -0
  277. package/foundation/integrations/contribution-inventory.mjs +56 -3
  278. package/foundation/integrations/validate-contributions.mjs +30 -1
  279. package/foundation/response/response-types.doc.mjs +14 -8
  280. package/foundation/xle/registry.mjs +4 -0
  281. package/foundation/xle/registry.test.mjs +28 -0
  282. package/package.json +11 -11
  283. package/scripts/postinstall.mjs +3 -3
@@ -8,6 +8,7 @@
8
8
  * none of them touch the integration system, the filesystem, or a source. This
9
9
  * adapter does: it resolves the project's installed integrations and what each
10
10
  * one adds, calls every discover source and keeps a saved copy of each answer,
11
+ * reads the official list the CLI carries after them,
11
12
  * locates and loads a component's doc, and names the package manager's add
12
13
  * command. Keeping that I/O here (INV21) is what lets `list`, `detail`,
13
14
  * `detail/doc`, `detail/item`, and `search` stay pure projections.
@@ -28,13 +29,29 @@ import {
28
29
  import {loadDocs} from '../../foundation/discovery/component-loader.mjs';
29
30
  import {collectIdentities} from '../../foundation/integrations/contribution-inventory.mjs';
30
31
  import {detectPackageManager} from '../../foundation/env/package-manager.mjs';
32
+ import {isValidSemver, semverCompare} from '../../foundation/env/semver.mjs';
31
33
  import {assertWithin} from '../../foundation/fs/path-safety.mjs';
34
+ import {CLI_ROOT, findInstalledPackage} from '../../foundation/fs/paths.mjs';
32
35
  import {parseDiscoverCatalog} from '../../authoring/discover/parse.mjs';
33
36
  import {AstryxError} from '../error.mjs';
34
37
  import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
35
38
 
36
39
  const SOURCE_TIMEOUT_MS = 30_000;
37
40
 
41
+ /** The name the official list goes by, as a source and on each of its entries. */
42
+ export const OFFICIAL_SOURCE_NAME = 'Astryx';
43
+
44
+ /** Where the official list comes from: the CLI package that carries it. */
45
+ const OFFICIAL_SOURCE_FROM = '@astryxdesign/cli';
46
+
47
+ /** The official list, generated from the repository when the CLI is built. */
48
+ export const OFFICIAL_LIST_FILE = path.join(
49
+ CLI_ROOT,
50
+ 'assets',
51
+ 'discover',
52
+ 'official-packages.json',
53
+ );
54
+
38
55
  const DEPENDENCY_FIELDS = [
39
56
  'dependencies',
40
57
  'devDependencies',
@@ -48,6 +65,29 @@ const DEPENDENCY_FIELDS = [
48
65
  * @typedef {import('../../authoring/discover/type').DiscoverSource} DiscoverSource
49
66
  */
50
67
 
68
+ /**
69
+ * An official package as this CLI lists it (spec:AST-051 FR11), with what its
70
+ * add command pins (FR2): the project's Core version, the Core range the
71
+ * project declares, or the latest release; with none, it pins nothing.
72
+ * @typedef {DiscoverPackage & {pin?: string}} OfficialPackage
73
+ */
74
+
75
+ /**
76
+ * The official packages the project's package.json declares: the real package
77
+ * name of each, mapped to the name it is installed under (an npm alias such as
78
+ * `"my-theme": "npm:@astryxdesign/theme-gothic@0.7.0"` installs under
79
+ * `my-theme`). `dependency` holds those declared in dependencies,
80
+ * devDependencies or optionalDependencies, which make a package the project's
81
+ * own; `any` adds peerDependencies, which only keep it from being offered.
82
+ * @typedef {{dependency: Map<string, string>, any: Set<string>}} OfficialDeclarations
83
+ */
84
+
85
+ /**
86
+ * The official list as this CLI reads it. `error` says why the file could not
87
+ * be read; the list is then empty and discover reports it like a failed source.
88
+ * @typedef {{packages: OfficialPackage[], error?: string}} OfficialList
89
+ */
90
+
51
91
  /**
52
92
  * An installed integration with what it adds, per kind. `components` is the
53
93
  * scanned component list the discover leaves have always used.
@@ -64,7 +104,8 @@ const DEPENDENCY_FIELDS = [
64
104
  * @typedef {object} SourceState
65
105
  * @property {string} name the source's own name, or where it came from when it
66
106
  * gave none
67
- * @property {string} from `astryx.config`, or the integration that exports it
107
+ * @property {string} from `astryx.config`, the integration that exports it, or
108
+ * `@astryxdesign/cli` for the official list
68
109
  * @property {'fresh' | 'saved' | 'failed'} status
69
110
  * @property {string} [generatedAt]
70
111
  * @property {boolean} [complete]
@@ -73,9 +114,10 @@ const DEPENDENCY_FIELDS = [
73
114
  */
74
115
 
75
116
  /**
76
- * Everything every source returned, merged. When two sources list the same
77
- * package, the earlier source's entry is kept.
78
- * @typedef {{sources: SourceState[], packages: Array<DiscoverPackage & {source: string}>}} CatalogResult
117
+ * Everything every source returned, merged, the official list last. When two
118
+ * sources list the same package, the earlier source's entry is kept. `official`
119
+ * marks an entry the official list supplied.
120
+ * @typedef {{sources: SourceState[], packages: Array<DiscoverPackage & {source: string, official?: true}>}} CatalogResult
79
121
  */
80
122
 
81
123
  /**
@@ -225,13 +267,151 @@ export function declaredDependencies(project) {
225
267
  return names;
226
268
  }
227
269
 
270
+ /**
271
+ * The nearest package.json at or above the project's folder, read. Discover
272
+ * runs from any folder of a project; this is the project's own manifest.
273
+ * @param {Project | undefined} project
274
+ * @returns {{dir: string, pkg: Record<string, any>} | null}
275
+ */
276
+ function nearestManifest(project) {
277
+ let dir = project?.cwd ?? process.cwd();
278
+ for (;;) {
279
+ const file = path.join(dir, 'package.json');
280
+ if (fs.existsSync(file)) {
281
+ try {
282
+ return {dir, pkg: JSON.parse(fs.readFileSync(file, 'utf8'))};
283
+ } catch {
284
+ return null;
285
+ }
286
+ }
287
+ const parent = path.dirname(dir);
288
+ if (parent === dir) return null;
289
+ dir = parent;
290
+ }
291
+ }
292
+
293
+ /** The fields that make a declared package the project's own. */
294
+ const OWN_DEPENDENCY_FIELDS = [
295
+ 'dependencies',
296
+ 'devDependencies',
297
+ 'optionalDependencies',
298
+ ];
299
+
300
+ /**
301
+ * The package a dependency entry installs: its own name, or the target of an
302
+ * npm alias (`npm:<name>@<range>`).
303
+ * @param {string} name
304
+ * @param {unknown} spec
305
+ * @returns {string}
306
+ */
307
+ function declaredPackage(name, spec) {
308
+ if (typeof spec !== 'string' || !spec.startsWith('npm:')) return name;
309
+ const target = spec.slice('npm:'.length);
310
+ const at = target.indexOf('@', 1);
311
+ return at > 0 ? target.slice(0, at) : target;
312
+ }
313
+
314
+ /**
315
+ * What the project's own package.json, the nearest one above the folder
316
+ * discover runs in, declares among the official packages.
317
+ * @param {Project | undefined} project
318
+ * @param {OfficialPackage[]} official
319
+ * @returns {OfficialDeclarations}
320
+ */
321
+ export function officialDeclarations(project, official) {
322
+ const names = new Set(official.map(pkg => pkg.package));
323
+ /** @type {OfficialDeclarations} */
324
+ const declared = {dependency: new Map(), any: new Set()};
325
+ const manifest = nearestManifest(project);
326
+ if (manifest == null) return declared;
327
+ for (const field of DEPENDENCY_FIELDS) {
328
+ for (const [local, spec] of Object.entries(manifest.pkg?.[field] ?? {})) {
329
+ const real = declaredPackage(local, spec);
330
+ if (!names.has(real)) continue;
331
+ declared.any.add(real);
332
+ if (
333
+ OWN_DEPENDENCY_FIELDS.includes(field) &&
334
+ !declared.dependency.has(real)
335
+ )
336
+ declared.dependency.set(real, local);
337
+ }
338
+ }
339
+ return declared;
340
+ }
341
+
342
+ /**
343
+ * One part of a semver range: an optional comparator and a version that may
344
+ * use `x` or `*` wildcards.
345
+ */
346
+ const RANGE_PART =
347
+ /^(?:[<>]=?|=|\^|~)?v?(?:\d+|[xX*])(?:\.(?:\d+|[xX*])){0,2}(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$/u;
348
+
349
+ /**
350
+ * Whether a dependency spec is a semver range every supported package manager
351
+ * resolves: comparators, wildcards, `||` and hyphen ranges. A dist-tag, path,
352
+ * URL, alias or workspace protocol is not.
353
+ * @param {string} spec
354
+ * @returns {boolean}
355
+ */
356
+ function isSemverRange(spec) {
357
+ const parts = spec
358
+ .trim()
359
+ .split(/\s*\|\|\s*|\s+/u)
360
+ .filter(part => part !== '-');
361
+ return parts.length > 0 && parts.every(part => RANGE_PART.test(part));
362
+ }
363
+
364
+ /**
365
+ * The project's Core: the installed copy's version, else what its own
366
+ * package.json declares (a fresh clone before install): an exact `version`, or
367
+ * a semver `range`. Neither, for a declaration no package manager can pin
368
+ * against, such as a path or a dist-tag. Null when the project has no Core.
369
+ * @param {Project | undefined} project
370
+ * @returns {{version?: string, range?: string} | null}
371
+ */
372
+ function projectCore(project) {
373
+ const installed = installedVersion(project, '@astryxdesign/core');
374
+ if (installed) return {version: installed};
375
+ const manifest = nearestManifest(project);
376
+ for (const field of OWN_DEPENDENCY_FIELDS) {
377
+ const spec = manifest?.pkg?.[field]?.['@astryxdesign/core'];
378
+ if (typeof spec !== 'string') continue;
379
+ // A space after an operator (`~ 0.6.8`, `>= 0.6.8 < 0.7.0`) is allowed by
380
+ // node-semver; join each operator to its version before reading the spec.
381
+ const range = spec.trim().replace(/([<>]=?|=|\^|~)\s+/gu, '$1');
382
+ const exact = range.replace(/^=/u, '');
383
+ if (isValidSemver(exact)) return {version: exact};
384
+ return isSemverRange(range) ? {range} : {};
385
+ }
386
+ return null;
387
+ }
388
+
389
+ /**
390
+ * Newest first: by release number, then a release before its prereleases.
391
+ * @param {string} a
392
+ * @param {string} b
393
+ * @returns {number}
394
+ */
395
+ function newerFirst(a, b) {
396
+ const byNumber = semverCompare(b, a);
397
+ if (byNumber !== 0) return byNumber;
398
+ const aRelease = !a.includes('-');
399
+ if (aRelease !== !b.includes('-')) return aRelease ? -1 : 1;
400
+ return b.localeCompare(a, 'en', {numeric: true});
401
+ }
402
+
228
403
  /**
229
404
  * The command that adds a package with the project's package manager. Discover
230
- * only prints it; it never runs it.
405
+ * only prints it; it never runs it. Asked for one version, it adds that
406
+ * version. Given the official list, it otherwise pins an official package to
407
+ * what its entry names (spec:AST-051 FR2): the project's Core version or
408
+ * declared Core range, else the latest release. A range is quoted, because
409
+ * `^`, `<`, `>`, `|` and spaces mean something to a shell.
231
410
  * @param {Project | undefined} project
411
+ * @param {{official?: OfficialPackage[]}} [options]
232
412
  * @returns {(name: string, version?: string) => string}
233
413
  */
234
- export function addCommand(project) {
414
+ export function addCommand(project, {official = []} = {}) {
235
415
  const pm = detectPackageManager(project?.cwd ?? process.cwd());
236
416
  const verb =
237
417
  pm === 'pnpm'
@@ -241,7 +421,15 @@ export function addCommand(project) {
241
421
  : pm === 'bun'
242
422
  ? 'bun add'
243
423
  : 'npm install';
244
- return (name, version) => `${verb} ${version ? `${name}@${version}` : name}`;
424
+ const byName = new Map(official.map(pkg => [pkg.package, pkg]));
425
+ return (name, version) => {
426
+ if (version) return `${verb} ${name}@${version}`;
427
+ const pin = byName.get(name)?.pin;
428
+ if (!pin) return `${verb} ${name}`;
429
+ return isValidSemver(pin)
430
+ ? `${verb} ${name}@${pin}`
431
+ : `${verb} "${name}@${pin}"`;
432
+ };
245
433
  }
246
434
 
247
435
  /**
@@ -347,8 +535,8 @@ async function callSource(source, request, label, timeoutMs) {
347
535
  }
348
536
 
349
537
  /**
350
- * Call every discover source — the project's `discover` first, then each
351
- * integration's in load order — once per distinct function. A source that
538
+ * Call the project's own discover sources — the project's `discover` first,
539
+ * then each integration's in load order — once per distinct function. A source that
352
540
  * throws, times out, or returns an invalid catalog falls back to its saved copy;
353
541
  * with none, it is reported and adds nothing. The others always count.
354
542
  *
@@ -358,7 +546,7 @@ async function callSource(source, request, label, timeoutMs) {
358
546
  * @param {{cacheDir?: string, timeoutMs?: number}} [options]
359
547
  * @returns {Promise<CatalogResult>}
360
548
  */
361
- export async function callSources(project, request = {}, options = {}) {
549
+ async function callOwnSources(project, request = {}, options = {}) {
362
550
  const {cacheDir = defaultCacheDir(), timeoutMs = SOURCE_TIMEOUT_MS} = options;
363
551
  /** @type {CatalogResult} */
364
552
  const result = {sources: [], packages: []};
@@ -433,6 +621,257 @@ export async function callSources(project, request = {}, options = {}) {
433
621
  return result;
434
622
  }
435
623
 
624
+ /**
625
+ * Call every discover source (see {@link callOwnSources}), then read the
626
+ * official list (spec:AST-051 FR11). The list comes last, so a source keeps
627
+ * its entry for an official package, or for any package of the same
628
+ * integration (FR7); the list adds the official packages no source covers.
629
+ * The result stays in source order; discover puts official packages first
630
+ * only where it shows them ({@link officialFirst}).
631
+ *
632
+ * @param {Project | undefined} project
633
+ * @param {{package?: string, version?: string}} [request]
634
+ * @param {{cacheDir?: string, timeoutMs?: number, official?: OfficialList}} [options]
635
+ * `official`: the list {@link officialList} read; read here when absent
636
+ * @returns {Promise<CatalogResult>}
637
+ */
638
+ export async function callSources(project, request = {}, options = {}) {
639
+ const {official = officialList({project}), ...rest} = options;
640
+ const own = await callOwnSources(project, request, rest);
641
+ // A source's entry wins for its package and for the integration it
642
+ // publishes under any name (FR7), so a fork or mirror keeps its place.
643
+ const seen = new Set(
644
+ own.packages.flatMap(pkg => [pkg.package, pkg.integration, ...pkg.aliases]),
645
+ );
646
+ /** @type {CatalogResult['packages']} */
647
+ const added = [];
648
+ for (const {pin: _pin, ...pkg} of official.packages) {
649
+ if (seen.has(pkg.package)) continue;
650
+ if (request.package && pkg.package !== request.package) continue;
651
+ added.push({...pkg, source: OFFICIAL_SOURCE_NAME, official: true});
652
+ }
653
+ return {
654
+ sources: [
655
+ ...own.sources,
656
+ official.error
657
+ ? {
658
+ name: OFFICIAL_SOURCE_NAME,
659
+ from: OFFICIAL_SOURCE_FROM,
660
+ status: 'failed',
661
+ error: official.error,
662
+ }
663
+ : {
664
+ name: OFFICIAL_SOURCE_NAME,
665
+ from: OFFICIAL_SOURCE_FROM,
666
+ status: 'fresh',
667
+ complete: true,
668
+ },
669
+ ],
670
+ // Source order: the first entry per integration is the one discover
671
+ // keeps. Official packages are put first only where discover shows them.
672
+ packages: [...own.packages, ...added],
673
+ };
674
+ }
675
+
676
+ /**
677
+ * Official packages first, in the official list's order; the rest keep theirs.
678
+ * @template T
679
+ * @param {T[]} items
680
+ * @param {OfficialPackage[]} official
681
+ * @param {(item: T) => string} nameOf
682
+ * @returns {T[]}
683
+ */
684
+ export function officialFirst(items, official, nameOf) {
685
+ const order = new Map(official.map((pkg, index) => [pkg.package, index]));
686
+ const rank = (/** @type {T} */ item) => order.get(nameOf(item)) ?? order.size;
687
+ return [...items].sort((a, b) => rank(a) - rank(b));
688
+ }
689
+
690
+ /**
691
+ * The official list this CLI carries (spec:AST-051 FR11). The file holds no
692
+ * versions; each entry takes them from this CLI and the project:
693
+ *
694
+ * - Its latest release is the newest release this CLI knows: its own version
695
+ * on a release CLI, or the project's Core version when that is a newer
696
+ * release.
697
+ * - A package released with Core lists a release CLI's own version and the
698
+ * project's Core version (installed, else declared exactly), so each,
699
+ * the latest release included, can be looked up. Its add command pins the project's Core version, else the Core range
700
+ * the project declares, else the latest release; nothing on a canary CLI in
701
+ * a project with no Core, which may have been published without it.
702
+ * - A canary-only package is listed only by a canary CLI, as a prerelease at
703
+ * the project's Core version when that is a prerelease, else at the CLI's
704
+ * own version when the project has no Core. It is not listed beside a
705
+ * released or declared Core, which it cannot be installed with.
706
+ *
707
+ * @param {{file?: string, version?: string, project?: Project}} [options] the
708
+ * list file and the CLI version to read it for, both defaulting to this
709
+ * CLI's own, and the project whose Core decides the versions
710
+ * @returns {OfficialList}
711
+ */
712
+ export function officialList({
713
+ file = OFFICIAL_LIST_FILE,
714
+ version,
715
+ project,
716
+ } = {}) {
717
+ try {
718
+ const cli = version ?? packageVersion(CLI_ROOT);
719
+ if (cli == null) throw new Error('the CLI package names no version');
720
+ const core = project == null ? null : projectCore(project);
721
+ const list = JSON.parse(fs.readFileSync(file, 'utf8'));
722
+ const canaryCli = /-canary\./u.test(cli);
723
+ /** @type {(v: string | undefined) => v is string} */
724
+ const isRelease = v => v != null && !v.includes('-');
725
+ const latest =
726
+ [cli, core?.version].filter(isRelease).sort(newerFirst)[0] ?? null;
727
+ /** @type {OfficialPackage[]} */
728
+ const packages = [];
729
+ for (const pkg of list.packages) {
730
+ /** @type {string[]} */
731
+ let listed = [];
732
+ /** @type {string | null} */
733
+ let entryLatest = null;
734
+ /** @type {string | undefined} */
735
+ let pin;
736
+ if (pkg.canaryOnly === true) {
737
+ if (!canaryCli) continue;
738
+ if (core == null) pin = cli;
739
+ else if (core.version && !isRelease(core.version)) pin = core.version;
740
+ else continue;
741
+ listed = [pin];
742
+ } else if (pkg.releasedWithCore === true) {
743
+ entryLatest = latest;
744
+ listed = [
745
+ ...new Set(
746
+ [isRelease(cli) ? cli : undefined, core?.version].filter(
747
+ /** @returns {v is string} */ v => v != null,
748
+ ),
749
+ ),
750
+ ].sort(newerFirst);
751
+ // The project's Core version, else the plain range it declares, else
752
+ // the latest release (spec:AST-051 FR11): a Core declared as
753
+ // `workspace:*`, a `file:` path or a dist-tag, and not installed,
754
+ // still gets a pin.
755
+ pin =
756
+ core == null
757
+ ? canaryCli
758
+ ? undefined
759
+ : cli
760
+ : (core.version ?? core.range ?? entryLatest ?? undefined);
761
+ }
762
+ packages.push({
763
+ package: pkg.package,
764
+ integration: pkg.package,
765
+ aliases: [],
766
+ ...(pkg.description ? {description: pkg.description} : {}),
767
+ latest: entryLatest,
768
+ versions: listed.map(v => ({
769
+ version: v,
770
+ publishedAt: null,
771
+ prerelease: v.includes('-'),
772
+ status: 'ok',
773
+ })),
774
+ contributions: pkg.contributions,
775
+ ...(pin ? {pin} : {}),
776
+ });
777
+ }
778
+ return {packages};
779
+ } catch (err) {
780
+ return {
781
+ packages: [],
782
+ error: `the official list could not be read (${err instanceof Error ? err.message : String(err)})`,
783
+ };
784
+ }
785
+ }
786
+
787
+ /**
788
+ * What the project has: its installed integrations plus each official package
789
+ * its own package.json declares as a dependency whose entry the official list
790
+ * supplied, official packages first. A declared official package that a source
791
+ * lists stays as that source shows it (FR7). One that failed to load as an
792
+ * integration stays out. A declared one takes what it adds from the list and
793
+ * its version from the copy the project installed, under an npm alias too.
794
+ *
795
+ * @param {Project | undefined} project
796
+ * @param {InstalledPackage[]} installed from {@link describeInstalled}
797
+ * @param {CatalogResult} catalog
798
+ * @param {OfficialDeclarations} declared from {@link officialDeclarations}
799
+ * @param {OfficialPackage[]} official the official list, for the order
800
+ * @returns {Array<InstalledPackage & {declaredOfficial?: true}>}
801
+ */
802
+ export function withDeclaredOfficial(
803
+ project,
804
+ installed,
805
+ catalog,
806
+ declared,
807
+ official,
808
+ ) {
809
+ const loaded = new Set(project?.loadedIntegrations.map(i => i.name) ?? []);
810
+ /** @type {Array<InstalledPackage & {declaredOfficial?: true}>} */
811
+ const added = [];
812
+ for (const pkg of catalog.packages) {
813
+ const local = declared.dependency.get(pkg.package);
814
+ if (!pkg.official || local == null || loaded.has(pkg.package)) continue;
815
+ const version = installedVersion(project, local);
816
+ added.push(
817
+ /** @type {InstalledPackage & {declaredOfficial: true}} */ (
818
+ /** @type {unknown} */ ({
819
+ name: pkg.package,
820
+ category: pkg.package,
821
+ ...(version ? {version} : {}),
822
+ ...(pkg.description ? {description: pkg.description} : {}),
823
+ ...itemLists(pkg.contributions),
824
+ declaredOfficial: true,
825
+ })
826
+ ),
827
+ );
828
+ }
829
+ return officialFirst([...installed, ...added], official, pkg => pkg.name);
830
+ }
831
+
832
+ /**
833
+ * Group contributions into the entry fields, one list per kind that has any.
834
+ * `components` is always present, like on a scanned package.
835
+ * @param {DiscoverPackage['contributions']} contributions
836
+ * @returns {Record<string, string[]>}
837
+ */
838
+ function itemLists(contributions) {
839
+ /** @type {Record<string, string[]>} */
840
+ const lists = {components: []};
841
+ for (const [kind, field] of KIND_FIELDS) {
842
+ const names = contributions.filter(c => c.kind === kind).map(c => c.name);
843
+ if (names.length > 0) lists[field] = names;
844
+ }
845
+ return lists;
846
+ }
847
+
848
+ /**
849
+ * The version in a package directory's package.json, if it names one.
850
+ * @param {string} dir
851
+ * @returns {string | undefined}
852
+ */
853
+ function packageVersion(dir) {
854
+ try {
855
+ const {version} = JSON.parse(
856
+ fs.readFileSync(path.join(dir, 'package.json'), 'utf8'),
857
+ );
858
+ return typeof version === 'string' && version ? version : undefined;
859
+ } catch {
860
+ return undefined;
861
+ }
862
+ }
863
+
864
+ /**
865
+ * The version of `name` the project installed, if it installed it.
866
+ * @param {Project | undefined} project
867
+ * @param {string} name
868
+ * @returns {string | undefined}
869
+ */
870
+ function installedVersion(project, name) {
871
+ const dir = findInstalledPackage(project?.cwd ?? process.cwd(), name);
872
+ return dir == null ? undefined : packageVersion(dir);
873
+ }
874
+
436
875
  /** The list and detail entry fields for each item kind, in display order. */
437
876
  export const KIND_FIELDS = /** @type {const} */ ([
438
877
  ['component', 'components'],