remix-flat-routes
Advanced tools
Comparing version 0.2.1 to 0.3.0
@@ -62,1 +62,2 @@ #!/usr/bin/env node | ||
} | ||
//# sourceMappingURL=cli.js.map |
@@ -25,3 +25,3 @@ var __create = Object.create; | ||
flatRoutes: () => flatRoutes, | ||
parseRouteFile: () => parseRouteFile | ||
getRouteInfo: () => getRouteInfo | ||
}); | ||
@@ -31,3 +31,3 @@ module.exports = __toCommonJS(src_exports); | ||
var import_util = require("./util"); | ||
function flatRoutes(baseDir, defineRoutes) { | ||
function flatRoutes(baseDir, defineRoutes, options = {}) { | ||
const routeMap = /* @__PURE__ */ new Map(); | ||
@@ -37,10 +37,13 @@ const parentMap = /* @__PURE__ */ new Map(); | ||
path: "", | ||
file: "root.tsx" | ||
file: "root.tsx", | ||
name: "root", | ||
parent: "", | ||
isIndex: false | ||
}); | ||
var routes = defineRoutes((route) => { | ||
(0, import_util.visitFiles)(`app/${baseDir}`, (routeFile) => { | ||
const parsed = parseRouteFile(baseDir, routeFile); | ||
if (!parsed) | ||
const routeInfo = getRouteInfo(baseDir, routeFile, options.basePath); | ||
if (!routeInfo) | ||
return; | ||
routeMap.set(parsed.name, parsed); | ||
routeMap.set(routeInfo.name, routeInfo); | ||
}); | ||
@@ -53,3 +56,3 @@ for (let [_name, route2] of routeMap) { | ||
parent = { | ||
parsed: routeMap.get(parentRoute), | ||
routeInfo: routeMap.get(parentRoute), | ||
children: [] | ||
@@ -70,15 +73,21 @@ }; | ||
if (parentRoute && parentRoute.children) { | ||
route(parentRoute.parsed.path, parentRoute.parsed.file, () => { | ||
const routeOptions = { | ||
caseSensitive: false, | ||
index: parentRoute.routeInfo.isIndex | ||
}; | ||
const routeChildren = () => { | ||
for (let child of parentRoute.children) { | ||
getRoutes(parentMap, child.name, route); | ||
route(child.path.substring(parentRoute.parsed.path.length + 1), child.file, { index: child.isIndex }); | ||
const path2 = child.path.substring(parentRoute.routeInfo.path.length + 1); | ||
route(path2, child.file, { index: child.isIndex }); | ||
} | ||
}, { index: parentRoute.parsed.isIndex }); | ||
}; | ||
route(parentRoute.routeInfo.path, parentRoute.routeInfo.file, routeOptions, routeChildren); | ||
} | ||
} | ||
function parseRouteFile(baseDir, routeFile) { | ||
function getRouteInfo(baseDir, routeFile, basePath) { | ||
let state = "START"; | ||
let subState = "NORMAL"; | ||
let parentState = "APPEND"; | ||
let url = ""; | ||
let url = basePath != null ? basePath : ""; | ||
let parent = ""; | ||
@@ -173,1 +182,2 @@ let isIndex = false; | ||
} | ||
//# sourceMappingURL=index.js.map |
@@ -76,1 +76,2 @@ var __create = Object.create; | ||
} | ||
//# sourceMappingURL=migrate.js.map |
@@ -28,3 +28,3 @@ var __create = Object.create; | ||
var path = __toESM(require("path")); | ||
function visitFiles(dir, visitor, baseDir = dir) { | ||
const visitFiles = (dir, visitor, baseDir = dir) => { | ||
for (let filename of fs.readdirSync(dir)) { | ||
@@ -39,2 +39,3 @@ let file = path.resolve(dir, filename); | ||
} | ||
} | ||
}; | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "remix-flat-routes", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Package for generating routes using flat convention", | ||
@@ -17,3 +17,3 @@ "main": "dist/index.js", | ||
"clean": "rimraf dist", | ||
"build": "npm run clean && esbuild --format=cjs --outdir=./dist ./src/*.ts", | ||
"build": "npm run clean && esbuild --format=cjs --sourcemap --outdir=./dist ./src/*.ts", | ||
"test": "jest", | ||
@@ -20,0 +20,0 @@ "contributors:add": "all-contributors add", |
@@ -43,3 +43,2 @@ # Remix Flat Routes | ||
Example: | ||
@@ -49,3 +48,3 @@ npx migrate-flat-routes ./app/routes ./app/flatroutes --convention=flat-folders | ||
NOTE: | ||
sourceDir and targetDir are relative to project roo | ||
sourceDir and targetDir are relative to project root | ||
@@ -52,0 +51,0 @@ Options: |
115
src/index.ts
import * as path from 'path' | ||
import { visitFiles } from './util' | ||
type RouteInfo = { | ||
path: string | ||
file: string | ||
name: string | ||
parent: string | ||
isIndex: boolean | ||
} | ||
type DefineRouteOptions = { | ||
caseSensitive?: boolean | ||
index?: boolean | ||
} | ||
type DefineRouteChildren = { | ||
(): void | ||
} | ||
type DefineRouteFunction = ( | ||
path: string | undefined, | ||
file: string, | ||
optionsOrChildren?: DefineRouteOptions | DefineRouteChildren, | ||
children?: DefineRouteChildren, | ||
) => void | ||
export type VisitFilesFunction = ( | ||
dir: string, | ||
visitor: (file: string) => void, | ||
baseDir?: string, | ||
) => void | ||
type FlatRoutesOptions = { | ||
basePath?: string | ||
} | ||
type ParentMapEntry = { | ||
routeInfo: RouteInfo | ||
children: RouteInfo[] | ||
} | ||
export type DefineRoutesFunction = ( | ||
callback: (route: DefineRouteFunction) => void, | ||
) => any | ||
export default function flatRoutes( | ||
baseDir: string, | ||
defineRoutes: (route: (...args: any) => void) => any, | ||
defineRoutes: DefineRoutesFunction, | ||
options: FlatRoutesOptions = {}, | ||
) { | ||
const routeMap = new Map() | ||
const parentMap = new Map() | ||
const routeMap = new Map<string, RouteInfo>() | ||
const parentMap = new Map<string, ParentMapEntry>() | ||
@@ -15,8 +59,11 @@ // initialize root route | ||
file: 'root.tsx', | ||
name: 'root', | ||
parent: '', | ||
isIndex: false, | ||
}) | ||
var routes = defineRoutes(route => { | ||
visitFiles(`app/${baseDir}`, routeFile => { | ||
const parsed = parseRouteFile(baseDir, routeFile) | ||
if (!parsed) return | ||
routeMap.set(parsed.name, parsed) | ||
const routeInfo = getRouteInfo(baseDir, routeFile, options.basePath) | ||
if (!routeInfo) return | ||
routeMap.set(routeInfo.name, routeInfo) | ||
}) | ||
@@ -30,3 +77,3 @@ // setup parent map | ||
parent = { | ||
parsed: routeMap.get(parentRoute), | ||
routeInfo: routeMap.get(parentRoute)!, | ||
children: [], | ||
@@ -48,22 +95,26 @@ } | ||
function getRoutes( | ||
parentMap: Map<string, any>, | ||
parentMap: Map<string, ParentMapEntry>, | ||
parent: string, | ||
route: (...args: any) => void, | ||
route: DefineRouteFunction, | ||
) { | ||
let parentRoute = parentMap.get(parent) | ||
if (parentRoute && parentRoute.children) { | ||
const routeOptions: DefineRouteOptions = { | ||
caseSensitive: false, | ||
index: parentRoute!.routeInfo.isIndex, | ||
} | ||
const routeChildren: DefineRouteChildren = () => { | ||
for (let child of parentRoute!.children) { | ||
getRoutes(parentMap, child.name, route) | ||
const path = child.path.substring( | ||
parentRoute!.routeInfo.path.length + 1, | ||
) | ||
route(path, child.file, { index: child.isIndex }) | ||
} | ||
} | ||
route( | ||
parentRoute.parsed.path, | ||
parentRoute.parsed.file, | ||
() => { | ||
for (let child of parentRoute.children) { | ||
getRoutes(parentMap, child.name, route) | ||
route( | ||
child.path.substring(parentRoute.parsed.path.length + 1), | ||
child.file, | ||
{ index: child.isIndex }, | ||
) | ||
} | ||
}, | ||
{ index: parentRoute.parsed.isIndex }, | ||
parentRoute.routeInfo.path, | ||
parentRoute.routeInfo.file, | ||
routeOptions, | ||
routeChildren, | ||
) | ||
@@ -73,13 +124,6 @@ } | ||
export type RouteInfo = { | ||
path: string | ||
file: string | ||
name: string | ||
parent: string | ||
isIndex: boolean | ||
} | ||
export function parseRouteFile( | ||
export function getRouteInfo( | ||
baseDir: string, | ||
routeFile: string, | ||
basePath?: string, | ||
): RouteInfo | null { | ||
@@ -89,3 +133,3 @@ let state = 'START' | ||
let parentState = 'APPEND' | ||
let url = '' | ||
let url = basePath ?? '' | ||
let parent = '' | ||
@@ -118,3 +162,2 @@ let isIndex = false | ||
let char = name[index] | ||
//console.log({ char, state, subState, segment, url }) | ||
switch (state) { | ||
@@ -196,1 +239,7 @@ case 'START': | ||
export { flatRoutes } | ||
export type { | ||
DefineRouteFunction, | ||
DefineRouteOptions, | ||
DefineRouteChildren, | ||
RouteInfo, | ||
} |
import * as fs from 'fs' | ||
import * as path from 'path' | ||
import type { VisitFilesFunction } from './index' | ||
export function visitFiles( | ||
export const visitFiles: VisitFilesFunction = ( | ||
dir: string, | ||
visitor: (file: string) => void, | ||
baseDir = dir, | ||
) { | ||
) => { | ||
for (let filename of fs.readdirSync(dir)) { | ||
@@ -10,0 +11,0 @@ let file = path.resolve(dir, filename) |
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
51123
15
699
232