UNPKG

@storm-software/terraform-tools

Version:

Tools for managing Terraform infrastructure within a Nx workspace.

96 lines (92 loc) 3.03 kB
import { withRunExecutor } from "./chunk-WYONVDTO.mjs"; // ../config-tools/src/utilities/run.ts import { exec, execSync } from "node:child_process"; var LARGE_BUFFER = 1024 * 1e6; var run = (config, command, cwd = config.workspaceRoot ?? process.cwd(), stdio = "inherit", env = process.env) => { return execSync(command, { cwd, env: { ...process.env, ...env, CLICOLOR: "true", FORCE_COLOR: "true" }, windowsHide: true, stdio, maxBuffer: LARGE_BUFFER, killSignal: "SIGTERM" }); }; // src/base/terraform-executor.ts import { which } from "shelljs"; var withTerraformExecutor = (command, executorOptions = {}) => async (_options, context) => { return withRunExecutor( `Terraform \`${command}\` Command Executor`, async (options, context2, config) => { if (!which("tofu") || !which("terraform")) { throw new Error( "Both OpenTofu and Terraform are not installed. Please install one of the two before running this executor." ); } if (!which("terragrunt")) { throw new Error( "Terragrunt is not installed. Please install them before running this executor." ); } const { backendConfig = [], planFile, autoApproval, formatWrite, upgrade, migrateState, lock, varFile, varString, reconfigure } = options; let jsonBackendConfig = backendConfig; if (typeof jsonBackendConfig === "string") { jsonBackendConfig = JSON.parse(jsonBackendConfig); } run( config, [ "terragrunt", command, ...jsonBackendConfig.map( (config2) => `-backend-config="${config2.key}=${config2.name}"` ), command === "plan" && planFile && `-out ${planFile}`, command === "plan" && varFile && `--var-file ${varFile}`, command === "plan" && varString && `--var ${varString}`, command === "destroy" && autoApproval && "-auto-approve", command === "apply" && autoApproval && "-auto-approve", command === "apply" && planFile, command === "apply" && varString && `--var ${varString}`, command === "fmt" && "--recursive", command === "fmt" && !formatWrite && "--check --list", command === "init" && upgrade && "-upgrade", command === "init" && migrateState && "-migrate-state", command === "init" && reconfigure && "-reconfigure", command === "providers" && lock && "lock", command === "test" && varFile && `--var-file ${varFile}`, command === "test" && varString && `--var ${varString}` ].filter(Boolean).join(" "), options.sourceRoot, "inherit", process.env.CI ? { TF_IN_AUTOMATION: "true", TF_INPUT: "0" } : {} ); return null; }, executorOptions )(_options, context); }; export { withTerraformExecutor };