UNPKG

one

Version:

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

162 lines 5.46 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = mod => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var injectRouteHelpers_exports = {}; __export(injectRouteHelpers_exports, { injectRouteHelpers: () => injectRouteHelpers }); module.exports = __toCommonJS(injectRouteHelpers_exports); var import_node_fs = require("node:fs"); var import_promises = require("node:fs/promises"); async function injectRouteHelpers(filePath, routePath, mode) { if (!(0, import_node_fs.existsSync)(filePath)) { return false; } try { let content = await (0, import_promises.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 (0, import_promises.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; }