UNPKG

@limlabs/limo

Version:

Infrastructure as Code generator

138 lines (136 loc) 6.28 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.readPulumiProjectMetadata = exports.generatePulumiProjectYaml = exports.ensureResourceGroupFolder = exports.importResourceGroupType = exports.AllResourceGroupTypes = void 0; const staticsiteAws_1 = __importDefault(require("./staticsite-aws/staticsiteAws")); const fullstackAws_1 = __importDefault(require("./fullstack-aws/fullstackAws")); const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const zod_1 = require("zod"); const yaml_1 = require("../yaml"); /** * An array containing all possible resource group types. * Currently, it includes only 'fullstack-aws'. */ exports.AllResourceGroupTypes = [ "fullstack-aws", "staticsite-aws" ]; /** * Imports a resource group module based on the provided resource group type. * * @param resourceGroupType - The type of the resource group to import. * @param name - The name of the resource group. * @param directory - The directory in which the resource group is to be created. * @returns A promise that resolves to the imported resource group module. */ function importResourceGroupType(resourceGroupType, name, directory) { switch (resourceGroupType) { case "fullstack-aws": return new fullstackAws_1.default(name, directory); case "staticsite-aws": return new staticsiteAws_1.default(name, directory); default: break; } } exports.importResourceGroupType = importResourceGroupType; /** * Initializes a resourceGroup folder within the 'infrastructure' directory. * Creates the directory recursively if it does not exist. * * @param resourceGroupName - The name of the resourceGroup for which the folder is to be created. * @returns A promise that resolves when the directory has been created. */ function ensureResourceGroupFolder(resourceGroupName) { return __awaiter(this, void 0, void 0, function* () { // Rename the 'projects' folder to 'resourceGroups' if it exists if ((0, fs_1.existsSync)(path_1.default.join("infrastructure", "projects"))) { const projectPath = path_1.default.resolve("infrastructure", "projects"); yield fs_1.promises.rename(projectPath, projectPath.replace("projects", "resourceGroups")); } yield fs_1.promises.mkdir(path_1.default.join("infrastructure", "resourceGroups", resourceGroupName), { recursive: true }); }); } exports.ensureResourceGroupFolder = ensureResourceGroupFolder; /** * Generates a Pulumi project YAML file with the provided options. * * @param opts - The options to use when generating the project YAML file. * @returns A promise that resolves when the project YAML file has been written. */ function generatePulumiProjectYaml(opts) { return __awaiter(this, void 0, void 0, function* () { const { resourceGroupName: resourceGroupName, resourceGroupType: resourceGroupType, framework } = opts, resourceGroupInputs = __rest(opts, ["resourceGroupName", "resourceGroupType", "framework"]); yield ensureResourceGroupFolder(opts.resourceGroupName); const yaml = ` name: ${opts.resourceGroupName} description: A pulumi project created with limo runtime: name: nodejs options: packagemanager: pnpm config: pulumi:tags: value: pulumi:template: aws-typescript limo: resourceGroupName: ${opts.resourceGroupName} resourceGroupType: ${opts.resourceGroupType}${opts.framework ? ` framework: ${opts.framework}` : ""}${Object.keys(resourceGroupInputs).length ? ` resourceGroupInputs:${Object.entries(resourceGroupInputs) .map(([key, value]) => ` ${key}: ${value}`) .join("")}` : ""} `.trimStart(); yield fs_1.promises.writeFile(path_1.default.join("infrastructure", "resourceGroups", opts.resourceGroupName, "Pulumi.yaml"), yaml); }); } exports.generatePulumiProjectYaml = generatePulumiProjectYaml; function readPulumiProjectMetadata(opts) { return __awaiter(this, void 0, void 0, function* () { const pulumiYamlPath = path_1.default.join("infrastructure", "resourceGroups", opts.resourceGroupName, "Pulumi.yaml"); const pulumiYaml = yield fs_1.promises.readFile(pulumiYamlPath, "utf-8"); // get the settings under the limo key if (!/limo:(\n\s+.*)+/g.test(pulumiYaml)) { throw new Error(`Could not find limo settings in ${pulumiYamlPath}`); } const { limo } = (0, yaml_1.parseYaml)(pulumiYaml); const limoSchema = zod_1.z.object({ resourceGroupName: zod_1.z.string(), resourceGroupType: zod_1.z.string(), framework: zod_1.z.string().optional(), resourceGroupInputs: zod_1.z.record(zod_1.z.unknown()) }); return limoSchema.parse(limo); }); } exports.readPulumiProjectMetadata = readPulumiProjectMetadata;