import { OperationOptions, type OptionsType } from "../data"; import z from "zod"; import prompts from "prompts"; import path, { basename } from "path"; import { FrameworkType } from "@/frameworks"; import { ensureResourceGroupFolder, generatePulumiProjectYaml, importResourceGroupType, ResourceGroupType } from "@/resourceGroups"; import { generatePackageJSON, generateTSConfig } from "@/workspace"; import { animateTyping, bold, colorize, delay, spinner } from "@/cli-helpers"; import { copyFileDependencies } from "@/files"; import { installDependencies, addNodePackageScript } from "@/npm"; const initResourceGroupSchema = z.object({ args: z.object({ name: z.string(), type: z.string(), framework: z.string(), frameworkInputs: z.any().optional(), inputs: z.any().optional() }) }); export async function initResourceGroup(options: OperationOptions) { const operationOptions = initResourceGroupSchema.parse(options.options).args; const noScroll = options.cmdArgs?.noScroll ?? false; const frameworkType = operationOptions.framework as FrameworkType; const resourceGroupType = operationOptions.type as ResourceGroupType; const resourceGroupName = operationOptions.name; const resourceGroupInputs = operationOptions.inputs; const resourceGroup = importResourceGroupType( resourceGroupType, resourceGroupName, process.cwd() ); if (!resourceGroup) { throw new Error("Resource group type not found"); } resourceGroup.collectInputs(resourceGroupInputs); await ensureResourceGroupFolder(resourceGroupName); const resourceGroupSpinner = spinner( `${bold("Initializing resource group")} …` ).start(); const { files, packages } = await resourceGroup.dependsOn(); await Promise.all([ copyFileDependencies(files), generatePackageJSON().then(() => installDependencies(packages)), generateTSConfig(), generatePulumiProjectYaml({ framework: frameworkType, resourceGroupName: resourceGroupName, resourceGroupType: resourceGroupType }) ]); await resourceGroup.generate(); const scripts = resourceGroup.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); } resourceGroupSpinner.succeed(); const cmdToRun = `(cd ${path.join("infrastructure", "resourceGroups", resourceGroupName)} && pulumi up)`; if (noScroll) { console.log( `Resource group ${colorize("cyan", resourceGroupName)} initialized successfully!` ); console.log(`To deploy your resources, run ${bold(cmdToRun)}\n`); } else { await animateTyping( `Resource group ${colorize("cyan", resourceGroupName)} initialized successfully!` ); await delay(500); await animateTyping(`To deploy your resources, run ${bold(cmdToRun)}\n`); } }