@forge/cli
12.19.012.19.0-experimental-a6c6519
out/command-line/controller/module-controller.js+
out/command-line/controller/module-controller.jsNew file+99
Index: package/out/command-line/controller/module-controller.js
===================================================================
--- package/out/command-line/controller/module-controller.js
+++ package/out/command-line/controller/module-controller.js
@@ -0,0 +1,99 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.ModuleController = void 0;
+const cli_shared_1 = require("@forge/cli-shared");
+const register_app_commands_1 = require("../register-app-commands");
+const MODULE_PRODUCTS = [register_app_commands_1.TemplateContext.JIRA, register_app_commands_1.TemplateContext.CONFLUENCE];
+class ModuleController {
+ moduleView;
+ moduleService;
+ constructor(moduleView, moduleService) {
+ this.moduleView = moduleView;
+ this.moduleService = moduleService;
+ }
+ async run(options) {
+ try {
+ await this.runModuleAdd(options);
+ }
+ finally {
+ this.moduleService.cleanup();
+ }
+ }
+ async runModuleAdd(options) {
+ const inferredProduct = options.moduleType?.split(':')[0];
+ const product = options.product ??
+ inferredProduct ??
+ (await this.moduleView.promptForList(cli_shared_1.Text.module.add.promptSelectProduct, MODULE_PRODUCTS));
+ if (!MODULE_PRODUCTS.includes(product)) {
+ throw new Error(cli_shared_1.Text.module.add.errorInvalidProduct(product));
+ }
+ const moduleMap = await this.moduleService.getAvailableModules(product);
+ const moduleChoiceMap = this.moduleService.getModuleChoice(moduleMap);
+ let selectedTemplate;
+ if (options.moduleType) {
+ selectedTemplate = moduleMap.get(options.moduleType);
+ if (!selectedTemplate) {
+ throw new Error(cli_shared_1.Text.module.add.errorFailedToResolveChoice(options.moduleType));
+ }
+ }
+ else {
+ const selectedChoice = await this.moduleView.promptForList(cli_shared_1.Text.module.add.promptSelectModule, Array.from(moduleChoiceMap.keys()));
+ selectedTemplate = moduleChoiceMap.get(selectedChoice);
+ if (!selectedTemplate) {
+ throw new Error(cli_shared_1.Text.module.add.errorFailedToResolveChoice(selectedChoice));
+ }
+ }
+ const uiType = options.uiType ?? (await this.resolveUIFramework(selectedTemplate));
+ const downloadedTemplate = await this.moduleService.downloadSelectedTemplate(selectedTemplate, uiType);
+ if (!downloadedTemplate) {
+ throw new Error(cli_shared_1.Text.module.add.errorFailedToDownloadTemplate(selectedTemplate.moduleKey));
+ }
+ const variables = await this.collectVariables(downloadedTemplate, uiType);
+ void variables;
+ this.moduleView.emptyLine();
+ }
+ async resolveUIFramework(template) {
+ if (!template.variants) {
+ return undefined;
+ }
+ const variants = Object.keys(template.variants);
+ if (variants.length > 1) {
+ return this.moduleView.promptForList(cli_shared_1.Text.module.add.promptSelectUIFramework, variants);
+ }
+ if (variants.length === 1) {
+ return variants[0];
+ }
+ return undefined;
+ }
+ async collectVariables(template, uiFramework) {
+ const variables = {};
+ const variableDefs = this.moduleService.getVariableDefinitions(template, uiFramework);
+ if (variableDefs.length === 0) {
+ return variables;
+ }
+ const { manifestPath, existingKeys } = this.moduleService.loadManifestContext(template.moduleKey);
+ const fragmentContent = template.cacheDir ? this.moduleService.readManifestFragment(template.cacheDir) : undefined;
+ for (const varDef of variableDefs) {
+ variables[varDef.name] = await this.promptForVariable(varDef, variables, existingKeys, template.moduleKey, fragmentContent, manifestPath);
+ }
+ return variables;
+ }
+ async promptForVariable(varDef, variables, existingKeys, moduleKey, fragmentContent, manifestPath) {
+ while (true) {
+ const effectiveDefault = this.moduleService.computeEffectiveDefault(varDef, variables);
+ const raw = await this.moduleView.promptForText(varDef.prompt, effectiveDefault);
+ const trimmed = (raw ?? '').trim();
+ const value = trimmed !== '' ? trimmed : effectiveDefault || '';
+ const errors = await this.moduleService.validateCandidateValue(varDef, value, existingKeys, moduleKey, fragmentContent, variables, manifestPath);
+ if (errors.length > 0) {
+ for (const err of errors) {
+ this.moduleView.warn(err);
+ }
+ continue;
+ }
+ return value;
+ }
+ }
+}
+exports.ModuleController = ModuleController;
+//# sourceMappingURL=module-controller.js.map
\ No newline at end of file