UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

138 lines (137 loc) 4.5 kB
import { existsSync } from "node:fs"; import { readFile, writeFile } from "node:fs/promises"; async function injectRouteHelpers(filePath, routePath, mode) { if (!existsSync(filePath)) { return false; } try { let content = await readFile(filePath, "utf-8"); let modified = false; const hasTypeRoute = /^type\s+Route\s*=/m.test(content); const hasConstRoute = /^const\s+route\s*=/m.test(content); if (mode === "runtime" && !hasConstRoute) { const { updatedContent } = addCreateRouteImport(content); content = updatedContent; const routeDeclaration = `const route = createRoute<'${routePath}'>()`; content = insertAfterImports(content, routeDeclaration); modified = true; } if (mode === "type" && !hasTypeRoute) { const { updatedContent } = addRouteTypeImport(content); content = updatedContent; const typeDeclaration = `type Route = RouteType<'${routePath}'>`; content = insertAfterImports(content, typeDeclaration); modified = true; } if (modified) { await writeFile(filePath, content, "utf-8"); return true; } return false; } catch (error) { console.error(`Failed to inject route helpers into ${filePath}:`, error); return false; } } function addCreateRouteImport(content) { if (/import\s+[^'"]*createRoute[^'"]*from\s+['"]one['"]/m.test(content)) { return { updatedContent: content, importAdded: false }; } const oneImportRegex = /import\s+{([^}]*)}\s+from\s+['"]one['"]/m; const match = content.match(oneImportRegex); if (match) { const existingImports = match[1].trim(); const newImports = existingImports ? `${existingImports}, createRoute` : "createRoute"; const updatedContent = content.replace(oneImportRegex, `import { ${newImports} } from 'one'`); return { updatedContent, importAdded: true }; } const lastImportIndex = findLastImportIndex(content); if (lastImportIndex >= 0) { const lines = content.split("\n"); lines.splice(lastImportIndex + 1, 0, `import { createRoute } from 'one'`); return { updatedContent: lines.join("\n"), importAdded: true }; } const newImport = `import { createRoute } from 'one' `; return { updatedContent: newImport + content, importAdded: true }; } function addRouteTypeImport(content) { if (/import\s+type\s+[^'"]*RouteType[^'"]*from\s+['"]one['"]/m.test(content)) { return { updatedContent: content, importAdded: false }; } const oneTypeImportRegex = /import\s+type\s+{([^}]*)}\s+from\s+['"]one['"]/m; const match = content.match(oneTypeImportRegex); if (match) { const existingImports = match[1].trim(); const newImports = existingImports ? `${existingImports}, RouteType` : "RouteType"; const updatedContent = content.replace(oneTypeImportRegex, `import type { ${newImports} } from 'one'`); return { updatedContent, importAdded: true }; } const lastImportIndex = findLastImportIndex(content); if (lastImportIndex >= 0) { const lines = content.split("\n"); lines.splice(lastImportIndex + 1, 0, `import type { RouteType } from 'one'`); return { updatedContent: lines.join("\n"), importAdded: true }; } const newImport = `import type { RouteType } from 'one' `; return { updatedContent: newImport + content, importAdded: true }; } function findLastImportIndex(content) { const lines = content.split("\n"); let lastImportIndex = -1; for (let i = 0; i < lines.length; i++) { const line = lines[i].trim(); if (line.startsWith("import ") || lastImportIndex >= 0 && (line.startsWith("from ") || line === "}")) { lastImportIndex = i; } else if (lastImportIndex >= 0 && line && !line.startsWith("//")) { break; } } return lastImportIndex; } function insertAfterImports(content, codeToInsert) { const lines = content.split("\n"); const lastImportIndex = findLastImportIndex(content); if (lastImportIndex >= 0) { const nextLine = lines[lastImportIndex + 1]; const hasBlankLine = nextLine === ""; if (hasBlankLine) { lines.splice(lastImportIndex + 2, 0, codeToInsert, ""); } else { lines.splice(lastImportIndex + 1, 0, "", codeToInsert, ""); } return lines.join("\n"); } return codeToInsert + "\n\n" + content; } export { injectRouteHelpers }; //# sourceMappingURL=injectRouteHelpers.mjs.map