UNPKG

4bnode

Version:

4bnode is a CLI-powered backend development platform with a built-in visual dashboard to generate, manage, and test Node.js/Express APIs faster.

39 lines (33 loc) 1.33 kB
import fs from "fs"; import path from "path"; import { getProjectRoot } from "./project.js"; import { addImportToContent, insertCodeIntoContent } from "./codegen.js"; export function getRouteFilePath(routeFile) { return path.join(getProjectRoot(), "src", "routes", routeFile); } export function addImportToRoute(routeFilePath, importStatement) { const original = fs.readFileSync(routeFilePath, "utf8"); const content = addImportToContent(original, importStatement); // Only write when the import was actually new (avoids spurious nodemon restarts). if (content !== original) { fs.writeFileSync(routeFilePath, content, "utf8"); } return content; } export function insertCodeIntoRoute(routeFilePath, codeBlock) { const content = insertCodeIntoContent( fs.readFileSync(routeFilePath, "utf8"), codeBlock ); fs.writeFileSync(routeFilePath, content, "utf8"); } export function detectHttpMethod(routeFilePath) { const content = fs.readFileSync(routeFilePath, "utf8"); const methodRegex = /router\.(get|post|put|patch|delete)\((['"`])\/.*?\2,\s*async\s*\(req,\s*res/; const match = content.match(methodRegex); return match ? match[1].toLowerCase() : "post"; } export function getRequestDataSource(method) { return method === "get" || method === "delete" ? "req.query" : "req.body"; }