UNPKG

one

Version:

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

93 lines (91 loc) 3.95 kB
import { existsSync } from "node:fs"; import { readFile, writeFile } from "node:fs/promises"; async function injectRouteHelpers(filePath, routePath, mode) { if (!existsSync(filePath)) return !1; try { let content = await readFile(filePath, "utf-8"), modified = !1; const hasTypeRoute = /^type\s+Route\s*=/m.test(content), 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 = !0; } if (mode === "type" && !hasTypeRoute) { const { updatedContent } = addRouteTypeImport(content); content = updatedContent; const typeDeclaration = `type Route = RouteType<'${routePath}'>`; content = insertAfterImports(content, typeDeclaration), modified = !0; } return modified ? (await writeFile(filePath, content, "utf-8"), !0) : !1; } catch (error) { return console.error(`Failed to inject route helpers into ${filePath}:`, error), !1; } } function addCreateRouteImport(content) { if (/import\s+[^'"]*createRoute[^'"]*from\s+['"]one['"]/m.test(content)) return { updatedContent: content, importAdded: !1 }; const oneImportRegex = /import\s+{([^}]*)}\s+from\s+['"]one['"]/m, match = content.match(oneImportRegex); if (match) { const existingImports = match[1].trim(), newImports = existingImports ? `${existingImports}, createRoute` : "createRoute"; return { updatedContent: content.replace( oneImportRegex, `import { ${newImports} } from 'one'` ), importAdded: !0 }; } const lastImportIndex = findLastImportIndex(content); if (lastImportIndex >= 0) { const lines = content.split(` `); return lines.splice(lastImportIndex + 1, 0, "import { createRoute } from 'one'"), { updatedContent: lines.join(` `), importAdded: !0 }; } return { updatedContent: `import { createRoute } from 'one' ` + content, importAdded: !0 }; } function addRouteTypeImport(content) { if (/import\s+type\s+[^'"]*RouteType[^'"]*from\s+['"]one['"]/m.test(content)) return { updatedContent: content, importAdded: !1 }; const oneTypeImportRegex = /import\s+type\s+{([^}]*)}\s+from\s+['"]one['"]/m, match = content.match(oneTypeImportRegex); if (match) { const existingImports = match[1].trim(), newImports = existingImports ? `${existingImports}, RouteType` : "RouteType"; return { updatedContent: content.replace( oneTypeImportRegex, `import type { ${newImports} } from 'one'` ), importAdded: !0 }; } const lastImportIndex = findLastImportIndex(content); if (lastImportIndex >= 0) { const lines = content.split(` `); return lines.splice(lastImportIndex + 1, 0, "import type { RouteType } from 'one'"), { updatedContent: lines.join(` `), importAdded: !0 }; } return { updatedContent: `import type { RouteType } from 'one' ` + content, importAdded: !0 }; } function findLastImportIndex(content) { const lines = content.split(` `); 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(` `), lastImportIndex = findLastImportIndex(content); return lastImportIndex >= 0 ? (lines[lastImportIndex + 1] === "" ? lines.splice(lastImportIndex + 2, 0, codeToInsert, "") : lines.splice(lastImportIndex + 1, 0, "", codeToInsert, ""), lines.join(` `)) : codeToInsert + ` ` + content; } export { injectRouteHelpers }; //# sourceMappingURL=injectRouteHelpers.js.map