remix-flat-routes
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -33,2 +33,3 @@ var __create = Object.create; | ||
const parentMap = /* @__PURE__ */ new Map(); | ||
const visitor = (options == null ? void 0 : options.visitFiles) || import_util.visitFiles; | ||
routeMap.set("root", { | ||
@@ -42,3 +43,3 @@ path: "", | ||
var routes = defineRoutes((route) => { | ||
(0, import_util.visitFiles)(`app/${baseDir}`, (routeFile) => { | ||
visitor(`app/${baseDir}`, (routeFile) => { | ||
const routeInfo = getRouteInfo(baseDir, routeFile, options.basePath); | ||
@@ -49,4 +50,6 @@ if (!routeInfo) | ||
}); | ||
for (let [_name, route2] of routeMap) { | ||
let parentRoute = route2.parent; | ||
for (let [name, route2] of routeMap) { | ||
if (name === "root") | ||
continue; | ||
let parentRoute = getParentRoute(routeMap, name); | ||
if (parentRoute) { | ||
@@ -66,5 +69,17 @@ let parent = parentMap.get(parentRoute); | ||
}); | ||
delete routes.root; | ||
if (routes) { | ||
delete routes.root; | ||
} | ||
return routes; | ||
} | ||
function getParentRoute(routeMap, name) { | ||
var parentName = name.substring(0, name.lastIndexOf(".")); | ||
if (parentName === "") { | ||
return "root"; | ||
} | ||
if (routeMap.has(parentName)) { | ||
return parentName; | ||
} | ||
return getParentRoute(routeMap, parentName); | ||
} | ||
function getRoutes(parentMap, parent, route) { | ||
@@ -88,8 +103,3 @@ let parentRoute = parentMap.get(parent); | ||
function getRouteInfo(baseDir, routeFile, basePath) { | ||
let state = "START"; | ||
let subState = "NORMAL"; | ||
let parentState = "APPEND"; | ||
let url = basePath != null ? basePath : ""; | ||
let parent = ""; | ||
let isIndex = false; | ||
let ext = path.extname(routeFile); | ||
@@ -99,60 +109,22 @@ if (![".js", ".jsx", ".ts", ".tsx", ".md", ".mdx"].includes(ext)) { | ||
} | ||
let name = routeFile.substring(0, routeFile.length - ext.length); | ||
if (routeFile.includes("/")) { | ||
if (!name.endsWith("/index") && !name.endsWith("/_layout") && !name.endsWith("/_route")) { | ||
let name = routeFile.substring(0, routeFile.length - ext.length).replace(path.win32.sep, "/"); | ||
if (name.includes("/")) { | ||
if (["/index", "/_index", "/_layout", "/_route", ".route"].every((suffix) => !name.endsWith(suffix))) { | ||
return null; | ||
} | ||
name = path.dirname(routeFile); | ||
isIndex = name.endsWith("/index") || name.endsWith(".index"); | ||
} | ||
let index = 0; | ||
let pathSegment = ""; | ||
let routeSegment = ""; | ||
while (index < name.length) { | ||
let char = name[index]; | ||
switch (state) { | ||
case "START": | ||
url = appendPathSegment(url, pathSegment); | ||
if (routeSegment.endsWith("_")) { | ||
parentState = "IGNORE"; | ||
} | ||
if (parentState === "APPEND") { | ||
if (parent) { | ||
parent += "."; | ||
} | ||
parent += routeSegment; | ||
} | ||
if (routeSegment === "index") | ||
isIndex = true; | ||
pathSegment = ""; | ||
routeSegment = ""; | ||
state = "PATH"; | ||
continue; | ||
case "PATH": | ||
if (isPathSeparator(char) && subState === "NORMAL") { | ||
state = "START"; | ||
break; | ||
} else if (char === "$") { | ||
pathSegment += ":"; | ||
} else if (char === "[") { | ||
subState = "ESCAPE"; | ||
} else if (char === "]") { | ||
subState = "NORMAL"; | ||
} else { | ||
pathSegment += char; | ||
} | ||
routeSegment += char; | ||
break; | ||
if (name.endsWith(".route")) { | ||
name = name.replace(/[\/\\]/g, ".").replace(/\.route$/, "/_index"); | ||
} | ||
index++; | ||
name = path.dirname(name); | ||
} | ||
if (routeSegment === "index") | ||
isIndex = true; | ||
url = appendPathSegment(url, pathSegment); | ||
let routeSegments = (0, import_util.getRouteSegments)(name); | ||
for (let i = 0; i < routeSegments.length; i++) { | ||
let routeSegment = routeSegments[i]; | ||
url = appendPathSegment(url, routeSegment); | ||
} | ||
return { | ||
path: url, | ||
file: `${baseDir}/${routeFile}`, | ||
file: path.join(baseDir, routeFile), | ||
name, | ||
parent: parent || "root", | ||
isIndex | ||
isIndex: routeSegments.at(-1) === "index" || routeSegments.at(-1) === "_index" | ||
}; | ||
@@ -164,14 +136,11 @@ } | ||
return url; | ||
} | ||
if (segment === "index") { | ||
} else if (["index", "_index"].some((name) => segment === name)) { | ||
if (!url.endsWith("/")) { | ||
url += "/"; | ||
} | ||
} else if (segment === ":" || segment === ":_") { | ||
url += "/*"; | ||
} else if (segment !== "route") { | ||
if (segment.endsWith("_")) { | ||
segment = segment.slice(0, -1); | ||
} | ||
} else if (segment.startsWith("$")) { | ||
segment = segment === "$" ? "*" : `:${segment.substring(1)}`; | ||
url += "/" + segment; | ||
} else { | ||
url += "/" + segment; | ||
} | ||
@@ -181,5 +150,2 @@ } | ||
} | ||
function isPathSeparator(char) { | ||
return char === "/" || char === path.win32.sep || char === "."; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -42,7 +42,7 @@ var __create = Object.create; | ||
const route = convertToRoute(name); | ||
const targetFile = `${targetDir}/${route}${extension}`; | ||
fs.cpSync(`${sourceDir}/${file}`, targetFile, { force: true }); | ||
const targetFile = path.join(targetDir, `${route}${extension}`); | ||
fs.cpSync(path.join(sourceDir, file), targetFile, { force: true }); | ||
}; | ||
} | ||
const routeExtensions = [".js", ".jsx", ".ts", ".tsx"]; | ||
const routeExtensions = [".js", ".jsx", ".ts", ".tsx", ".md", ".mdx"]; | ||
function flatFolders(sourceDir, targetDir) { | ||
@@ -54,3 +54,3 @@ return (file) => { | ||
const route = convertToRoute(name); | ||
const targetFolder = `${targetDir}/${route}`; | ||
const targetFolder = path.join(targetDir, route); | ||
if (!routeExtensions.includes(extension)) { | ||
@@ -60,3 +60,3 @@ return; | ||
fs.mkdirSync(targetFolder, { recursive: true }); | ||
fs.cpSync(`${sourceDir}/${file}`, `${targetFolder}/index${extension}`, { | ||
fs.cpSync(path.join(sourceDir, file), path.join(targetFolder, `/_index${extension}`), { | ||
force: true | ||
@@ -66,15 +66,12 @@ }); | ||
} | ||
function convertToRoute(file) { | ||
const pathSegments = file.split("/"); | ||
const parent = pathSegments.length == 1 ? "" : pathSegments.slice(0, -1).join("/"); | ||
const route = pathSegments.slice(-1)[0]; | ||
const routeSegments = route.split("."); | ||
if (routeSegments.length > 1) { | ||
routeSegments[0] = `${routeSegments[0]}_`; | ||
} | ||
return `${getFlatRoute(parent.split("/"))}${parent === "" ? "" : "."}${getFlatRoute(routeSegments)}`; | ||
function convertToRoute(name) { | ||
const pathSegments = name.split(path.sep); | ||
return pathSegments.map((pathSegment) => { | ||
const routeSegments = (0, import_util.getRouteSegments)(pathSegment); | ||
return getFlatRoute(routeSegments); | ||
}).join("."); | ||
} | ||
function getFlatRoute(segments) { | ||
return segments.map((segment) => segment.startsWith("__") ? segment.substring(1) : segment).join("."); | ||
return segments.map((segment) => segment.startsWith("__") ? segment.substring(1) : segment === "index" ? "_index" : segment).join("."); | ||
} | ||
//# sourceMappingURL=migrate.js.map |
@@ -23,2 +23,3 @@ var __create = Object.create; | ||
__export(util_exports, { | ||
getRouteSegments: () => getRouteSegments, | ||
visitFiles: () => visitFiles | ||
@@ -29,2 +30,43 @@ }); | ||
var path = __toESM(require("path")); | ||
function getRouteSegments(name) { | ||
let routeSegments = []; | ||
let index = 0; | ||
let routeSegment = ""; | ||
let state = "START"; | ||
let subState = "NORMAL"; | ||
const pushRouteSegment = (routeSegment2) => { | ||
if (routeSegment2) { | ||
routeSegments.push(routeSegment2); | ||
} | ||
}; | ||
while (index < name.length) { | ||
let char = name[index]; | ||
switch (state) { | ||
case "START": | ||
pushRouteSegment(routeSegment); | ||
routeSegment = ""; | ||
state = "PATH"; | ||
continue; | ||
case "PATH": | ||
if (isPathSeparator(char) && subState === "NORMAL") { | ||
state = "START"; | ||
break; | ||
} else if (char === "[") { | ||
subState = "ESCAPE"; | ||
break; | ||
} else if (char === "]") { | ||
subState = "NORMAL"; | ||
break; | ||
} | ||
routeSegment += char; | ||
break; | ||
} | ||
index++; | ||
} | ||
pushRouteSegment(routeSegment); | ||
return routeSegments; | ||
} | ||
function isPathSeparator(char) { | ||
return char === "/" || char === path.win32.sep || char === "."; | ||
} | ||
const visitFiles = (dir, visitor, baseDir = dir) => { | ||
@@ -31,0 +73,0 @@ for (let filename of fs.readdirSync(dir)) { |
{ | ||
"name": "remix-flat-routes", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Package for generating routes using flat convention", | ||
@@ -41,2 +41,3 @@ "main": "dist/index.js", | ||
"@babel/preset-typescript": "^7.16.0", | ||
"@remix-run/dev": "^1.5.1", | ||
"@types/jest": "^27.0.3", | ||
@@ -46,4 +47,4 @@ "@types/node": "^17.0.21", | ||
"babel-jest": "^27.4.2", | ||
"esbuild": "^0.14.36", | ||
"esbuild-register": "^3.3.2", | ||
"esbuild": "^0.14.36", | ||
"formdata-polyfill": "^4.0.10", | ||
@@ -50,0 +51,0 @@ "jest": "^27.4.3", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26306
358
18