mirror of
https://github.com/p-stream/providers.git
synced 2026-03-11 17:55:36 +00:00
improve cli error system
This commit is contained in:
parent
a4571795cc
commit
153ad33b8d
2 changed files with 49 additions and 8 deletions
|
|
@ -83,7 +83,8 @@ async function runQuestions() {
|
|||
name: 'source',
|
||||
message: 'Select a source',
|
||||
choices: sources.map((source) => ({
|
||||
message: `[${source.type.toLocaleUpperCase()}] ${source.name} ${joinMediaTypes(source.mediaTypes)}`.trim(),
|
||||
message:
|
||||
`[${source.type.toLocaleUpperCase()}] [${source.rank}] ${source.name} ${joinMediaTypes(source.mediaTypes)}`.trim(),
|
||||
name: source.id,
|
||||
})),
|
||||
},
|
||||
|
|
|
|||
|
|
@ -1,24 +1,64 @@
|
|||
import { FeatureMap, flagsAllowedInFeatures } from '@/entrypoint/utils/targets';
|
||||
import { Embed, Sourcerer } from '@/providers/base';
|
||||
import { hasDuplicates } from '@/utils/predicates';
|
||||
|
||||
export interface ProviderList {
|
||||
sources: Sourcerer[];
|
||||
embeds: Embed[];
|
||||
}
|
||||
|
||||
function findDuplicates<T>(items: T[], keyFn: (item: T) => string | number): { key: string | number; items: T[] }[] {
|
||||
const groups = new Map<string | number, T[]>();
|
||||
|
||||
for (const item of items) {
|
||||
const key = keyFn(item);
|
||||
if (!groups.has(key)) {
|
||||
groups.set(key, []);
|
||||
}
|
||||
groups.get(key)!.push(item);
|
||||
}
|
||||
|
||||
return Array.from(groups.entries())
|
||||
.filter(([_, groupItems]) => groupItems.length > 1)
|
||||
.map(([key, groupItems]) => ({ key, items: groupItems }));
|
||||
}
|
||||
|
||||
function formatDuplicateError(
|
||||
type: string,
|
||||
duplicates: { key: string | number; items: any[] }[],
|
||||
keyName: string,
|
||||
): string {
|
||||
const duplicateList = duplicates
|
||||
.map(({ key, items }) => {
|
||||
const itemNames = items.map((item) => item.name || item.id).join(', ');
|
||||
return ` ${keyName} ${key}: ${itemNames}`;
|
||||
})
|
||||
.join('\n');
|
||||
|
||||
return `${type} have duplicate ${keyName}s:\n${duplicateList}`;
|
||||
}
|
||||
|
||||
export function getProviders(features: FeatureMap, list: ProviderList): ProviderList {
|
||||
const sources = list.sources.filter((v) => !v?.disabled);
|
||||
const embeds = list.embeds.filter((v) => !v?.disabled);
|
||||
const combined = [...sources, ...embeds];
|
||||
|
||||
const anyDuplicateId = hasDuplicates(combined.map((v) => v.id));
|
||||
const anyDuplicateSourceRank = hasDuplicates(sources.map((v) => v.rank));
|
||||
const anyDuplicateEmbedRank = hasDuplicates(embeds.map((v) => v.rank));
|
||||
// Check for duplicate IDs
|
||||
const duplicateIds = findDuplicates(combined, (v) => v.id);
|
||||
if (duplicateIds.length > 0) {
|
||||
throw new Error(formatDuplicateError('Sources/embeds', duplicateIds, 'ID'));
|
||||
}
|
||||
|
||||
if (anyDuplicateId) throw new Error('Duplicate id found in sources/embeds');
|
||||
if (anyDuplicateSourceRank) throw new Error('Duplicate rank found in sources');
|
||||
if (anyDuplicateEmbedRank) throw new Error('Duplicate rank found in embeds');
|
||||
// Check for duplicate source ranks
|
||||
const duplicateSourceRanks = findDuplicates(sources, (v) => v.rank);
|
||||
if (duplicateSourceRanks.length > 0) {
|
||||
throw new Error(formatDuplicateError('Sources', duplicateSourceRanks, 'rank'));
|
||||
}
|
||||
|
||||
// Check for duplicate embed ranks
|
||||
const duplicateEmbedRanks = findDuplicates(embeds, (v) => v.rank);
|
||||
if (duplicateEmbedRanks.length > 0) {
|
||||
throw new Error(formatDuplicateError('Embeds', duplicateEmbedRanks, 'rank'));
|
||||
}
|
||||
|
||||
return {
|
||||
sources: sources.filter((s) => flagsAllowedInFeatures(features, s.flags)),
|
||||
|
|
|
|||
Loading…
Reference in a new issue