An open source design system that's fully customizable and agent ready
TypeScript
search: find a template by its display name (#7895)
Joeyfba04ed6a65a
diff --git a/.changeset/search-template-display-name.md b/.changeset/search-template-display-name.mdnew file mode 100644index 0000000000..28096e2235--- /dev/null+++ b/.changeset/search-template-display-name.md@@ -0,0 +1,7 @@+---+'@astryxdesign/cli': patch+---++[fix] `astryx search` and `astryx build` find a block by its authored display name when that name has two words or more and holds every word of the component the block shows: a query naming it, such as "rich text editor" for an integration's Rich Text Editor block, ranks it first. A display name that labels only the variant, such as "Inside a card", is not read.++@josephfarinadiff --git a/docs/specs/AST-039/spec.md b/docs/specs/AST-039/spec.mdindex 056c8c76d7..4eb66c0b63 100644--- a/docs/specs/AST-039/spec.md+++ b/docs/specs/AST-039/spec.md@@ -89,7 +89,9 @@ change.precedence over Core's keyword fallback. Discovery MUST reject a missingsource, more than one eligible same-stem source, an invalid inferred exportname, or a descriptor whose authored identity conflicts with its required- directory identity.+ directory identity. A template answers to its display name too when that+ name holds every word of the component it shows; a variant label alone does+ not.- **FR5 — Multi-file items have one confined ownership boundary.** A kind whoseitem needs nested source, token, palette, asset, or receipt files MUST defineone item-local copy and pack boundary. Discovery and authoring MUST enumeratediff --git a/packages/cli/api/search/_gather-templates.mjs b/packages/cli/api/search/_gather-templates.mjsindex d4534ce67b..863e896124 100644--- a/packages/cli/api/search/_gather-templates.mjs+++ b/packages/cli/api/search/_gather-templates.mjs@@ -8,9 +8,31 @@ import {CORE_PACKAGE} from '../../foundation/discovery/component-discovery.mjs';import {discoverTemplates, extractComponents} from '../template/template.mjs';import {listAvailableThemes} from '../theme/_adapter.mjs';import {templateLookupIds} from '../../foundation/discovery/template-adapter.mjs';+import {sameWord, wordsOf} from './_words.mjs';/** @typedef {import('./_lexical.mjs').Candidate} Candidate */+/**+ * A template's authored display name as another name and title of it, when the+ * name stands for the template: two words or more that hold every word of the+ * component the block shows (`exampleFor`), as "Rich Text Editor" does for+ * RichTextEditor. A query that is the name then names the entry whole, and one+ * that holds it as a phrase ("rich text") reaches search's title tier, above+ * entries that match one of its words. A label of the variant alone ("Inside a+ * card" for a Comments block) does not name the block, so it stays out.+ * @param {{displayName?: string, exampleFor?: string}} t+ * @returns {{aliases?: string[], titles?: string[]}}+ */+function subjectName(t) {+ const name = String(t.displayName ?? '').trim();+ const words = wordsOf(name);+ const subject = wordsOf(String(t.exampleFor ?? ''));+ if (words.length < 2 || subject.length === 0) return {};+ return subject.every(s => words.some(w => sameWord(s, w)))+ ? {aliases: [name], titles: [name]}+ : {};+}+/*** Build template candidates (page + block) from the template discovery API.* @param {string} cwd@@ -67,6 +89,9 @@ export async function gatherTemplates(cwd) {// as its prose does, not as a name: one of them matching one word of a// short idea is not a direct match.prose: t.keywords ?? [],+ // A display name that names the block's subject is another name of it+ // and its title (subjectName).+ ...subjectName(t),_displayName: t.name,_package: t.package ?? CORE_PACKAGE,_kind: t.type, // 'page' | 'block'diff --git a/packages/cli/api/search/search.integration-block.test.mjs b/packages/cli/api/search/search.integration-block.test.mjsnew file mode 100644index 0000000000..44736c0155--- /dev/null+++ b/packages/cli/api/search/search.integration-block.test.mjs@@ -0,0 +1,100 @@+// Copyright (c) Meta Platforms, Inc. and affiliates.++/**+ * @file An integration's own block answers a query that names it, though no+ * learned table lists it: its authored display name is a title search reads,+ * so "rich text editor" finds the rich text package's block first and build+ * offers it, while a variant label that does not name the block (Lab's "Inside+ * a card") captures nothing.+ */++import fs from 'node:fs';+import os from 'node:os';+import path from 'node:path';+import {fileURLToPath} from 'node:url';+import {afterAll, beforeAll, describe, expect, it} from 'vitest';+import {search} from './search.mjs';+import {build} from '../build/build.mjs';++const SLOW = 60_000;+const packages = path.resolve(+ path.dirname(fileURLToPath(import.meta.url)),+ '../../..',+);++describe('an integration block found by its display name', () => {+ /** @type {string} */+ let dir;+ beforeAll(() => {+ dir = fs.mkdtempSync(path.join(os.tmpdir(), 'astryx-integration-block-'));+ fs.writeFileSync(+ path.join(dir, 'package.json'),+ JSON.stringify({+ name: 'consumer',+ dependencies: {+ '@astryxdesign/core': '*',+ '@astryxdesign/richtext': '*',+ '@astryxdesign/lab': '*',+ },+ }),+ );+ const scope = path.join(dir, 'node_modules', '@astryxdesign');+ fs.mkdirSync(scope, {recursive: true});+ for (const name of ['core', 'richtext', 'lab'])+ fs.symlinkSync(path.join(packages, name), path.join(scope, name), 'dir');+ });+ afterAll(() => fs.rmSync(dir, {recursive: true, force: true}));++ it.each(['rich text editor', 'rich text', 'text editor'])(+ '`%s` ranks the rich text block first among templates',+ async query => {+ const {results} = (await search(query, {cwd: dir, type: 'template'}))+ .data;+ expect(results[0], JSON.stringify(results.slice(0, 3))).toMatchObject({+ name: 'RichTextEditorShowcase',+ });+ },+ SLOW,+ );++ it(+ 'a whole display name names the block outright',+ async () => {+ const {results} = (+ await search('rich text editor', {cwd: dir, type: 'template'})+ ).data;+ expect(results[0]).toMatchObject({+ name: 'RichTextEditorShowcase',+ reason: 'exact name',+ });+ },+ SLOW,+ );++ it(+ "a variant label that does not name the block's subject is no title",+ async () => {+ const {results} = (+ await search('table inside a card', {cwd: dir, type: 'template'})+ ).data;+ expect(results[0], JSON.stringify(results.slice(0, 3))).toMatchObject({+ name: 'TableInCard',+ });+ const kit = (await build('a settings form inside a card', {cwd: dir}))+ .data;+ expect(kit.blocks[0]?.name).not.toBe('CommentsInCard');+ },+ SLOW,+ );++ it(+ 'build offers the rich text block for a request that names it',+ async () => {+ const kit = (+ await build('a comment box with a rich text editor', {cwd: dir})+ ).data;+ expect(kit.blocks.map(b => b.name)).toContain('RichTextEditorShowcase');+ },+ SLOW,+ );+});