|
| 1 | +import { CompiledProject } from '../config-compiler/output-generator/entities/compiled-project.js'; |
| 2 | +import { ParsedProject } from '../config-compiler/parser/entities/parsed-project.js'; |
| 3 | +import { Plugin } from './entities/plugin.js'; |
| 4 | +import { PluginResolver } from './resolver.js'; |
| 5 | + |
| 6 | +type PluginName = string; |
| 7 | + |
| 8 | +const DEFAULT_PLUGINS = { |
| 9 | + 'default': 'latest', |
| 10 | + // 'default:node': 'latest', |
| 11 | +} |
| 12 | + |
| 13 | +export class PluginCollection { |
| 14 | + |
| 15 | + private plugins = new Map<PluginName, Plugin>() |
| 16 | + private resourceToPluginMapping = new Map<string, string>() |
| 17 | + private pluginToResourceMapping = new Map<string, string[]>() |
| 18 | + |
| 19 | + async initialize(project: ParsedProject): Promise<Map<string, string[]>> { |
| 20 | + const plugins = await this.resolvePlugins(project); |
| 21 | + |
| 22 | + plugins.forEach((plugin) => { |
| 23 | + this.plugins.set(plugin.name, plugin) |
| 24 | + }) |
| 25 | + |
| 26 | + const dependencyMap = await this.initializePlugins(plugins); |
| 27 | + return dependencyMap; |
| 28 | + } |
| 29 | + |
| 30 | + async getPlan(project: CompiledProject): Promise<Array<string>> { |
| 31 | + const result = new Array<string>(); |
| 32 | + for (const applyable of project.getApplySequence()) { |
| 33 | + const plugin = this.plugins.get(applyable.pluginName); |
| 34 | + if (!plugin) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + |
| 38 | + console.log('a'); |
| 39 | + |
| 40 | + // eslint-disable-next-line no-await-in-loop |
| 41 | + result.push(await plugin.generateResourcePlan(applyable) as string); |
| 42 | + } |
| 43 | + |
| 44 | + return result; |
| 45 | + } |
| 46 | + |
| 47 | + async destroy(): Promise<void> { |
| 48 | + for (const plugin of this.plugins.values()) { |
| 49 | + plugin.destroy(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private async resolvePlugins(project: ParsedProject): Promise<Plugin[]> { |
| 54 | + const pluginDefinitions: Record<string, string> = { |
| 55 | + ...DEFAULT_PLUGINS, |
| 56 | + ...project.projectConfig.plugins, |
| 57 | + }; |
| 58 | + |
| 59 | + return await Promise.all(Object.entries(pluginDefinitions).map(([name, version]) => |
| 60 | + PluginResolver.resolve(name, version) |
| 61 | + )); |
| 62 | + } |
| 63 | + |
| 64 | + private async initializePlugins(plugins: Plugin[]): Promise<Map<string, string[]>> { |
| 65 | + const responses = await Promise.all( |
| 66 | + plugins.map(async (p) => [p.name, (await p.initialize()).resourceDefinitions] as const) |
| 67 | + ); |
| 68 | + |
| 69 | + const resourceMap = new Map<string, string[]>; |
| 70 | + |
| 71 | + for (const [pluginName, definitions] of responses) { |
| 72 | + for (const definition of definitions) { |
| 73 | + // Build resource to plugin mapping |
| 74 | + if (this.resourceToPluginMapping.has(definition.type)) { |
| 75 | + throw new Error(`Duplicated types between plugin ${this.resourceToPluginMapping.get(definition.type)} and ${pluginName}`) |
| 76 | + } |
| 77 | + this.resourceToPluginMapping.set(definition.type, pluginName); |
| 78 | + |
| 79 | + // Build plugin to resource mapping |
| 80 | + if (!this.pluginToResourceMapping.has(pluginName)) { |
| 81 | + this.pluginToResourceMapping.set(pluginName, []); |
| 82 | + } |
| 83 | + this.pluginToResourceMapping.get(pluginName)!.push(definition.type); |
| 84 | + |
| 85 | + // Build resource dependency map |
| 86 | + if (resourceMap.has(definition.type)) { |
| 87 | + throw new Error(`Duplicated types between plugins ${this.resourceToPluginMapping.get(definition.type)} and ${pluginName}`); |
| 88 | + } |
| 89 | + resourceMap.set(definition.type, definition.dependencies) |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + return resourceMap; |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments