@astryxdesign/cli 0.6.5-canary.01972bc → 0.6.5-canary.01fb22f
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 +2 -2
- package/api/build/_adapter.d.mts +14 -0
- package/api/build/_adapter.mjs +58 -1
- package/api/build/build.test.mjs +74 -1
- package/api/build/kit/kit.mjs +39 -7
- package/api/build/kit/rank.d.mts +8 -0
- package/api/build/kit/rank.mjs +14 -0
- package/api/build/kit/weights.d.mts +82 -0
- package/api/build/kit/weights.json +1 -0
- package/api/build/kit/weights.mjs +305 -0
- package/api/build/kit/weights.test.mjs +190 -0
- package/api/docs/docs.d.mts +6 -0
- package/api/docs/docs.depth.test.mjs +132 -0
- package/api/docs/docs.doc.mjs +31 -2
- package/api/docs/docs.mjs +44 -1
- package/api/docs/docs.type.d.mts +38 -1
- package/api/docs/docs.type.mjs +18 -1
- package/api/docs/node/node.d.mts +28 -3
- package/api/docs/node/node.mjs +146 -23
- package/api/gap-report/gap-report.d.mts +3 -1
- package/api/gap-report/gap-report.mjs +12 -2
- package/api/gap-report/gap-report.test.mjs +106 -0
- package/api/hook/list/list.d.mts +1 -1
- package/api/integration/add-theme.mjs +93 -3
- package/api/integration/add-theme.test.mjs +100 -0
- package/api/integration/integrationAddTheme.doc.mjs +1 -1
- package/api/search/search.d.mts +12 -5
- package/api/search/search.doc.mjs +4 -4
- package/api/search/search.mjs +123 -53
- package/api/search/search.test.mjs +123 -8
- package/api/search/search.type.d.mts +5 -5
- package/api/search/search.type.mjs +5 -5
- package/api/theme/build/build.icon-lineage.test.mjs +117 -0
- package/api/theme/build/build.icon-preservation.test.mjs +639 -0
- package/api/theme/build/build.mjs +146 -69
- package/api/theme/build/build.test.mjs +190 -1
- package/api/theme/build/icon-imports.d.mts +47 -0
- package/api/theme/build/icon-imports.mjs +691 -0
- package/api/theme/themeBuild.doc.d.mts +4 -1
- package/api/theme/themeBuild.doc.mjs +14 -3
- package/assets/docs/theme.doc.mjs +2 -1
- package/assets/docs/tokens.doc.mjs +3 -3
- package/assets/docs/tree/add-a-theme.doc.mjs +4 -0
- package/assets/docs/tree/debug-and-gap-reports.doc.mjs +1 -1
- package/assets/templates/blocks/components/DropdownMenuGroup/DropdownMenuGroupShowcase.doc.mjs +15 -0
- package/assets/templates/blocks/components/DropdownMenuGroup/DropdownMenuGroupShowcase.tsx +38 -0
- package/assets/templates/blocks/components/DropdownMenuGroup/DropdownMenuGroupWithSelectable.doc.mjs +20 -0
- package/assets/templates/blocks/components/DropdownMenuGroup/DropdownMenuGroupWithSelectable.tsx +48 -0
- package/clients/cli/commands/build-theme.mjs +8 -2
- package/clients/cli/commands/discover.mjs +29 -1
- package/clients/cli/commands/discover.no-source.test.mjs +75 -0
- package/clients/cli/commands/docs.depth.test.mjs +219 -0
- package/clients/cli/commands/docs.doc.mjs +15 -2
- package/clients/cli/commands/docs.mjs +222 -5
- package/clients/cli/commands/gap-report.mjs +12 -0
- package/clients/cli/commands/gap-report.test.mjs +63 -0
- package/clients/cli/commands/integration-add.doc.mjs +1 -1
- package/clients/cli/commands/search.doc.mjs +5 -4
- package/clients/cli/commands/search.mjs +30 -6
- package/clients/cli/commands/search.test.mjs +60 -16
- package/clients/cli/commands/theme-list.behavior.test.mjs +41 -0
- package/foundation/agent-docs/agent-docs.mjs +2 -1
- package/foundation/agent-docs/agent-docs.test.mjs +8 -0
- package/foundation/discovery/theme-discovery.d.mts +6 -0
- package/foundation/discovery/theme-discovery.mjs +9 -0
- package/foundation/doc-compiler/doc-loads.test.mjs +1 -1
- package/package.json +9 -9
|
@@ -10,8 +10,10 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import * as fs from 'node:fs';
|
|
13
|
+
import {isBuiltin} from 'node:module';
|
|
13
14
|
import * as path from 'node:path';
|
|
14
15
|
import {AstryxError} from '../error.mjs';
|
|
16
|
+
import {CLI_ROOT} from '../../foundation/fs/paths.mjs';
|
|
15
17
|
import {ERROR_CODES} from '../../foundation/response/error-codes.mjs';
|
|
16
18
|
import {
|
|
17
19
|
assertWithin,
|
|
@@ -21,6 +23,7 @@ import {
|
|
|
21
23
|
import {
|
|
22
24
|
discoverBundledThemes,
|
|
23
25
|
discoverThemeDirectory,
|
|
26
|
+
themeFileImports,
|
|
24
27
|
} from '../../foundation/discovery/theme-discovery.mjs';
|
|
25
28
|
import {
|
|
26
29
|
findLocalIntegrationManifestOrNull,
|
|
@@ -214,6 +217,77 @@ function escapeRegExp(s) {
|
|
|
214
217
|
return s.replace(/[.*+?^${}()|[\]\\]/gu, '\\$&');
|
|
215
218
|
}
|
|
216
219
|
|
|
220
|
+
/** What every app that uses Astryx already has: Core, and React through Core's peers. */
|
|
221
|
+
const APP_PROVIDED = new Set(['@astryxdesign/core', 'react', 'react-dom']);
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* The npm package a module specifier names, or null for a relative path, an
|
|
225
|
+
* absolute path, or a Node built-in.
|
|
226
|
+
* @param {string} specifier
|
|
227
|
+
* @returns {string | null}
|
|
228
|
+
*/
|
|
229
|
+
function packageOfSpecifier(specifier) {
|
|
230
|
+
if (specifier.startsWith('.') || specifier.startsWith('/')) return null;
|
|
231
|
+
if (isBuiltin(specifier)) return null;
|
|
232
|
+
const parts = specifier.split('/');
|
|
233
|
+
return specifier.startsWith('@') ? parts.slice(0, 2).join('/') : parts[0];
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* The npm packages a fork's copied files import that its package does not
|
|
238
|
+
* declare yet, each at the range the bundled themes are built against (the
|
|
239
|
+
* CLI's own package.json). The fork depends on them at runtime, so an app that
|
|
240
|
+
* installs the package needs them too. Derived from the imports, not copied
|
|
241
|
+
* from a base's whole dependency list. Core and React are left out, since every
|
|
242
|
+
* Astryx app has them; so is a fork of the package's own theme, whose imports
|
|
243
|
+
* the package already resolves.
|
|
244
|
+
*
|
|
245
|
+
* @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} base
|
|
246
|
+
* @param {Record<string, any>} pkg - The package's package.json.
|
|
247
|
+
* @returns {Record<string, string>} package name to range
|
|
248
|
+
*/
|
|
249
|
+
function forkDependencies(base, pkg) {
|
|
250
|
+
if (!base.bundled) return {};
|
|
251
|
+
/** @type {Record<string, any>} */
|
|
252
|
+
let cli;
|
|
253
|
+
try {
|
|
254
|
+
cli = JSON.parse(
|
|
255
|
+
fs.readFileSync(path.join(CLI_ROOT, 'package.json'), 'utf-8'),
|
|
256
|
+
);
|
|
257
|
+
} catch {
|
|
258
|
+
return {};
|
|
259
|
+
}
|
|
260
|
+
/** @type {Record<string, string>} */
|
|
261
|
+
const ranges = {
|
|
262
|
+
...cli.devDependencies,
|
|
263
|
+
...cli.peerDependencies,
|
|
264
|
+
...cli.dependencies,
|
|
265
|
+
};
|
|
266
|
+
const declared = {
|
|
267
|
+
...pkg.devDependencies,
|
|
268
|
+
...pkg.peerDependencies,
|
|
269
|
+
...pkg.dependencies,
|
|
270
|
+
};
|
|
271
|
+
/** @type {Record<string, string>} */
|
|
272
|
+
const needed = {};
|
|
273
|
+
for (const file of base.files) {
|
|
274
|
+
const code = /\.(?:[cm]?[jt]sx?)$/u.test(file);
|
|
275
|
+
if (!code || file.endsWith('.doc.mjs')) continue;
|
|
276
|
+
let specifiers;
|
|
277
|
+
try {
|
|
278
|
+
specifiers = themeFileImports(path.join(base.sourceDir, file));
|
|
279
|
+
} catch {
|
|
280
|
+
continue; // discovery already refuses a theme whose source does not parse
|
|
281
|
+
}
|
|
282
|
+
for (const specifier of specifiers) {
|
|
283
|
+
const name = packageOfSpecifier(specifier);
|
|
284
|
+
if (name == null || APP_PROVIDED.has(name) || name in declared) continue;
|
|
285
|
+
if (typeof ranges[name] === 'string') needed[name] = ranges[name];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return needed;
|
|
289
|
+
}
|
|
290
|
+
|
|
217
291
|
/**
|
|
218
292
|
* Build the write plans for a forked theme.
|
|
219
293
|
* @param {import('../../foundation/discovery/theme-discovery.mjs').DiscoveredTheme} base
|
|
@@ -467,11 +541,27 @@ export async function integrationAddTheme(name, options = {}) {
|
|
|
467
541
|
packageUpdate?.expectedOriginal ?? fs.readFileSync(packageFile);
|
|
468
542
|
const text = packageUpdate?.contents ?? expectedOriginal.toString('utf-8');
|
|
469
543
|
const current = JSON.parse(text);
|
|
470
|
-
|
|
544
|
+
let next =
|
|
545
|
+
themesCliProblem(current) != null
|
|
546
|
+
? withCliPeer(current, THEMES_CLI)
|
|
547
|
+
: current;
|
|
548
|
+
// A fork runs the base's imports: declare the packages they come from.
|
|
549
|
+
const forked = baseTheme ? forkDependencies(baseTheme, next) : {};
|
|
550
|
+
if (Object.keys(forked).length > 0) {
|
|
551
|
+
const dependencies = {...next.dependencies, ...forked};
|
|
552
|
+
next = {
|
|
553
|
+
...next,
|
|
554
|
+
dependencies: Object.fromEntries(
|
|
555
|
+
Object.keys(dependencies)
|
|
556
|
+
.sort()
|
|
557
|
+
.map(name => [name, dependencies[name]]),
|
|
558
|
+
),
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
if (next !== current) {
|
|
471
562
|
packageUpdate = {
|
|
472
563
|
contents:
|
|
473
|
-
JSON.stringify(
|
|
474
|
-
(text.endsWith('\n') ? '\n' : ''),
|
|
564
|
+
JSON.stringify(next, null, 2) + (text.endsWith('\n') ? '\n' : ''),
|
|
475
565
|
expectedOriginal,
|
|
476
566
|
};
|
|
477
567
|
}
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
2
|
|
|
3
3
|
import {afterEach, beforeEach, describe, expect, it} from 'vitest';
|
|
4
|
+
import {execFileSync} from 'node:child_process';
|
|
4
5
|
import * as fs from 'node:fs';
|
|
5
6
|
import * as path from 'node:path';
|
|
7
|
+
import {CLI_ROOT, findInstalledPackage} from '../../foundation/fs/paths.mjs';
|
|
6
8
|
import {integrationAddTheme} from './add-theme.mjs';
|
|
7
9
|
import {validateLocalIntegration} from './validate-integration.mjs';
|
|
8
10
|
|
|
@@ -473,6 +475,104 @@ describe('integrationAddTheme --from', () => {
|
|
|
473
475
|
).rejects.toMatchObject({code: 'ERR_FILE_EXISTS'});
|
|
474
476
|
});
|
|
475
477
|
|
|
478
|
+
it('declares the npm packages the forked files import, at the range the bundled themes use', async () => {
|
|
479
|
+
setup({includeFiles: false});
|
|
480
|
+
await integrationAddTheme('ocean', {cwd: tmpDir, from: 'neutral'});
|
|
481
|
+
const pkg = JSON.parse(
|
|
482
|
+
fs.readFileSync(path.join(tmpDir, 'package.json'), 'utf-8'),
|
|
483
|
+
);
|
|
484
|
+
const cli = JSON.parse(
|
|
485
|
+
fs.readFileSync(path.join(CLI_ROOT, 'package.json'), 'utf-8'),
|
|
486
|
+
);
|
|
487
|
+
// The copied icons.tsx imports lucide-react; Core and React are every
|
|
488
|
+
// app's own, so the fork does not declare them.
|
|
489
|
+
expect(pkg.dependencies).toEqual({
|
|
490
|
+
'lucide-react': cli.devDependencies['lucide-react'],
|
|
491
|
+
});
|
|
492
|
+
expect(pkg.peerDependencies).toEqual({'@astryxdesign/cli': '>=0.6.4'});
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
it('keeps a dependency the package already declares', async () => {
|
|
496
|
+
setup({includeFiles: false});
|
|
497
|
+
const file = path.join(tmpDir, 'package.json');
|
|
498
|
+
const pkg = JSON.parse(fs.readFileSync(file, 'utf-8'));
|
|
499
|
+
fs.writeFileSync(
|
|
500
|
+
file,
|
|
501
|
+
`${JSON.stringify({...pkg, dependencies: {'lucide-react': '^1.0.0'}}, null, 2)}\n`,
|
|
502
|
+
);
|
|
503
|
+
await integrationAddTheme('ocean', {cwd: tmpDir, from: 'neutral'});
|
|
504
|
+
expect(JSON.parse(fs.readFileSync(file, 'utf-8')).dependencies).toEqual({
|
|
505
|
+
'lucide-react': '^1.0.0',
|
|
506
|
+
});
|
|
507
|
+
});
|
|
508
|
+
|
|
509
|
+
it('declares nothing for a fork of a theme the package owns', async () => {
|
|
510
|
+
setup({includeFiles: false});
|
|
511
|
+
await integrationAddTheme('base', {cwd: tmpDir});
|
|
512
|
+
await integrationAddTheme('variant', {cwd: tmpDir, from: 'base'});
|
|
513
|
+
const pkg = JSON.parse(
|
|
514
|
+
fs.readFileSync(path.join(tmpDir, 'package.json'), 'utf-8'),
|
|
515
|
+
);
|
|
516
|
+
expect(pkg.dependencies).toBeUndefined();
|
|
517
|
+
});
|
|
518
|
+
|
|
519
|
+
it('builds a forked neutral theme once the package installs what --from declared', async () => {
|
|
520
|
+
setup({includeFiles: false});
|
|
521
|
+
await integrationAddTheme('ocean', {cwd: tmpDir, from: 'neutral'});
|
|
522
|
+
const pkg = JSON.parse(
|
|
523
|
+
fs.readFileSync(path.join(tmpDir, 'package.json'), 'utf-8'),
|
|
524
|
+
);
|
|
525
|
+
// Install exactly what the package declares, plus what every Astryx
|
|
526
|
+
// project has: Core and React.
|
|
527
|
+
for (const name of [
|
|
528
|
+
...Object.keys(pkg.dependencies ?? {}),
|
|
529
|
+
'@astryxdesign/core',
|
|
530
|
+
'react',
|
|
531
|
+
]) {
|
|
532
|
+
const installed = findInstalledPackage(CLI_ROOT, name);
|
|
533
|
+
expect(installed, name).not.toBeNull();
|
|
534
|
+
const link = path.join(tmpDir, 'node_modules', ...name.split('/'));
|
|
535
|
+
fs.mkdirSync(path.dirname(link), {recursive: true});
|
|
536
|
+
fs.symlinkSync(
|
|
537
|
+
fs.realpathSync(/** @type {string} */ (installed)),
|
|
538
|
+
link,
|
|
539
|
+
'dir',
|
|
540
|
+
);
|
|
541
|
+
}
|
|
542
|
+
// A separate process without NODE_PATH, so only what the package
|
|
543
|
+
// installed can resolve.
|
|
544
|
+
let status = 0;
|
|
545
|
+
let output = '';
|
|
546
|
+
try {
|
|
547
|
+
execFileSync(
|
|
548
|
+
process.execPath,
|
|
549
|
+
[
|
|
550
|
+
path.join(CLI_ROOT, 'clients/cli/bin/astryx.mjs'),
|
|
551
|
+
'theme',
|
|
552
|
+
'build',
|
|
553
|
+
'themes/ocean/oceanTheme.ts',
|
|
554
|
+
],
|
|
555
|
+
{
|
|
556
|
+
cwd: tmpDir,
|
|
557
|
+
env: {...process.env, NODE_PATH: ''},
|
|
558
|
+
encoding: 'utf-8',
|
|
559
|
+
stdio: 'pipe',
|
|
560
|
+
},
|
|
561
|
+
);
|
|
562
|
+
} catch (error) {
|
|
563
|
+
const failure = /** @type {any} */ (error);
|
|
564
|
+
status = failure.status ?? 1;
|
|
565
|
+
output = `${failure.stdout ?? ''}${failure.stderr ?? ''}`;
|
|
566
|
+
}
|
|
567
|
+
expect({status, output: status === 0 ? '' : output}).toEqual({
|
|
568
|
+
status: 0,
|
|
569
|
+
output: '',
|
|
570
|
+
});
|
|
571
|
+
expect(fs.existsSync(path.join(tmpDir, 'themes/ocean/ocean.css'))).toBe(
|
|
572
|
+
true,
|
|
573
|
+
);
|
|
574
|
+
});
|
|
575
|
+
|
|
476
576
|
it('works with different bundled themes', async () => {
|
|
477
577
|
setup();
|
|
478
578
|
const result = await integrationAddTheme('dusk', {
|
|
@@ -37,7 +37,7 @@ export const doc = {
|
|
|
37
37
|
name: 'options.from',
|
|
38
38
|
type: 'string',
|
|
39
39
|
description:
|
|
40
|
-
'Slug of an existing theme to fork as the starting point. The new theme copies the base and has no link back.',
|
|
40
|
+
'Slug of an existing theme to fork as the starting point. The new theme copies the base and has no link back, and package.json `dependencies` gains the npm packages the copied files import (other than Core and React).',
|
|
41
41
|
},
|
|
42
42
|
],
|
|
43
43
|
returns: [
|
package/api/search/search.d.mts
CHANGED
|
@@ -66,6 +66,7 @@ export function scoreQuery(term: string, tokens: string[], candidate: Candidate)
|
|
|
66
66
|
* @param {string} term - Lowercased search term.
|
|
67
67
|
* @param {object} candidate
|
|
68
68
|
* @param {string} candidate.name - Primary identifier (component/hook name, topic, template name).
|
|
69
|
+
* @param {string[]} [candidate.aliases] - Other names, scored like the name (a theme's display name).
|
|
69
70
|
* @param {string} [candidate.domain] - A component, hook, or template name
|
|
70
71
|
* also matches typed as words: `command palette` is CommandPalette.
|
|
71
72
|
* @param {string[]} [candidate.keywords] - Authored intent (componentsUsed, category words).
|
|
@@ -76,8 +77,9 @@ export function scoreQuery(term: string, tokens: string[], candidate: Candidate)
|
|
|
76
77
|
* @param {{fuzzy?: boolean}} [opts] - `fuzzy`: allow edit-distance (typo) matches. Default true; multi-word queries pass false.
|
|
77
78
|
* @returns {{score: number, reason: string} | null}
|
|
78
79
|
*/
|
|
79
|
-
export function scoreCandidate(term: string, { name, domain, keywords, weakKeywords, description, prose, guidance, }: {
|
|
80
|
+
export function scoreCandidate(term: string, { name, aliases, domain, keywords, weakKeywords, description, prose, guidance, }: {
|
|
80
81
|
name: string;
|
|
82
|
+
aliases?: string[] | undefined;
|
|
81
83
|
domain?: string | undefined;
|
|
82
84
|
keywords?: string[] | undefined;
|
|
83
85
|
weakKeywords?: string[] | undefined;
|
|
@@ -115,18 +117,18 @@ export function componentKeywords(coreDir: string, cwd: string): Promise<{
|
|
|
115
117
|
keywords: string[];
|
|
116
118
|
}[]>;
|
|
117
119
|
/**
|
|
118
|
-
* Unified ranked search across components, hooks, docs, and
|
|
120
|
+
* Unified ranked search across components, hooks, docs, templates, and themes.
|
|
119
121
|
*
|
|
120
122
|
* @param {string} query - Free-text search term.
|
|
121
123
|
* @param {object} [options]
|
|
122
124
|
* @param {string} [options.cwd]
|
|
123
|
-
* @param {'component'|'hook'|'doc'|'template'} [options.type] - Restrict to one domain.
|
|
125
|
+
* @param {'component'|'hook'|'doc'|'template'|'theme'} [options.type] - Restrict to one domain.
|
|
124
126
|
* @param {number} [options.limit] - Max results (default 20).
|
|
125
127
|
* @returns {Promise<import('./search.type.mjs').SearchResponse>}
|
|
126
128
|
*/
|
|
127
129
|
export function search(query: string, options?: {
|
|
128
130
|
cwd?: string | undefined;
|
|
129
|
-
type?: "component" | "hook" | "template" | "doc" | undefined;
|
|
131
|
+
type?: "theme" | "component" | "hook" | "template" | "doc" | undefined;
|
|
130
132
|
limit?: number | undefined;
|
|
131
133
|
}): Promise<import("./search.type.mjs").SearchResponse>;
|
|
132
134
|
export namespace SYNONYMS {
|
|
@@ -159,8 +161,13 @@ export const STOPWORDS: Set<string>;
|
|
|
159
161
|
* prefixed fields carry domain-specific payload used only by {@link toResult}.
|
|
160
162
|
*/
|
|
161
163
|
export type Candidate = {
|
|
162
|
-
domain: "component" | "hook" | "doc" | "template";
|
|
164
|
+
domain: "component" | "hook" | "doc" | "template" | "theme";
|
|
163
165
|
name: string;
|
|
166
|
+
/**
|
|
167
|
+
* - Other names the candidate answers to, scored
|
|
168
|
+
* with the same name signals: a theme's display name.
|
|
169
|
+
*/
|
|
170
|
+
aliases?: string[] | undefined;
|
|
164
171
|
keywords?: string[] | undefined;
|
|
165
172
|
weakKeywords?: string[] | undefined;
|
|
166
173
|
description?: string | undefined;
|
|
@@ -14,7 +14,7 @@ export const doc = {
|
|
|
14
14
|
namespace: 'cli/api',
|
|
15
15
|
displayName: 'search()',
|
|
16
16
|
summary:
|
|
17
|
-
'Unified ranked search across components, hooks, docs, and
|
|
17
|
+
'Unified ranked search across components, hooks, docs, templates, and themes.',
|
|
18
18
|
description:
|
|
19
19
|
'The single "I\'m looking for X" entry point across every content domain. ' +
|
|
20
20
|
'Ranking is keyword + fuzzy (not embeddings); name and keyword signals outrank ' +
|
|
@@ -35,7 +35,7 @@ export const doc = {
|
|
|
35
35
|
},
|
|
36
36
|
{
|
|
37
37
|
name: 'options.type',
|
|
38
|
-
type: "'component' | 'hook' | 'doc' | 'template'",
|
|
38
|
+
type: "'component' | 'hook' | 'doc' | 'template' | 'theme'",
|
|
39
39
|
description: 'Restrict results to a single domain.',
|
|
40
40
|
},
|
|
41
41
|
{
|
|
@@ -48,14 +48,14 @@ export const doc = {
|
|
|
48
48
|
name: 'options.cwd',
|
|
49
49
|
type: 'string',
|
|
50
50
|
description:
|
|
51
|
-
"Directory to resolve @astryxdesign/core from. A docs-only search (`type: 'doc'`) does not need it, and a search with no `type` covers the docs alone when core is missing.",
|
|
51
|
+
"Directory to resolve @astryxdesign/core from. A docs-only or themes-only search (`type: 'doc'` or `type: 'theme'`) does not need it, and a search with no `type` covers the docs and themes alone when core is missing.",
|
|
52
52
|
},
|
|
53
53
|
],
|
|
54
54
|
returns: [
|
|
55
55
|
{
|
|
56
56
|
type: 'search',
|
|
57
57
|
description:
|
|
58
|
-
'The query echoed back, `matchCount` (how many candidates matched in total, before `limit`), plus a ranked SearchResultEntry[] bounded by `limit`: each has domain, name, score, reason, description, and follow-up command, plus `import` for components and hooks, `title` for docs,
|
|
58
|
+
'The query echoed back, `matchCount` (how many candidates matched in total, before `limit`), plus a ranked SearchResultEntry[] bounded by `limit`: each has domain, name, score, reason, description, and follow-up command, plus `import` for components and hooks, `title` for docs, `displayName` and `kind` for templates, and `displayName` for themes. A doc result is the smallest part that answers: one section (with `section`, and a command that reads only it), one docs-tree route, or a topic, whose command lists its sections. `parent` is the command that opens the level above it: the section list of its topic, the namespace a tree node sits in, the Unorganized level for a flat topic, or the topic list for a top-level namespace. Every doc result also carries `package`, the npm package that authored it (for a section, the package its file came from). A theme result is one theme you can add: `name` is its slug, its command is the `astryx theme add` that copies it into the app, and `package` is the npm package that ships it (`@astryxdesign/cli` for the CLI\'s own themes).',
|
|
59
59
|
},
|
|
60
60
|
],
|
|
61
61
|
throws: [
|
package/api/search/search.mjs
CHANGED
|
@@ -7,10 +7,10 @@
|
|
|
7
7
|
* outputs. The CLI command handler is a thin wrapper around this function.
|
|
8
8
|
*
|
|
9
9
|
* `search(query)` is the single "I'm looking for X" entry point across ALL
|
|
10
|
-
* content domains — components, hooks, docs topics,
|
|
11
|
-
*
|
|
12
|
-
* (`component --list`, `hook --list`, `docs`, `template --list`)
|
|
13
|
-
* scanning; this collapses them into one ranked, typed result set.
|
|
10
|
+
* content domains — components, hooks, docs topics, templates (page + block),
|
|
11
|
+
* and themes. Finding the right thing otherwise takes separate list calls
|
|
12
|
+
* (`component --list`, `hook --list`, `docs`, `template --list`, `theme list`)
|
|
13
|
+
* plus manual scanning; this collapses them into one ranked, typed result set.
|
|
14
14
|
*
|
|
15
15
|
* Scoring is keyword + fuzzy ranking (NOT semantic / embeddings — that is a
|
|
16
16
|
* deliberate future follow-up). It reuses the same signal weighting as the
|
|
@@ -89,6 +89,7 @@ import {loadIntegrationsSafely} from '../component/_adapter.mjs';
|
|
|
89
89
|
import {levenshteinDistance} from '../../foundation/text/string-utils.mjs';
|
|
90
90
|
import {discoverTemplates, extractComponents} from '../template/template.mjs';
|
|
91
91
|
import {templateLookupIds} from '../../foundation/discovery/template-adapter.mjs';
|
|
92
|
+
import {listAvailableThemes} from '../theme/_adapter.mjs';
|
|
92
93
|
import {
|
|
93
94
|
guideEntry,
|
|
94
95
|
loadDocsCatalog,
|
|
@@ -110,8 +111,10 @@ import {setResultCoverage} from './coverage.mjs';
|
|
|
110
111
|
* A search candidate gathered from one content domain. Extra underscore-
|
|
111
112
|
* prefixed fields carry domain-specific payload used only by {@link toResult}.
|
|
112
113
|
* @typedef {object} Candidate
|
|
113
|
-
* @property {'component'|'hook'|'doc'|'template'} domain
|
|
114
|
+
* @property {'component'|'hook'|'doc'|'template'|'theme'} domain
|
|
114
115
|
* @property {string} name
|
|
116
|
+
* @property {string[]} [aliases] - Other names the candidate answers to, scored
|
|
117
|
+
* with the same name signals: a theme's display name.
|
|
115
118
|
* @property {string[]} [keywords]
|
|
116
119
|
* @property {string[]} [weakKeywords]
|
|
117
120
|
* @property {string} [description]
|
|
@@ -291,7 +294,13 @@ const isTypo = (a, b, dist) =>
|
|
|
291
294
|
TYPO_MIN_LENGTH[/** @type {1 | 2 | 3} */ (dist)];
|
|
292
295
|
|
|
293
296
|
/** Valid domain filters for `--type`. */
|
|
294
|
-
export const SEARCH_DOMAINS = ['component', 'hook', 'doc', 'template'];
|
|
297
|
+
export const SEARCH_DOMAINS = ['component', 'hook', 'doc', 'template', 'theme'];
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* The domains a search reads without @astryxdesign/core. An open search outside
|
|
301
|
+
* an app covers these alone.
|
|
302
|
+
*/
|
|
303
|
+
const CORELESS_DOMAINS = ['doc', 'theme'];
|
|
295
304
|
|
|
296
305
|
/**
|
|
297
306
|
* Filler words stripped from multi-word queries so natural-language phrasing
|
|
@@ -618,6 +627,7 @@ export function scoreQuery(term, tokens, candidate) {
|
|
|
618
627
|
// strong hits, then reward coverage so candidates matching more terms win.
|
|
619
628
|
let strongest = 0;
|
|
620
629
|
let matched = 0;
|
|
630
|
+
let tokenSum = 0;
|
|
621
631
|
/** @type {string[]} */
|
|
622
632
|
const hitTerms = [];
|
|
623
633
|
for (const tok of tokens) {
|
|
@@ -626,6 +636,7 @@ export function scoreQuery(term, tokens, candidate) {
|
|
|
626
636
|
if (h.score > strongest) strongest = h.score;
|
|
627
637
|
matched++;
|
|
628
638
|
hitTerms.push(tok);
|
|
639
|
+
tokenSum += h.score;
|
|
629
640
|
}
|
|
630
641
|
}
|
|
631
642
|
// The reverse of the title tier, a step lower: the query holds a whole title
|
|
@@ -650,14 +661,25 @@ export function scoreQuery(term, tokens, candidate) {
|
|
|
650
661
|
// (50 + bonus + coverage = 77) lost to thirty docs that each match
|
|
651
662
|
// `integration` alone, by name or in a code tick (98-108). The reader asked for
|
|
652
663
|
// both; a candidate that has both comes first, ordered among its peers by
|
|
653
|
-
// how strong its
|
|
664
|
+
// how strong its matches are. It needs one keyword-strength hit:
|
|
654
665
|
// every word mentioned in prose, or rendered by a page, is breadth, and
|
|
655
666
|
// stays on the token sum below an exact hit on one word.
|
|
667
|
+
//
|
|
668
|
+
// The TOTAL quality of matches orders candidates within this tier, not
|
|
669
|
+
// just the strongest single hit. A doc matching both words by keyword
|
|
670
|
+
// (90 + 90 = 180) outranks one matching keyword + prose (90 + 50 = 140).
|
|
671
|
+
// Before this, every all-word match whose strongest hit was a keyword (90)
|
|
672
|
+
// scored 157, burying the better match among dozens of ties.
|
|
656
673
|
if (matched === tokens.length && strongest >= STRONG_TOKEN_SCORE) {
|
|
657
674
|
return {
|
|
658
675
|
score:
|
|
659
676
|
FULL_COVERAGE_SCORE +
|
|
660
|
-
Math.
|
|
677
|
+
Math.min(
|
|
678
|
+
Math.floor(
|
|
679
|
+
(tokenSum - matched * MIN_TOKEN_SCORE) / (matched * 5),
|
|
680
|
+
),
|
|
681
|
+
8,
|
|
682
|
+
),
|
|
661
683
|
reason,
|
|
662
684
|
matched,
|
|
663
685
|
total,
|
|
@@ -697,6 +719,7 @@ export function scoreQuery(term, tokens, candidate) {
|
|
|
697
719
|
* @param {string} term - Lowercased search term.
|
|
698
720
|
* @param {object} candidate
|
|
699
721
|
* @param {string} candidate.name - Primary identifier (component/hook name, topic, template name).
|
|
722
|
+
* @param {string[]} [candidate.aliases] - Other names, scored like the name (a theme's display name).
|
|
700
723
|
* @param {string} [candidate.domain] - A component, hook, or template name
|
|
701
724
|
* also matches typed as words: `command palette` is CommandPalette.
|
|
702
725
|
* @param {string[]} [candidate.keywords] - Authored intent (componentsUsed, category words).
|
|
@@ -711,6 +734,7 @@ export function scoreCandidate(
|
|
|
711
734
|
term,
|
|
712
735
|
{
|
|
713
736
|
name,
|
|
737
|
+
aliases = [],
|
|
714
738
|
domain,
|
|
715
739
|
keywords = [],
|
|
716
740
|
weakKeywords = [],
|
|
@@ -733,40 +757,44 @@ export function scoreCandidate(
|
|
|
733
757
|
}
|
|
734
758
|
};
|
|
735
759
|
|
|
736
|
-
const nameLower = name.toLowerCase();
|
|
737
|
-
// A placed guide's name is its route, and the route's last segment is its
|
|
738
|
-
// name too, as a flat topic's is: `codemods` is cli/integrations/codemods.
|
|
739
|
-
const leafLower = nameLower.slice(nameLower.lastIndexOf('/') + 1);
|
|
740
|
-
|
|
741
760
|
// ── Name signals ────────────────────────────────────────────────
|
|
742
|
-
//
|
|
743
|
-
//
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
//
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
);
|
|
761
|
+
// An alias is a name too: a theme answers to its display name as well as
|
|
762
|
+
// its slug.
|
|
763
|
+
for (const candidateName of [name, ...aliases.filter(Boolean)]) {
|
|
764
|
+
const nameLower = candidateName.toLowerCase();
|
|
765
|
+
// A placed guide's name is its route, and the route's last segment is its
|
|
766
|
+
// name too, as a flat topic's is: `codemods` is cli/integrations/codemods.
|
|
767
|
+
const leafLower = nameLower.slice(nameLower.lastIndexOf('/') + 1);
|
|
768
|
+
|
|
769
|
+
// A plural of the name is the name: `integration` is the `integrations`
|
|
770
|
+
// guides, `tab` the `tabs` doc.
|
|
771
|
+
// A component, hook, or template name typed as words is its name:
|
|
772
|
+
// `command palette` is CommandPalette. A doc's name is a route or key,
|
|
773
|
+
// matched as written.
|
|
774
|
+
const spelled =
|
|
775
|
+
domain !== 'doc' &&
|
|
776
|
+
!/[\s_-]/.test(nameLower) &&
|
|
777
|
+
nameLower === term.replace(/\s+/g, '');
|
|
778
|
+
if (nameLower === term || leafLower === term || spelled) {
|
|
779
|
+
consider(100, 'exact name');
|
|
780
|
+
} else if (pluralOf(nameLower, term) || pluralOf(term, nameLower)) {
|
|
781
|
+
// One point under the exact spelling, so the doc named `tokens` still
|
|
782
|
+
// outranks the Token component for `tokens`.
|
|
783
|
+
consider(99, 'plural of the name');
|
|
784
|
+
} else {
|
|
785
|
+
if (sameWord(term, nameLower)) consider(95, `name "${candidateName}"`);
|
|
786
|
+
// The term is a word of the name, or starts one: "input" in TextInput.
|
|
787
|
+
else if (startsAWordOf(term, candidateName)) {
|
|
788
|
+
consider(60, `name contains "${term}"`);
|
|
789
|
+
}
|
|
790
|
+
if (fuzzy) {
|
|
791
|
+
const dist = levenshteinDistance(term, nameLower);
|
|
792
|
+
if (isTypo(term, nameLower, dist)) {
|
|
793
|
+
consider(
|
|
794
|
+
dist === 1 ? 80 : dist === 2 ? 40 : 20,
|
|
795
|
+
`similar name (distance ${dist})`,
|
|
796
|
+
);
|
|
797
|
+
}
|
|
770
798
|
}
|
|
771
799
|
}
|
|
772
800
|
}
|
|
@@ -1463,6 +1491,36 @@ async function gatherTemplates(cwd) {
|
|
|
1463
1491
|
});
|
|
1464
1492
|
}
|
|
1465
1493
|
|
|
1494
|
+
/**
|
|
1495
|
+
* Build theme candidates from bundled and integration-provided themes. Without
|
|
1496
|
+
* this, an integration's themes are invisible to `search` even though
|
|
1497
|
+
* `theme list` and `discover` already resolve them.
|
|
1498
|
+
*
|
|
1499
|
+
* A theme's slug and display name are its names, and its description is prose
|
|
1500
|
+
* (`spec:AST-050/FR14`). A theme declares no keywords, so a word it shares with
|
|
1501
|
+
* the query only through its description is a description mention: read as a
|
|
1502
|
+
* keyword, every word of the description would outrank the components and docs
|
|
1503
|
+
* that declare that word.
|
|
1504
|
+
* @param {string} cwd
|
|
1505
|
+
* @returns {Promise<Candidate[]>}
|
|
1506
|
+
*/
|
|
1507
|
+
async function gatherThemes(cwd) {
|
|
1508
|
+
let themes;
|
|
1509
|
+
try {
|
|
1510
|
+
themes = await listAvailableThemes(cwd);
|
|
1511
|
+
} catch {
|
|
1512
|
+
return [];
|
|
1513
|
+
}
|
|
1514
|
+
return themes.map(t => ({
|
|
1515
|
+
domain: 'theme',
|
|
1516
|
+
name: t.slug,
|
|
1517
|
+
aliases: t.displayName ? [t.displayName] : [],
|
|
1518
|
+
description: t.description || '',
|
|
1519
|
+
_displayName: t.displayName,
|
|
1520
|
+
_package: t.package,
|
|
1521
|
+
}));
|
|
1522
|
+
}
|
|
1523
|
+
|
|
1466
1524
|
/**
|
|
1467
1525
|
* Map a scored candidate to its public, actionable result shape. Each result
|
|
1468
1526
|
* carries enough to act on it: the domain, name, a one-line description, and
|
|
@@ -1519,6 +1577,14 @@ function toResult(c, score, reason, matchedTerms, queryTerms) {
|
|
|
1519
1577
|
command: `astryx template ${c._commandName ?? c.name} --type ${c._kind}`,
|
|
1520
1578
|
};
|
|
1521
1579
|
break;
|
|
1580
|
+
case 'theme':
|
|
1581
|
+
result = {
|
|
1582
|
+
...base,
|
|
1583
|
+
displayName: c._displayName,
|
|
1584
|
+
command: `astryx theme add ${c.name}`,
|
|
1585
|
+
...(c._package ? {package: c._package} : {}),
|
|
1586
|
+
};
|
|
1587
|
+
break;
|
|
1522
1588
|
default:
|
|
1523
1589
|
result = base;
|
|
1524
1590
|
}
|
|
@@ -1526,12 +1592,12 @@ function toResult(c, score, reason, matchedTerms, queryTerms) {
|
|
|
1526
1592
|
}
|
|
1527
1593
|
|
|
1528
1594
|
/**
|
|
1529
|
-
* Unified ranked search across components, hooks, docs, and
|
|
1595
|
+
* Unified ranked search across components, hooks, docs, templates, and themes.
|
|
1530
1596
|
*
|
|
1531
1597
|
* @param {string} query - Free-text search term.
|
|
1532
1598
|
* @param {object} [options]
|
|
1533
1599
|
* @param {string} [options.cwd]
|
|
1534
|
-
* @param {'component'|'hook'|'doc'|'template'} [options.type] - Restrict to one domain.
|
|
1600
|
+
* @param {'component'|'hook'|'doc'|'template'|'theme'} [options.type] - Restrict to one domain.
|
|
1535
1601
|
* @param {number} [options.limit] - Max results (default 20).
|
|
1536
1602
|
* @returns {Promise<import('./search.type.mjs').SearchResponse>}
|
|
1537
1603
|
*/
|
|
@@ -1568,12 +1634,14 @@ export async function search(query, options = {}) {
|
|
|
1568
1634
|
const term = String(query).trim().toLowerCase();
|
|
1569
1635
|
const tokens = tokenizeQuery(term);
|
|
1570
1636
|
|
|
1571
|
-
// `astryx docs` reads docs without @astryxdesign/core,
|
|
1572
|
-
//
|
|
1573
|
-
//
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1637
|
+
// `astryx docs` reads docs without @astryxdesign/core, and `astryx theme
|
|
1638
|
+
// list` reads themes without it (bundled themes need no project), so a
|
|
1639
|
+
// search of either must too. Every other domain reads core: asked for by
|
|
1640
|
+
// name, it is an error without core; an open search then covers the docs
|
|
1641
|
+
// and themes alone.
|
|
1642
|
+
const needsCore = !type || !CORELESS_DOMAINS.includes(type);
|
|
1643
|
+
const coreDir = needsCore ? findCoreDir(cwd) : null;
|
|
1644
|
+
if (type && needsCore && !coreDir) {
|
|
1577
1645
|
throw new AstryxError(
|
|
1578
1646
|
'Could not find @astryxdesign/core package',
|
|
1579
1647
|
undefined,
|
|
@@ -1583,17 +1651,19 @@ export async function search(query, options = {}) {
|
|
|
1583
1651
|
|
|
1584
1652
|
// Gather candidates from each requested domain in parallel.
|
|
1585
1653
|
/** @param {string} d */
|
|
1586
|
-
const wants = d =>
|
|
1587
|
-
|
|
1654
|
+
const wants = d =>
|
|
1655
|
+
(!type && (coreDir != null || CORELESS_DOMAINS.includes(d))) || type === d;
|
|
1656
|
+
const [components, hooks, docTopics, templates, themes] = await Promise.all([
|
|
1588
1657
|
wants('component')
|
|
1589
1658
|
? gatherComponents(/** @type {string} */ (coreDir), cwd)
|
|
1590
1659
|
: [],
|
|
1591
1660
|
wants('hook') ? gatherHooks(/** @type {string} */ (coreDir)) : [],
|
|
1592
1661
|
wants('doc') ? gatherDocs(cwd) : [],
|
|
1593
1662
|
wants('template') ? gatherTemplates(cwd) : [],
|
|
1663
|
+
wants('theme') ? gatherThemes(cwd) : [],
|
|
1594
1664
|
]);
|
|
1595
1665
|
|
|
1596
|
-
const all = [...components, ...hooks, ...docTopics, ...templates];
|
|
1666
|
+
const all = [...components, ...hooks, ...docTopics, ...templates, ...themes];
|
|
1597
1667
|
|
|
1598
1668
|
// Score every candidate on its own merits. The consumer groups results by
|
|
1599
1669
|
// role (page / block / component) and takes the top of each, so there's no
|
|
@@ -1610,7 +1680,7 @@ export async function search(query, options = {}) {
|
|
|
1610
1680
|
|
|
1611
1681
|
// Sort by score desc, then domain (stable order), then name.
|
|
1612
1682
|
/** @type {Record<string, number>} */
|
|
1613
|
-
const domainOrder = {component: 0, hook: 1, doc: 2, template: 3};
|
|
1683
|
+
const domainOrder = {component: 0, hook: 1, doc: 2, template: 3, theme: 4};
|
|
1614
1684
|
scored.sort(
|
|
1615
1685
|
(a, b) =>
|
|
1616
1686
|
b.score - a.score ||
|