Initial import
This commit is contained in:
45
src/samples/registry.ts
Executable file
45
src/samples/registry.ts
Executable file
@@ -0,0 +1,45 @@
|
||||
import type { SampleMeta, SampleModule } from '../widgets/core';
|
||||
|
||||
export type SampleEntry = {
|
||||
modulePath: string;
|
||||
load: () => Promise<SampleModule>;
|
||||
};
|
||||
|
||||
export type LoadedSampleEntry = {
|
||||
modulePath: string;
|
||||
Sample: SampleModule['Sample'];
|
||||
sampleMeta: SampleMeta;
|
||||
};
|
||||
|
||||
function sortSamples(left: LoadedSampleEntry, right: LoadedSampleEntry) {
|
||||
return (
|
||||
left.sampleMeta.componentId.localeCompare(right.sampleMeta.componentId) ||
|
||||
Number(right.sampleMeta.kind === 'base') - Number(left.sampleMeta.kind === 'base') ||
|
||||
(left.sampleMeta.order ?? Number.MAX_SAFE_INTEGER) -
|
||||
(right.sampleMeta.order ?? Number.MAX_SAFE_INTEGER) ||
|
||||
left.sampleMeta.title.localeCompare(right.sampleMeta.title)
|
||||
);
|
||||
}
|
||||
|
||||
export async function resolveSampleEntries(
|
||||
entries: SampleEntry[],
|
||||
pathFilter?: string,
|
||||
) {
|
||||
const filteredEntries = pathFilter
|
||||
? entries.filter((entry) => entry.modulePath.includes(pathFilter))
|
||||
: entries;
|
||||
|
||||
const loadedEntries = await Promise.all(
|
||||
filteredEntries.map(async (entry) => {
|
||||
const moduleExports = await entry.load();
|
||||
|
||||
return {
|
||||
modulePath: entry.modulePath,
|
||||
Sample: moduleExports.Sample,
|
||||
sampleMeta: moduleExports.sampleMeta,
|
||||
} satisfies LoadedSampleEntry;
|
||||
}),
|
||||
);
|
||||
|
||||
return loadedEntries.sort(sortSamples);
|
||||
}
|
||||
Reference in New Issue
Block a user