|
| 1 | +import { |
| 2 | + MissingParameter, |
| 3 | + MissingParameterPrompt, |
| 4 | + MissingParametersResult, |
| 5 | + ResourceMissingParameters, |
| 6 | +} from '../entities/missing-parameter.js'; |
| 7 | +import { ResourceConfig } from '../entities/resource-config.js'; |
| 8 | +import { Reporter } from '../ui/reporters/reporter.js'; |
| 9 | +import { coerceParameterValue, formatParameterPath, setValueAtPath } from '../utils/missing-parameters.js'; |
| 10 | +import { InitializationResult } from './initialize-plugins.js'; |
| 11 | + |
| 12 | +/** |
| 13 | + * Prompts the user for values that are still needed before planning: required keys that are missing and values set to |
| 14 | + * the dashboard placeholder ('<Replace me here!>'). Answers are applied to the in-memory project only. They are never |
| 15 | + * written back to the config file. |
| 16 | + * |
| 17 | + * Must run after plugins are initialized (resource schemas are needed) and before validation. |
| 18 | + */ |
| 19 | +export const MissingParametersResolver = { |
| 20 | + async run(initializationResult: InitializationResult, reporter: Reporter): Promise<void> { |
| 21 | + const { project, pluginManager, resourceDefinitions } = initializationResult; |
| 22 | + |
| 23 | + // Unknown types are reported by validation |
| 24 | + const types = [...new Set(project.resourceConfigs.map((r) => r.type))].filter((t) => resourceDefinitions.has(t)); |
| 25 | + const resourceInfoMap = new Map( |
| 26 | + (await pluginManager.getMultipleResourceInfo(types)).map((info) => [info.type, info]) |
| 27 | + ); |
| 28 | + |
| 29 | + const requests: ResourceMissingParameters[] = []; |
| 30 | + for (const resource of project.resourceConfigs) { |
| 31 | + const resourceInfo = resourceInfoMap.get(resource.type); |
| 32 | + if (!resourceInfo) { |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + const missing = await MissingParametersResolver.resolveChoices( |
| 37 | + resourceInfo.findMissingParameters(resource), |
| 38 | + resource, |
| 39 | + reporter, |
| 40 | + ); |
| 41 | + |
| 42 | + // Values known from the schema (ex: discriminators of the chosen option) are filled without asking |
| 43 | + for (const parameter of missing.filter((p) => p.knownValue !== undefined)) { |
| 44 | + MissingParametersResolver.setParameter(resource, parameter, parameter.knownValue); |
| 45 | + } |
| 46 | + |
| 47 | + const sensitiveParameters = resourceDefinitions.get(resource.type)?.sensitiveParameters ?? []; |
| 48 | + const prompts: MissingParameterPrompt[] = missing |
| 49 | + .filter((p) => p.knownValue === undefined) |
| 50 | + .map((p) => ({ |
| 51 | + ...p, |
| 52 | + label: formatParameterPath(p.path), |
| 53 | + sensitive: sensitiveParameters.includes('*') || sensitiveParameters.includes(String(p.path[0])), |
| 54 | + })); |
| 55 | + |
| 56 | + if (prompts.length > 0) { |
| 57 | + requests.push({ resource, parameters: prompts, sensitiveParameters }); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + if (requests.length === 0) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + const answers = await reporter.promptMissingParameters(requests); |
| 66 | + for (const [idx, { resource, parameters }] of requests.entries()) { |
| 67 | + for (const [parameterIdx, parameter] of parameters.entries()) { |
| 68 | + const value = coerceParameterValue(answers[idx]?.[parameterIdx], parameter.type, parameter.itemsType); |
| 69 | + MissingParametersResolver.setParameter(resource, parameter, value); |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + |
| 74 | + /** |
| 75 | + * Flattens the result into a list of parameters. Where the schema allows multiple options (oneOf / anyOf) the user is |
| 76 | + * asked to pick one first. |
| 77 | + */ |
| 78 | + async resolveChoices( |
| 79 | + result: MissingParametersResult, |
| 80 | + resource: ResourceConfig, |
| 81 | + reporter: Reporter, |
| 82 | + ): Promise<MissingParameter[]> { |
| 83 | + const parameters = [...result.parameters]; |
| 84 | + |
| 85 | + for (const choice of result.choices) { |
| 86 | + const location = choice.path.length > 0 ? ` at "${formatParameterPath(choice.path)}"` : ''; |
| 87 | + const selected = await reporter.promptOptions( |
| 88 | + `Resource "${resource.id}" requires one of the following options${location}. Which one would you like to configure?`, |
| 89 | + choice.options.map((o) => o.label), |
| 90 | + ); |
| 91 | + |
| 92 | + const option = choice.options[selected]; |
| 93 | + if (!option) { |
| 94 | + throw new Error(`Invalid option selected for resource "${resource.id}"`); |
| 95 | + } |
| 96 | + |
| 97 | + parameters.push(...await MissingParametersResolver.resolveChoices(option.result, resource, reporter)); |
| 98 | + } |
| 99 | + |
| 100 | + // The same value can be reported by more than one branch of a schema |
| 101 | + const seen = new Set<string>(); |
| 102 | + return parameters.filter((p) => { |
| 103 | + const key = JSON.stringify(p.path); |
| 104 | + if (seen.has(key)) return false; |
| 105 | + seen.add(key); |
| 106 | + return true; |
| 107 | + }); |
| 108 | + }, |
| 109 | + |
| 110 | + setParameter(resource: ResourceConfig, parameter: MissingParameter, value: unknown): void { |
| 111 | + const [key, ...rest] = parameter.path; |
| 112 | + resource.setParameter(String(key), setValueAtPath(resource.parameters[key], rest, value)); |
| 113 | + }, |
| 114 | +}; |
0 commit comments