UNPKG

@storm-software/terraform-tools

Version:

Tools for managing Terraform infrastructure within a Nx workspace.

96 lines (89 loc) 3.37 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } var _chunk2NXAAPRNjs = require('./chunk-2NXAAPRN.js'); // ../config-tools/src/utilities/run.ts var _child_process = require('child_process'); var LARGE_BUFFER = 1024 * 1e6; var run = (config, command, cwd = _nullishCoalesce(config.workspaceRoot, () => ( process.cwd())), stdio = "inherit", env = process.env) => { return _child_process.execSync.call(void 0, command, { cwd, env: { ...process.env, ...env, CLICOLOR: "true", FORCE_COLOR: "true" }, windowsHide: true, stdio, maxBuffer: LARGE_BUFFER, killSignal: "SIGTERM" }); }; // src/base/terraform-executor.ts var _shelljs = require('shelljs'); var withTerraformExecutor = (command, executorOptions = {}) => async (_options, context) => { return _chunk2NXAAPRNjs.withRunExecutor.call(void 0, `Terraform \`${command}\` Command Executor`, async (options, context2, config) => { if (!_shelljs.which.call(void 0, "tofu") || !_shelljs.which.call(void 0, "terraform")) { throw new Error( "Both OpenTofu and Terraform are not installed. Please install one of the two before running this executor." ); } if (!_shelljs.which.call(void 0, "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); }; exports.withTerraformExecutor = withTerraformExecutor;