import { OperationOptions } from "../data"; import z from "zod"; import { AllFrameworkTypes, importFramework } from "@/frameworks"; import { AllResourceGroupTypes, ResourceGroupType } from "@/resourceGroups"; import { bold, colorize, spinner } from "@/cli-helpers"; import { copyFileDependencies } from "@/files"; import { addNodePackageScript, installDependencies } from "@/npm"; const configureFrameworkSchema = z.object({ args: z.object({ frameworkType: z.enum(AllFrameworkTypes), resourceGroupType: z.enum(AllResourceGroupTypes), resourceGroupName: z.string(), operation: z.enum(["apply", "revert"]), inputs: z.any().optional() }) }); export async function configureFramework(options: OperationOptions) { console.log("Configuring framework"); const operationOptions = configureFrameworkSchema.parse(options.options).args; const frameworkType = operationOptions.frameworkType; const resourceGroupType = operationOptions.resourceGroupType as ResourceGroupType; const resourceGroupName = operationOptions.resourceGroupName; const framework = await importFramework( frameworkType, resourceGroupType, resourceGroupName, process.cwd() ); if (!framework) { throw new Error("Framework not found"); } await framework.collectInputs(operationOptions.inputs); const frameworkSpinner = spinner( `${bold(`${operationOptions.operation === "apply" ? "Initializing" : "Removing"} framework configurations`)} ${colorize("yellow", frameworkType)} …` ).start(); if (operationOptions.operation === "apply") { const { files, packages } = await framework.dependsOn(); await Promise.all([ copyFileDependencies(files), installDependencies(packages) ]); const scripts = framework.scripts; const scriptKeys = Object.keys(scripts); for (let i = 0; i < scriptKeys.length; i++) { const scriptName = scriptKeys[i]; const script = scripts[scriptName]; await addNodePackageScript(scriptName, script); } await framework.generate(); } else { await framework.destroy(); } frameworkSpinner.succeed(); }