mirror of
https://github.com/p-stream/providers.git
synced 2026-04-21 15:02:18 +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',
|
name: 'source',
|
||||||
message: 'Select a source',
|
message: 'Select a source',
|
||||||
choices: sources.map((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,
|
name: source.id,
|
||||||
})),
|
})),
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -1,24 +1,64 @@
|
||||||
import { FeatureMap, flagsAllowedInFeatures } from '@/entrypoint/utils/targets';
|
import { FeatureMap, flagsAllowedInFeatures } from '@/entrypoint/utils/targets';
|
||||||
import { Embed, Sourcerer } from '@/providers/base';
|
import { Embed, Sourcerer } from '@/providers/base';
|
||||||
import { hasDuplicates } from '@/utils/predicates';
|
|
||||||
|
|
||||||
export interface ProviderList {
|
export interface ProviderList {
|
||||||
sources: Sourcerer[];
|
sources: Sourcerer[];
|
||||||
embeds: Embed[];
|
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 {
|
export function getProviders(features: FeatureMap, list: ProviderList): ProviderList {
|
||||||
const sources = list.sources.filter((v) => !v?.disabled);
|
const sources = list.sources.filter((v) => !v?.disabled);
|
||||||
const embeds = list.embeds.filter((v) => !v?.disabled);
|
const embeds = list.embeds.filter((v) => !v?.disabled);
|
||||||
const combined = [...sources, ...embeds];
|
const combined = [...sources, ...embeds];
|
||||||
|
|
||||||
const anyDuplicateId = hasDuplicates(combined.map((v) => v.id));
|
// Check for duplicate IDs
|
||||||
const anyDuplicateSourceRank = hasDuplicates(sources.map((v) => v.rank));
|
const duplicateIds = findDuplicates(combined, (v) => v.id);
|
||||||
const anyDuplicateEmbedRank = hasDuplicates(embeds.map((v) => v.rank));
|
if (duplicateIds.length > 0) {
|
||||||
|
throw new Error(formatDuplicateError('Sources/embeds', duplicateIds, 'ID'));
|
||||||
|
}
|
||||||
|
|
||||||
if (anyDuplicateId) throw new Error('Duplicate id found in sources/embeds');
|
// Check for duplicate source ranks
|
||||||
if (anyDuplicateSourceRank) throw new Error('Duplicate rank found in sources');
|
const duplicateSourceRanks = findDuplicates(sources, (v) => v.rank);
|
||||||
if (anyDuplicateEmbedRank) throw new Error('Duplicate rank found in embeds');
|
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 {
|
return {
|
||||||
sources: sources.filter((s) => flagsAllowedInFeatures(features, s.flags)),
|
sources: sources.filter((s) => flagsAllowedInFeatures(features, s.flags)),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue