|
| 1 | +import type {AindexProjectSeriesName, PluginOptions} from '@/plugins/plugin-core' |
| 2 | +import {AINDEX_PROJECT_SERIES_NAMES} from '@/plugins/plugin-core' |
| 3 | + |
| 4 | +export interface AindexProjectSeriesConfig { |
| 5 | + readonly name: AindexProjectSeriesName |
| 6 | + readonly src: string |
| 7 | + readonly dist: string |
| 8 | +} |
| 9 | + |
| 10 | +export interface AindexProjectSeriesProjectRef { |
| 11 | + readonly projectName: string |
| 12 | + readonly seriesName: AindexProjectSeriesName |
| 13 | + readonly seriesDir: string |
| 14 | +} |
| 15 | + |
| 16 | +export interface AindexProjectSeriesProjectNameConflict { |
| 17 | + readonly projectName: string |
| 18 | + readonly refs: readonly AindexProjectSeriesProjectRef[] |
| 19 | +} |
| 20 | + |
| 21 | +type AindexProjectSeriesOptions = Required<PluginOptions>['aindex'] |
| 22 | + |
| 23 | +export function isAindexProjectSeriesName(value: string): value is AindexProjectSeriesName { |
| 24 | + return AINDEX_PROJECT_SERIES_NAMES.includes(value as AindexProjectSeriesName) |
| 25 | +} |
| 26 | + |
| 27 | +export function resolveAindexProjectSeriesConfigs( |
| 28 | + options: Required<PluginOptions> |
| 29 | +): readonly AindexProjectSeriesConfig[] { |
| 30 | + return AINDEX_PROJECT_SERIES_NAMES.map(name => buildAindexProjectSeriesConfig(options.aindex, name)) |
| 31 | +} |
| 32 | + |
| 33 | +export function resolveAindexProjectSeriesConfig( |
| 34 | + options: Required<PluginOptions>, |
| 35 | + seriesName: AindexProjectSeriesName |
| 36 | +): AindexProjectSeriesConfig { |
| 37 | + return buildAindexProjectSeriesConfig(options.aindex, seriesName) |
| 38 | +} |
| 39 | + |
| 40 | +export function collectAindexProjectSeriesProjectNameConflicts( |
| 41 | + refs: readonly AindexProjectSeriesProjectRef[] |
| 42 | +): readonly AindexProjectSeriesProjectNameConflict[] { |
| 43 | + const refsByProjectName = new Map<string, AindexProjectSeriesProjectRef[]>() |
| 44 | + |
| 45 | + for (const ref of refs) { |
| 46 | + const existingRefs = refsByProjectName.get(ref.projectName) |
| 47 | + if (existingRefs == null) refsByProjectName.set(ref.projectName, [ref]) |
| 48 | + else existingRefs.push(ref) |
| 49 | + } |
| 50 | + |
| 51 | + return Array.from(refsByProjectName.entries(), ([projectName, projectRefs]) => ({ |
| 52 | + projectName, |
| 53 | + refs: [...projectRefs] |
| 54 | + .sort((left, right) => left.seriesName.localeCompare(right.seriesName)) |
| 55 | + })) |
| 56 | + .filter(conflict => { |
| 57 | + const uniqueSeriesNames = new Set(conflict.refs.map(ref => ref.seriesName)) |
| 58 | + return uniqueSeriesNames.size > 1 |
| 59 | + }) |
| 60 | + .sort((left, right) => left.projectName.localeCompare(right.projectName)) |
| 61 | +} |
| 62 | + |
| 63 | +function buildAindexProjectSeriesConfig( |
| 64 | + aindexOptions: AindexProjectSeriesOptions, |
| 65 | + seriesName: AindexProjectSeriesName |
| 66 | +): AindexProjectSeriesConfig { |
| 67 | + return { |
| 68 | + name: seriesName, |
| 69 | + src: aindexOptions[seriesName].src, |
| 70 | + dist: aindexOptions[seriesName].dist |
| 71 | + } |
| 72 | +} |
0 commit comments