mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 09:54:14 +00:00
feat(sdk): support for plugin icons
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
// Regenerates src/lucide-icon-names.ts from the lucide-react version installed at
|
||||
// the repo root. Run from plugin-sdk/: `node scripts/gen-lucide-icon-names.mjs`.
|
||||
//
|
||||
// The SDK ships no lucide dependency of its own (it is a dependency-light CLI), so
|
||||
// the name list is a checked-in snapshot rather than a runtime lookup — same shape
|
||||
// as KNOWN_ADDONS in src/manifest.ts.
|
||||
import { writeFileSync } from 'node:fs';
|
||||
import { createRequire } from 'node:module';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import { dirname, join } from 'node:path';
|
||||
|
||||
const here = dirname(fileURLToPath(import.meta.url));
|
||||
const require = createRequire(join(here, '..', '..', 'package.json'));
|
||||
|
||||
const dynamicIconImports = require('lucide-react/dynamicIconImports.js');
|
||||
const iconsByKebabName = dynamicIconImports.default ?? dynamicIconImports;
|
||||
const { version } = require('lucide-react/package.json');
|
||||
|
||||
const toPascalCase = (kebab) =>
|
||||
kebab.split('-').map((part) => part[0].toUpperCase() + part.slice(1)).join('');
|
||||
|
||||
const names = Object.keys(iconsByKebabName).map(toPascalCase).sort();
|
||||
|
||||
const source = `// GENERATED — do not edit by hand.
|
||||
// Snapshot of every lucide icon name (lucide-react ${version}), used by
|
||||
// \`trek-plugin validate\` to WARN on a manifest \`icon\` lucide doesn't know —
|
||||
// never a hard error (the host falls back to Blocks, and a plugin built for a
|
||||
// newer TREK may name an icon this SDK predates). Regenerate with
|
||||
// \`node scripts/gen-lucide-icon-names.mjs\`.
|
||||
export const LUCIDE_ICON_NAMES: ReadonlySet<string> = new Set([
|
||||
${names.map((n) => ' ' + JSON.stringify(n) + ',').join('\n')}
|
||||
]);
|
||||
`;
|
||||
|
||||
writeFileSync(join(here, '..', 'src', 'lucide-icon-names.ts'), source);
|
||||
console.log(`wrote ${names.length} icon names from lucide-react ${version}`);
|
||||
@@ -36,7 +36,7 @@ interface Version {
|
||||
}
|
||||
interface Entry {
|
||||
id: string; name: string; author: string; description: string; repo: string;
|
||||
homepage?: string; tags?: string[]; type: string; authorPublicKey?: string; versions: Version[];
|
||||
homepage?: string; tags?: string[]; type: string; icon?: string; authorPublicKey?: string; versions: Version[];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -114,7 +114,11 @@ export function buildEntry(opts: {
|
||||
throw new Error('this plugin was published signed — sign the update too (pass --sign) or TREK will refuse it.');
|
||||
}
|
||||
const versions = [version, ...existing.versions.filter((v) => v.version !== version.version)];
|
||||
return { ...existing, authorPublicKey: authorPublicKey ?? existing.authorPublicKey, versions };
|
||||
// Refresh the icon from the manifest when this release declares one (an author who adds
|
||||
// or changes it should see it in the store), but never wipe an icon the entry already
|
||||
// carries just because the manifest omits it.
|
||||
const icon = typeof manifest.icon === 'string' && manifest.icon ? manifest.icon : existing.icon;
|
||||
return { ...existing, icon, authorPublicKey: authorPublicKey ?? existing.authorPublicKey, versions };
|
||||
}
|
||||
|
||||
const entry: Entry = {
|
||||
@@ -126,6 +130,7 @@ export function buildEntry(opts: {
|
||||
type: String(manifest.type),
|
||||
versions: [version],
|
||||
};
|
||||
if (typeof manifest.icon === 'string' && manifest.icon) entry.icon = manifest.icon;
|
||||
if (typeof manifest.homepage === 'string') entry.homepage = manifest.homepage;
|
||||
if (Array.isArray(manifest.tags)) entry.tags = manifest.tags.map(String).slice(0, 8);
|
||||
if (authorPublicKey) entry.authorPublicKey = authorPublicKey;
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
import fs from 'node:fs';
|
||||
import path from 'node:path';
|
||||
import { validateManifest, isUnboundedRange, KNOWN_ADDONS } from '../manifest.js';
|
||||
import { LUCIDE_ICON_NAMES } from '../lucide-icon-names.js';
|
||||
import { readJsonFile } from './json.js';
|
||||
|
||||
export interface ValidateReport {
|
||||
@@ -45,6 +46,14 @@ export function validatePluginDir(dir: string): ValidateReport {
|
||||
for (const a of res.manifest?.requiredAddons ?? []) {
|
||||
if (!KNOWN_ADDONS.includes(a)) warnings.push(`requiredAddons: "${a}" is not a known TREK addon on this SDK version`);
|
||||
}
|
||||
// Same deal for `icon`: TREK resolves it against lucide at render time and falls
|
||||
// back to Blocks on a name it can't find, so a typo is silent in the UI. Warn
|
||||
// rather than error — the list is a snapshot, and a newer lucide may have names
|
||||
// this SDK predates.
|
||||
const icon = typeof raw.icon === 'string' ? raw.icon : '';
|
||||
if (icon && !LUCIDE_ICON_NAMES.has(icon)) {
|
||||
warnings.push(`icon: "${icon}" is not a known lucide icon name on this SDK version — TREK will fall back to Blocks`);
|
||||
}
|
||||
// TREK renders its UI with lucide icons only. It STRIPS emojis from the declarative
|
||||
// text your hooks return (badges, columns, warnings, PDF sections, map/calendar/photo
|
||||
// labels) and from notifications, so emojis in that text simply vanish at render.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -409,6 +409,32 @@ describe('scaffold + validate CLIs', () => {
|
||||
expect(r.warnings.some((w) => /placeholder|screenshot/.test(w))).toBe(true); // README is the unfilled template
|
||||
});
|
||||
|
||||
// TREK resolves `icon` against lucide at render time and falls back to Blocks on a
|
||||
// name it can't find, so a typo is invisible in the UI — validate has to surface it.
|
||||
it('warns on an icon lucide does not know, without failing validation', () => {
|
||||
scaffold('icon-plug', 'widget', tmp);
|
||||
const dir = path.join(tmp, 'icon-plug');
|
||||
const file = path.join(dir, 'trek-plugin.json');
|
||||
const m = JSON.parse(fs.readFileSync(file, 'utf8'));
|
||||
fs.writeFileSync(file, JSON.stringify({ ...m, icon: 'Stethscope' }, null, 2));
|
||||
|
||||
const r = validatePluginDir(dir);
|
||||
expect(r.ok).toBe(true); // a bad icon is never a hard error
|
||||
expect(r.warnings.some((w) => /icon: "Stethscope" is not a known lucide icon name/.test(w))).toBe(true);
|
||||
});
|
||||
|
||||
it('accepts any real lucide icon name, not just a curated few', () => {
|
||||
scaffold('icon-plug', 'widget', tmp);
|
||||
const dir = path.join(tmp, 'icon-plug');
|
||||
const file = path.join(dir, 'trek-plugin.json');
|
||||
const m = JSON.parse(fs.readFileSync(file, 'utf8'));
|
||||
fs.writeFileSync(file, JSON.stringify({ ...m, icon: 'Stethoscope' }, null, 2));
|
||||
|
||||
const r = validatePluginDir(dir);
|
||||
expect(r.ok).toBe(true);
|
||||
expect(r.warnings.some((w) => /lucide icon name/.test(w))).toBe(false);
|
||||
});
|
||||
|
||||
it('scaffolds a client that opts into the design kit via the marker (source stays one line)', () => {
|
||||
scaffold('kit-plug', 'widget', tmp);
|
||||
const html = fs.readFileSync(path.join(tmp, 'kit-plug', 'client', 'index.html'), 'utf8');
|
||||
@@ -618,6 +644,49 @@ describe('pack + entry (publishing automation)', () => {
|
||||
expect(v.nativeModules).toBe(false);
|
||||
});
|
||||
|
||||
// The store tile reads `icon` off the registry ENTRY (the index has no manifest to consult),
|
||||
// so if the entry generator drops it, every plugin in the store renders as a generic Blocks.
|
||||
it('carries the manifest icon onto the registry entry', () => {
|
||||
const out = path.join(tmp, 'plugin.zip');
|
||||
packPluginDir(koffi, out);
|
||||
const entry = buildEntry({
|
||||
dir: koffi, repo: 'mauriceboe/trek-plugin-koffi', tag: 'v1.0.0', zipPath: out,
|
||||
commit: 'a'.repeat(40), now: '2026-07-04T00:00:00.000Z',
|
||||
});
|
||||
expect(entry.icon).toBe('Luggage'); // koffi's trek-plugin.json declares it
|
||||
});
|
||||
|
||||
it('--merge refreshes the icon from the manifest, but never wipes one the entry already carries', () => {
|
||||
const out = path.join(tmp, 'plugin.zip');
|
||||
packPluginDir(koffi, out);
|
||||
const prior = (icon: string) => {
|
||||
const p = path.join(tmp, `prior-${icon}.json`);
|
||||
fs.writeFileSync(p, JSON.stringify({
|
||||
id: 'koffi', name: 'Koffi', author: 'TREK', description: 'x',
|
||||
repo: 'mauriceboe/trek-plugin-koffi', type: 'widget', icon,
|
||||
versions: [{ version: '0.9.0', gitTag: 'v0.9.0', commitSha: 'b'.repeat(40), downloadUrl: 'https://x/y.zip', sha256: 'c'.repeat(64), size: 10, apiVersion: 1, nativeModules: false, publishedAt: '2026-01-01T00:00:00.000Z' }],
|
||||
}));
|
||||
return p;
|
||||
};
|
||||
|
||||
// koffi's manifest declares Luggage → an author who rebrands sees it in the store
|
||||
const rebranded = buildEntry({
|
||||
dir: koffi, repo: 'mauriceboe/trek-plugin-koffi', tag: 'v1.0.0', zipPath: out,
|
||||
commit: 'a'.repeat(40), now: '2026-07-04T00:00:00.000Z', mergePath: prior('Coffee'),
|
||||
});
|
||||
expect(rebranded.icon).toBe('Luggage');
|
||||
|
||||
// a manifest that declares NO icon leaves the entry's existing one alone
|
||||
const noIconOpts = entryOptsFor('no-icon', '>=3.2.0 <4.0.0');
|
||||
const keepPath = path.join(tmp, 'keep.json');
|
||||
fs.writeFileSync(keepPath, JSON.stringify({
|
||||
id: 'no-icon', name: 'No Icon', author: 'TREK', description: 'x', repo: 'a/b',
|
||||
type: 'integration', icon: 'Coffee', versions: [],
|
||||
}));
|
||||
const kept = buildEntry({ ...noIconOpts, now: '2026-07-04T00:00:00.000Z', mergePath: keepPath });
|
||||
expect(kept.icon).toBe('Coffee');
|
||||
});
|
||||
|
||||
it('publishes an upper-bound-only range verbatim, with no floor to get wrong', () => {
|
||||
// This is why the derived floor is gone. "<4.0.0" has a first X.Y.Z of 4.0.0 but a lower
|
||||
// bound of 0.0.0, and the old regex published 4.0.0 as the MINIMUM — advertising a plugin
|
||||
|
||||
Reference in New Issue
Block a user