import fs from "fs/promises"; import { existsSync } from "fs"; import { ResourceGroupType } from "@/resourceGroups"; import { cwd } from "process"; import path from "path"; import NextJsFramework from "./nextjs/framework"; import TanstackStartFramework from "./tanstack-start/framework"; import { BaseTemplate } from "@/templates"; export const AllFrameworkTypes = [ "nextjs", "tanstack-start", "unknown" ] as const; /** * Represents the type of framework being used. * * - 'nextjs': Indicates that the framework is Next.js. * - 'tanstack-start': Indicates that the framework is TanStack Start. * - 'unknown': Indicates that the framework is unknown. */ export type FrameworkType = (typeof AllFrameworkTypes)[number]; /** * Detects the framework being used in the resource group. * * @returns {Promise} The detected framework type. */ export async function detectFramework( directory: string = cwd() ): Promise { const baseDirectory = path.resolve(directory); const nodePackageJson = path.join(baseDirectory, "package.json"); if (!existsSync(nodePackageJson)) { console.log("No package.json found in the current directory."); return "unknown"; } const packageJSON = await fs.readFile(nodePackageJson, "utf-8"); const packageJSONParsed = JSON.parse(packageJSON); // check for next in dependencies const dependencies = Object.keys(packageJSONParsed.dependencies); if (dependencies.includes("next")) { return "nextjs"; } if (dependencies.includes("@tanstack/start")) { return "tanstack-start"; } return "unknown"; } /** * Gets the supported resource group types for a given framework. * * @param {BaseTemplate} framework - The framework to get supported resource group types for. * * @returns {Promise} The supported resource group types for the given framework. */ export async function getSupportedResourceGroupTypesForFramework( framework: FrameworkType ) { switch (framework) { case "nextjs": return NextJsFramework.resourceGroupsSupported; case "tanstack-start": return TanstackStartFramework.resourceGroupsSupported; default: return []; } } /** * Imports a framework based on the given framework type. * * @param {FrameworkType} frameworkType - The type of framework to import. * @param {ResourceGroupType} resourceGroupType - The type of resource group to import. * @param {string} resourceGroupName - the name of the resource group to import. * @param {string} directory - The directory to import the framework into. * * @returns {Promise} The imported framework. */ export async function importFramework( frameworkType: FrameworkType, resourceGroupType: ResourceGroupType, resourceGroupName: string, directory: string = cwd() ): Promise { if (!AllFrameworkTypes.includes(frameworkType)) { throw new Error(`Unsupported framework type: ${frameworkType}`); } switch (frameworkType) { case "nextjs": return new NextJsFramework( resourceGroupName, directory, resourceGroupType ); case "tanstack-start": return new TanstackStartFramework( resourceGroupName, directory, resourceGroupType ); default: throw new Error(`Unsupported framework type: ${frameworkType}`); } }