UNPKG

@autobe/agent

Version:

AI backend server code generator

78 lines 5 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.replaceImportStatements = replaceImportStatements; function replaceImportStatements(ctx) { return function (artifacts, code, decoratorType) { return __awaiter(this, void 0, void 0, function* () { const typeReferences = Array.from(new Set(Object.keys(artifacts.document.components.schemas).map((key) => key.split(".")[0]))); const compiler = yield ctx.compiler(); code = yield compiler.typescript.beautify(code); // Remove existing import statements using flexible regex patterns code = code .replace(/import\s*{\s*MyGlobal\s*}\s*from\s*["']\.\.\/MyGlobal["']\s*;?\s*/gm, "") .replace(/import\s+typia\s*,\s*{\s*tags\s*}\s*from\s*["']typia["']\s*;?\s*/gm, "") .replace(/import\s*{\s*tags\s*}\s*from\s*["']typia["']\s*;?\s*/gm, "") .replace(/import\s*{\s*tags\s*,\s*typia\s*}\s*from\s*["']typia["']\s*;?\s*/gm, "") .replace(/import\s+typia\s*from\s*["']typia["']\s*;?\s*/gm, "") .replace(/import\s*{\s*Prisma\s*}\s*from\s*["']@prisma\/client["']\s*;?\s*/gm, "") .replace(/import\s*{\s*v4\s*}\s*from\s*["']uuid["']\s*;?\s*/gm, "") .replace(/import\s*{\s*toISOStringSafe\s*}\s*from\s*["']\.\.\/util\/toISOStringSafe["']\s*;?\s*/gm, ""); // Remove any existing API structure imports // Pattern 1: ../api/structures path code = code.replace(/import\s*(?:type\s*)?{\s*[^}]+\s*}\s*from\s*["']\.\.\/api\/structures\/[^"']+["']\s*;?\s*/gm, ""); // Pattern 2: @ORGANIZATION/PROJECT-api/lib/structures path code = code.replace(/import\s*(?:type\s*)?{\s*[^}]+\s*}\s*from\s*["']@ORGANIZATION\/PROJECT-api\/lib\/structures\/[^"']+["']\s*;?\s*/gm, ""); // Remove specific type imports that match our typeReferences for (const ref of typeReferences) { // Remove any import of this specific type from any path const typeImportRegex = new RegExp(`import\\s*(?:type\\s*)?{\\s*${ref}\\s*}\\s*from\\s*["'][^"']+["']\\s*;?\\s*`, "gm"); code = code.replace(typeImportRegex, ""); } // Remove any existing decoratorType imports if LLM mistakenly added them if (decoratorType) { const decoratorTypeRegex = new RegExp(`import\\s*(?:type\\s*)?{\\s*${decoratorType}\\s*}\\s*from\\s*["']\\.\\.\/decorators\/payload\/${decoratorType}["']\\s*;?\\s*`, "gm"); code = code.replace(decoratorTypeRegex, ""); } // Build the standard imports const imports = [ 'import { MyGlobal } from "../MyGlobal";', 'import typia, { tags } from "typia";', 'import { Prisma } from "@prisma/client";', 'import { v4 } from "uuid";', 'import { toISOStringSafe } from "../util/toISOStringSafe"', ...typeReferences.map((ref) => `import { ${ref} } from "@ORGANIZATION/PROJECT-api/lib/structures/${ref}";`), ]; // Only add decoratorType import if it exists if (decoratorType) { imports.push(`import { ${decoratorType} } from "../decorators/payload/${decoratorType}"`); } code = [...imports, "", code].join("\n"); // Clean up formatting issues code = code // Remove lines with only whitespace .replace(/^\s+$/gm, "") // Replace 3+ consecutive newlines with exactly 2 newlines .replace(/\n{3,}/g, "\n\n") // Ensure proper spacing after import section .replace(/(import.*?;)(\s*)(\n(?!import|\s*$))/g, "$1\n\n$3") // Trim and ensure single trailing newline .trim() + "\n"; // fix escaped codes code = code.replace(/\\n/g, "\n").replace(/\\"/g, '"').replace(/\\'/g, "'"); // Apply final beautification code = yield compiler.typescript.beautify(code); return code; }); }; } //# sourceMappingURL=replaceImportStatements.js.map