@tanstack/router-cli 0.0.1-beta.189 → 0.0.1-beta.191
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/generator.js +44 -15
- package/build/cjs/generator.js.map +1 -1
- package/package.json +1 -1
- package/src/generator.ts +51 -22
package/build/cjs/generator.js
CHANGED
|
@@ -71,15 +71,15 @@ async function getRouteNodes(config) {
|
|
|
71
71
|
} else {
|
|
72
72
|
const filePath = path__default["default"].join(dir, fileName);
|
|
73
73
|
const filePathNoExt = removeExt(filePath);
|
|
74
|
-
let routePath = routerCore.cleanPath(`/${filePathNoExt.split('.').join('/')}`);
|
|
74
|
+
let routePath = replaceBackslash(routerCore.cleanPath(`/${filePathNoExt.split('.').join('/')}`)) ?? '';
|
|
75
75
|
const variableName = fileToVariable(routePath);
|
|
76
76
|
|
|
77
77
|
// Remove the index from the route path and
|
|
78
78
|
// if the route path is empty, use `/'
|
|
79
|
-
if (routePath
|
|
80
|
-
routePath = routePath.replace(/\/index$/, '/');
|
|
81
|
-
} else if (routePath === 'index') {
|
|
79
|
+
if (routePath === 'index') {
|
|
82
80
|
routePath = '/';
|
|
81
|
+
} else if (routePath.endsWith('/index')) {
|
|
82
|
+
routePath = routePath.replace(/\/index$/, '/');
|
|
83
83
|
}
|
|
84
84
|
routeNodes.push({
|
|
85
85
|
filePath,
|
|
@@ -123,13 +123,16 @@ async function generator(config) {
|
|
|
123
123
|
// Loop over the flat list of routeNodes and
|
|
124
124
|
// build up a tree based on the routeNodes' routePath
|
|
125
125
|
routeNodes.forEach(node => {
|
|
126
|
-
routeNodes.forEach(existingNode => {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}
|
|
126
|
+
// routeNodes.forEach((existingNode) => {
|
|
127
|
+
// if (
|
|
128
|
+
// node.routePath?.startsWith(`${existingNode?.routePath ?? ''}/`)
|
|
129
|
+
// // node.routePath.length > existingNode.routePath!.length
|
|
130
|
+
// ) {
|
|
131
|
+
// node.parent = existingNode
|
|
132
|
+
// }
|
|
133
|
+
// })
|
|
134
|
+
const parentRoute = hasParentRoute(routeNodes, node.routePath);
|
|
135
|
+
if (parentRoute) node.parent = parentRoute;
|
|
133
136
|
node.path = node.parent ? node.routePath?.replace(node.parent.routePath, '') || '/' : node.routePath;
|
|
134
137
|
const trimmedPath = routerCore.trimPathLeft(node.path ?? '');
|
|
135
138
|
const split = trimmedPath?.split('/') ?? [];
|
|
@@ -154,7 +157,12 @@ async function generator(config) {
|
|
|
154
157
|
}
|
|
155
158
|
|
|
156
159
|
// Ensure that new FileRoute(anything?) is replace with FileRoute(${node.routePath})
|
|
157
|
-
|
|
160
|
+
// routePath can contain $ characters, which have special meaning when used in replace
|
|
161
|
+
// so we have to escape it by turning all $ into $$. But since we do it through a replace call
|
|
162
|
+
// we have to double escape it into $$$$. For more information, see
|
|
163
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement
|
|
164
|
+
const escapedRoutePath = node.routePath?.replaceAll('$', '$$$$') ?? '';
|
|
165
|
+
const replaced = routeCode.replace(fileRouteRegex, `new FileRoute('${escapedRoutePath}')`);
|
|
158
166
|
if (replaced !== routeCode) {
|
|
159
167
|
await fs__default["default"].writeFile(node.fullPath, replaced);
|
|
160
168
|
}
|
|
@@ -168,8 +176,8 @@ async function generator(config) {
|
|
|
168
176
|
return (await Promise.all(children)).filter(Boolean).join(`,`);
|
|
169
177
|
}
|
|
170
178
|
const routeConfigChildrenText = await buildRouteConfig(routeTree);
|
|
171
|
-
const routeImports = [`import { route as rootRoute } from './${path__default["default"].relative(path__default["default"].dirname(config.generatedRouteTree), path__default["default"].resolve(config.routesDirectory, rootPathId))}'`, ...multiSortBy(routeNodes, [d => d.routePath?.includes(`/${rootPathId}`) ? -1 : 1, d => d.routePath?.split('/').length, d => d.routePath?.endsWith("index'") ? -1 : 1, d => d]).map(node => {
|
|
172
|
-
return `import { route as ${node.variableName}Route } from './${removeExt(path__default["default"].relative(path__default["default"].dirname(config.generatedRouteTree), path__default["default"].resolve(config.routesDirectory, node.filePath)))}'`;
|
|
179
|
+
const routeImports = [`import { route as rootRoute } from './${sanitize(path__default["default"].relative(path__default["default"].dirname(config.generatedRouteTree), path__default["default"].resolve(config.routesDirectory, rootPathId)))}'`, ...multiSortBy(routeNodes, [d => d.routePath?.includes(`/${rootPathId}`) ? -1 : 1, d => d.routePath?.split('/').length, d => d.routePath?.endsWith("index'") ? -1 : 1, d => d]).map(node => {
|
|
180
|
+
return `import { route as ${node.variableName}Route } from './${sanitize(removeExt(path__default["default"].relative(path__default["default"].dirname(config.generatedRouteTree), path__default["default"].resolve(config.routesDirectory, node.filePath))))}'`;
|
|
173
181
|
})].join('\n');
|
|
174
182
|
const routeTypes = `declare module '@tanstack/react-router' {
|
|
175
183
|
interface FileRoutesByPath {
|
|
@@ -177,7 +185,7 @@ async function generator(config) {
|
|
|
177
185
|
return `'${routeNode.routePath}': {
|
|
178
186
|
parentRoute: typeof ${routeNode.parent?.variableName ?? 'root'}Route
|
|
179
187
|
}`;
|
|
180
|
-
}).join('\n')}
|
|
188
|
+
}).join('\n')}
|
|
181
189
|
}
|
|
182
190
|
}`;
|
|
183
191
|
const routeOptions = routeNodes.map(routeNode => {
|
|
@@ -252,9 +260,30 @@ function capitalize(s) {
|
|
|
252
260
|
if (typeof s !== 'string') return '';
|
|
253
261
|
return s.charAt(0).toUpperCase() + s.slice(1);
|
|
254
262
|
}
|
|
263
|
+
function sanitize(s) {
|
|
264
|
+
return replaceBackslash(s?.replace(/\\index/gi, ''));
|
|
265
|
+
}
|
|
255
266
|
function removeUnderscores(s) {
|
|
256
267
|
return s?.replace(/(^_|_$)/, '').replace(/(\/_|_\/)/, '/');
|
|
257
268
|
}
|
|
269
|
+
function replaceBackslash(s) {
|
|
270
|
+
return s?.replace(/\\/gi, '/');
|
|
271
|
+
}
|
|
272
|
+
function hasParentRoute(routes, routeToCheck) {
|
|
273
|
+
if (!routeToCheck || routeToCheck === "/") {
|
|
274
|
+
return null;
|
|
275
|
+
}
|
|
276
|
+
for (const route of routes) {
|
|
277
|
+
if (route.routePath === '/') continue;
|
|
278
|
+
if (routeToCheck.startsWith(`${route.routePath}/`) && route.routePath !== routeToCheck) {
|
|
279
|
+
return route;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
const segments = routeToCheck.split("/");
|
|
283
|
+
segments.pop(); // Remove the last segment
|
|
284
|
+
const parentRoute = segments.join("/");
|
|
285
|
+
return hasParentRoute(routes, parentRoute);
|
|
286
|
+
}
|
|
258
287
|
|
|
259
288
|
exports.fileRouteRegex = fileRouteRegex;
|
|
260
289
|
exports.generator = generator;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"generator.js","sources":["../../src/generator.ts"],"sourcesContent":["import path from 'path'\nimport fs from 'fs-extra'\nimport * as prettier from 'prettier'\nimport { Config } from './config'\nimport { cleanPath, trimPathLeft } from '@tanstack/router-core'\n\nlet latestTask = 0\nexport const rootPathId = '__root'\nexport const fileRouteRegex = /new\\s+FileRoute\\(([^)]*)\\)/g\n\nexport type RouteNode = {\n filePath: string\n fullPath: string\n variableName: string\n routePath?: string\n cleanedPath?: string\n path?: string\n isNonPath?: boolean\n isNonLayout?: boolean\n isRoot?: boolean\n children?: RouteNode[]\n parent?: RouteNode\n}\n\nasync function getRouteNodes(config: Config) {\n const { routeFilePrefix, routeFileIgnorePrefix } = config\n\n let routeNodes: RouteNode[] = []\n\n async function recurse(dir: string) {\n const fullDir = path.resolve(config.routesDirectory, dir)\n let dirList = await fs.readdir(fullDir)\n\n dirList = dirList.filter((d) => {\n if (\n d.startsWith('.') ||\n (routeFileIgnorePrefix && d.startsWith(routeFileIgnorePrefix))\n ) {\n return false\n }\n\n if (routeFilePrefix) {\n return d.startsWith(routeFilePrefix)\n }\n\n return true\n })\n\n await Promise.all(\n dirList.map(async (fileName) => {\n const fullPath = path.join(fullDir, fileName)\n const relativePath = path.join(dir, fileName)\n const stat = await fs.stat(fullPath)\n\n if (stat.isDirectory()) {\n await recurse(relativePath)\n } else {\n const filePath = path.join(dir, fileName)\n const filePathNoExt = removeExt(filePath)\n let routePath = cleanPath(`/${filePathNoExt.split('.').join('/')}`)\n const variableName = fileToVariable(routePath)\n\n // Remove the index from the route path and\n // if the route path is empty, use `/'\n if (routePath.endsWith('/index')) {\n routePath = routePath.replace(/\\/index$/, '/')\n } else if (routePath === 'index') {\n routePath = '/'\n }\n\n routeNodes.push({\n filePath,\n fullPath,\n routePath,\n variableName,\n })\n }\n }),\n )\n\n return routeNodes\n }\n\n await recurse('./')\n\n return routeNodes\n}\n\nlet first = false\nlet skipMessage = false\n\nexport async function generator(config: Config) {\n console.log()\n\n if (!first) {\n console.log('🔄 Generating routes...')\n first = true\n } else if (skipMessage) {\n skipMessage = false\n } else {\n console.log('♻️ Regenerating routes...')\n }\n\n const taskId = latestTask + 1\n latestTask = taskId\n\n const checkLatest = () => {\n if (latestTask !== taskId) {\n skipMessage = true\n return false\n }\n\n return true\n }\n\n const start = Date.now()\n\n let routeNodes = await getRouteNodes(config)\n\n routeNodes = multiSortBy(routeNodes, [\n (d) => (d.routePath === '/' ? -1 : 1),\n (d) => d.routePath?.split('/').length,\n (d) => (d.routePath?.endsWith('/') ? -1 : 1),\n (d) => d.routePath,\n ]).filter((d) => d.routePath !== `/${rootPathId}`)\n\n const routeTree: RouteNode[] = []\n\n // Loop over the flat list of routeNodes and\n // build up a tree based on the routeNodes' routePath\n routeNodes.forEach((node) => {\n routeNodes.forEach((existingNode) => {\n if (\n node.routePath?.startsWith(`${existingNode?.routePath ?? ''}/`)\n // node.routePath.length > existingNode.routePath!.length\n ) {\n node.parent = existingNode\n }\n })\n\n node.path = node.parent\n ? node.routePath?.replace(node.parent.routePath!, '') || '/'\n : node.routePath\n\n const trimmedPath = trimPathLeft(node.path ?? '')\n\n const split = trimmedPath?.split('/') ?? []\n let first = split[0] ?? trimmedPath ?? ''\n\n node.isNonPath = first.startsWith('_')\n node.isNonLayout = first.endsWith('_')\n\n node.cleanedPath = removeUnderscores(node.path) ?? ''\n\n if (node.parent) {\n node.parent.children = node.parent.children ?? []\n node.parent.children.push(node)\n } else {\n routeTree.push(node)\n }\n })\n\n async function buildRouteConfig(\n nodes: RouteNode[],\n depth = 1,\n ): Promise<string> {\n const children = nodes.map(async (node) => {\n const routeCode = await fs.readFile(node.fullPath, 'utf-8')\n\n // Ensure the boilerplate for the route exists\n if (node.isRoot) {\n return\n }\n\n // Ensure that new FileRoute(anything?) is replace with FileRoute(${node.routePath})\n const replaced = routeCode.replace(\n fileRouteRegex,\n `new FileRoute('${node.routePath}')`,\n )\n\n if (replaced !== routeCode) {\n await fs.writeFile(node.fullPath, replaced)\n }\n\n const route = `${node.variableName}Route`\n\n if (node.children?.length) {\n const childConfigs = await buildRouteConfig(node.children, depth + 1)\n return `${route}.addChildren([${spaces(depth * 4)}${childConfigs}])`\n }\n\n return route\n })\n\n return (await Promise.all(children)).filter(Boolean).join(`,`)\n }\n\n const routeConfigChildrenText = await buildRouteConfig(routeTree)\n\n const routeImports = [\n `import { route as rootRoute } from './${path.relative(\n path.dirname(config.generatedRouteTree),\n path.resolve(config.routesDirectory, rootPathId),\n )}'`,\n ...multiSortBy(routeNodes, [\n (d) => (d.routePath?.includes(`/${rootPathId}`) ? -1 : 1),\n (d) => d.routePath?.split('/').length,\n (d) => (d.routePath?.endsWith(\"index'\") ? -1 : 1),\n (d) => d,\n ]).map((node) => {\n return `import { route as ${node.variableName}Route } from './${removeExt(\n path.relative(\n path.dirname(config.generatedRouteTree),\n path.resolve(config.routesDirectory, node.filePath),\n ),\n )}'`\n }),\n ].join('\\n')\n\n const routeTypes = `declare module '@tanstack/react-router' {\n interface FileRoutesByPath {\n ${routeNodes\n .map((routeNode) => {\n return `'${routeNode.routePath}': {\n parentRoute: typeof ${routeNode.parent?.variableName ?? 'root'}Route\n }`\n })\n .join('\\n')} \n }\n}`\n\n const routeOptions = routeNodes\n .map((routeNode) => {\n return `Object.assign(${routeNode.variableName ?? 'root'}Route.options, {\n ${[\n routeNode.isNonPath\n ? `id: '${routeNode.cleanedPath}'`\n : `path: '${routeNode.cleanedPath}'`,\n `getParentRoute: () => ${\n routeNode.parent?.variableName ?? 'root'\n }Route`,\n // `\\n// ${JSON.stringify(\n // {\n // ...routeNode,\n // parent: undefined,\n // children: undefined,\n // fullPath: undefined,\n // variableName: undefined,\n // },\n // null,\n // 2,\n // )\n // .split('\\n')\n // .join('\\n// ')}`,\n ]\n .filter(Boolean)\n .join(',')}\n })`\n })\n .join('\\n\\n')\n\n const routeConfig = `export const routeTree = rootRoute.addChildren([${routeConfigChildrenText}])`\n\n const routeConfigFileContent = await prettier.format(\n [routeImports, routeTypes, routeOptions, routeConfig].join('\\n\\n'),\n {\n semi: false,\n parser: 'typescript',\n },\n )\n\n const routeTreeContent = await fs\n .readFile(path.resolve(config.generatedRouteTree), 'utf-8')\n .catch((err: any) => {\n if (err.code === 'ENOENT') {\n return undefined\n }\n throw err\n })\n\n if (!checkLatest()) return\n\n if (routeTreeContent !== routeConfigFileContent) {\n await fs.ensureDir(path.dirname(path.resolve(config.generatedRouteTree)))\n if (!checkLatest()) return\n await fs.writeFile(\n path.resolve(config.generatedRouteTree),\n routeConfigFileContent,\n )\n }\n\n console.log(\n `🌲 Processed ${routeNodes.length} routes in ${Date.now() - start}ms`,\n )\n}\n\nfunction fileToVariable(d: string): string {\n return (\n removeUnderscores(d)\n ?.replace(/\\$/g, '')\n ?.split(/[/-]/g)\n .map((d, i) => (i > 0 ? capitalize(d) : d))\n .join('')\n .replace(/([^a-zA-Z0-9]|[\\.])/gm, '') ?? ''\n )\n}\n\nexport function removeExt(d: string) {\n return d.substring(0, d.lastIndexOf('.')) || d\n}\n\nfunction spaces(d: number): string {\n return Array.from({ length: d })\n .map(() => ' ')\n .join('')\n}\n\nexport function multiSortBy<T>(\n arr: T[],\n accessors: ((item: T) => any)[] = [(d) => d],\n): T[] {\n return arr\n .map((d, i) => [d, i] as const)\n .sort(([a, ai], [b, bi]) => {\n for (const accessor of accessors) {\n const ao = accessor(a)\n const bo = accessor(b)\n\n if (typeof ao === 'undefined') {\n if (typeof bo === 'undefined') {\n continue\n }\n return 1\n }\n\n if (ao === bo) {\n continue\n }\n\n return ao > bo ? 1 : -1\n }\n\n return ai - bi\n })\n .map(([d]) => d)\n}\n\nfunction capitalize(s: string) {\n if (typeof s !== 'string') return ''\n return s.charAt(0).toUpperCase() + s.slice(1)\n}\n\nfunction removeUnderscores(s?: string) {\n return s?.replace(/(^_|_$)/, '').replace(/(\\/_|_\\/)/, '/')\n}\n"],"names":["latestTask","rootPathId","fileRouteRegex","getRouteNodes","config","routeFilePrefix","routeFileIgnorePrefix","routeNodes","recurse","dir","fullDir","path","resolve","routesDirectory","dirList","fs","readdir","filter","d","startsWith","Promise","all","map","fileName","fullPath","join","relativePath","stat","isDirectory","filePath","filePathNoExt","removeExt","routePath","cleanPath","split","variableName","fileToVariable","endsWith","replace","push","first","skipMessage","generator","console","log","taskId","checkLatest","start","Date","now","multiSortBy","length","routeTree","forEach","node","existingNode","parent","trimmedPath","trimPathLeft","isNonPath","isNonLayout","cleanedPath","removeUnderscores","children","buildRouteConfig","nodes","depth","routeCode","readFile","isRoot","replaced","writeFile","route","childConfigs","spaces","Boolean","routeConfigChildrenText","routeImports","relative","dirname","generatedRouteTree","includes","routeTypes","routeNode","routeOptions","routeConfig","routeConfigFileContent","prettier","format","semi","parser","routeTreeContent","catch","err","code","undefined","ensureDir","i","capitalize","substring","lastIndexOf","Array","from","arr","accessors","sort","a","ai","b","bi","accessor","ao","bo","s","charAt","toUpperCase","slice"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,IAAIA,UAAU,GAAG,CAAC,CAAA;AACX,MAAMC,UAAU,GAAG,SAAQ;AAC3B,MAAMC,cAAc,GAAG,8BAA6B;AAgB3D,eAA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AAC,KAAKA,CAAC,CAAC,CAAA;AACpB,CAAA;AAEA,SAASgF,UAAUA,CAACe,CAAS,EAAE;AAC7B,EAAA,IAAI,OAAOA,CAAC,KAAK,QAAQ,EAAE,OAAO,EAAE,CAAA;AACpC,EAAA,OAAOA,CAAC,CAACC,MAAM,CAAC,CAAC,CAAC,CAACC,WAAW,EAAE,GAAGF,CAAC,CAACG,KAAK,CAAC,CAAC,CAAC,CAAA;AAC/C,CAAA;AAEA,SAAStD,iBAAiBA,CAACmD,CAAU,EAAE;AACrC,EAAA,OAAOA,CAAC,EAAE3E,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAACA,OAAO,CAAC,WAAW,EAAE,GAAG,CAAC,CAAA;AAC5D;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"generator.js","sources":["../../src/generator.ts"],"sourcesContent":["import path from 'path'\nimport fs from 'fs-extra'\nimport * as prettier from 'prettier'\nimport { Config } from './config'\nimport { cleanPath, trimPathLeft } from '@tanstack/router-core'\n\nlet latestTask = 0\nexport const rootPathId = '__root'\nexport const fileRouteRegex = /new\\s+FileRoute\\(([^)]*)\\)/g\n\nexport type RouteNode = {\n filePath: string\n fullPath: string\n variableName: string\n routePath?: string\n cleanedPath?: string\n path?: string\n isNonPath?: boolean\n isNonLayout?: boolean\n isRoot?: boolean\n children?: RouteNode[]\n parent?: RouteNode\n}\n\nasync function getRouteNodes(config: Config) {\n const { routeFilePrefix, routeFileIgnorePrefix } = config\n\n let routeNodes: RouteNode[] = []\n\n async function recurse(dir: string) {\n const fullDir = path.resolve(config.routesDirectory, dir)\n let dirList = await fs.readdir(fullDir)\n\n dirList = dirList.filter((d) => {\n if (\n d.startsWith('.') ||\n (routeFileIgnorePrefix && d.startsWith(routeFileIgnorePrefix))\n ) {\n return false\n }\n\n if (routeFilePrefix) {\n return d.startsWith(routeFilePrefix)\n }\n\n return true\n })\n\n await Promise.all(\n dirList.map(async (fileName) => {\n const fullPath = path.join(fullDir, fileName)\n const relativePath = path.join(dir, fileName)\n const stat = await fs.stat(fullPath)\n\n if (stat.isDirectory()) {\n await recurse(relativePath)\n } else {\n const filePath = path.join(dir, fileName)\n const filePathNoExt = removeExt(filePath)\n let routePath = replaceBackslash(cleanPath(`/${filePathNoExt.split('.').join('/')}`)) ?? ''\n const variableName = fileToVariable(routePath)\n\n // Remove the index from the route path and\n // if the route path is empty, use `/'\n if (routePath === 'index') {\n routePath = '/'\n } else if (routePath.endsWith('/index')) {\n routePath = routePath.replace(/\\/index$/, '/')\n }\n\n routeNodes.push({\n filePath,\n fullPath,\n routePath,\n variableName,\n })\n }\n }),\n )\n\n return routeNodes\n }\n\n await recurse('./')\n\n return routeNodes\n}\n\nlet first = false\nlet skipMessage = false\n\nexport async function generator(config: Config) {\n console.log()\n\n if (!first) {\n console.log('🔄 Generating routes...')\n first = true\n } else if (skipMessage) {\n skipMessage = false\n } else {\n console.log('♻️ Regenerating routes...')\n }\n\n const taskId = latestTask + 1\n latestTask = taskId\n\n const checkLatest = () => {\n if (latestTask !== taskId) {\n skipMessage = true\n return false\n }\n\n return true\n }\n\n const start = Date.now()\n\n let routeNodes = await getRouteNodes(config)\n\n routeNodes = multiSortBy(routeNodes, [\n (d) => (d.routePath === '/' ? -1 : 1),\n (d) => d.routePath?.split('/').length,\n (d) => (d.routePath?.endsWith('/') ? -1 : 1),\n (d) => d.routePath,\n ]).filter((d) => d.routePath !== `/${rootPathId}`)\n\n const routeTree: RouteNode[] = []\n\n // Loop over the flat list of routeNodes and\n // build up a tree based on the routeNodes' routePath\n routeNodes.forEach((node) => {\n // routeNodes.forEach((existingNode) => {\n // if (\n // node.routePath?.startsWith(`${existingNode?.routePath ?? ''}/`)\n // // node.routePath.length > existingNode.routePath!.length\n // ) {\n // node.parent = existingNode\n // }\n // })\n const parentRoute = hasParentRoute(routeNodes, node.routePath)\n if (parentRoute) node.parent = parentRoute\n\n node.path = node.parent\n ? node.routePath?.replace(node.parent.routePath!, '') || '/'\n : node.routePath\n\n const trimmedPath = trimPathLeft(node.path ?? '')\n\n const split = trimmedPath?.split('/') ?? []\n let first = split[0] ?? trimmedPath ?? ''\n\n node.isNonPath = first.startsWith('_')\n node.isNonLayout = first.endsWith('_')\n\n node.cleanedPath = removeUnderscores(node.path) ?? ''\n\n if (node.parent) {\n node.parent.children = node.parent.children ?? []\n node.parent.children.push(node)\n } else {\n routeTree.push(node)\n }\n })\n\n async function buildRouteConfig(\n nodes: RouteNode[],\n depth = 1,\n ): Promise<string> {\n const children = nodes.map(async (node) => {\n const routeCode = await fs.readFile(node.fullPath, 'utf-8')\n\n // Ensure the boilerplate for the route exists\n if (node.isRoot) {\n return\n }\n\n // Ensure that new FileRoute(anything?) is replace with FileRoute(${node.routePath})\n // routePath can contain $ characters, which have special meaning when used in replace\n // so we have to escape it by turning all $ into $$. But since we do it through a replace call\n // we have to double escape it into $$$$. For more information, see\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement\n const escapedRoutePath = node.routePath?.replaceAll('$', '$$$$') ?? ''\n const replaced = routeCode.replace(\n fileRouteRegex,\n `new FileRoute('${escapedRoutePath}')`,\n )\n\n if (replaced !== routeCode) {\n await fs.writeFile(node.fullPath, replaced)\n }\n\n const route = `${node.variableName}Route`\n\n if (node.children?.length) {\n const childConfigs = await buildRouteConfig(node.children, depth + 1)\n return `${route}.addChildren([${spaces(depth * 4)}${childConfigs}])`\n }\n\n return route\n })\n\n return (await Promise.all(children)).filter(Boolean).join(`,`)\n }\n\n const routeConfigChildrenText = await buildRouteConfig(routeTree)\n\n const routeImports = [\n `import { route as rootRoute } from './${sanitize(path.relative(\n path.dirname(config.generatedRouteTree),\n path.resolve(config.routesDirectory, rootPathId)))}'`,\n ...multiSortBy(routeNodes, [\n (d) => (d.routePath?.includes(`/${rootPathId}`) ? -1 : 1),\n (d) => d.routePath?.split('/').length,\n (d) => (d.routePath?.endsWith(\"index'\") ? -1 : 1),\n (d) => d,\n ]).map((node) => {\n return `import { route as ${node.variableName}Route } from './${sanitize(removeExt(\n path.relative(\n path.dirname(config.generatedRouteTree),\n path.resolve(config.routesDirectory, node.filePath),\n ),\n ))}'`\n }),\n ].join('\\n')\n\n const routeTypes = `declare module '@tanstack/react-router' {\n interface FileRoutesByPath {\n ${routeNodes\n .map((routeNode) => {\n return `'${routeNode.routePath}': {\n parentRoute: typeof ${routeNode.parent?.variableName ?? 'root'}Route\n }`\n })\n .join('\\n')}\n }\n}`\n\n const routeOptions = routeNodes\n .map((routeNode) => {\n return `Object.assign(${routeNode.variableName ?? 'root'}Route.options, {\n ${[\n routeNode.isNonPath\n ? `id: '${routeNode.cleanedPath}'`\n : `path: '${routeNode.cleanedPath}'`,\n `getParentRoute: () => ${routeNode.parent?.variableName ?? 'root'\n }Route`,\n // `\\n// ${JSON.stringify(\n // {\n // ...routeNode,\n // parent: undefined,\n // children: undefined,\n // fullPath: undefined,\n // variableName: undefined,\n // },\n // null,\n // 2,\n // )\n // .split('\\n')\n // .join('\\n// ')}`,\n ]\n .filter(Boolean)\n .join(',')}\n })`\n })\n .join('\\n\\n')\n\n const routeConfig = `export const routeTree = rootRoute.addChildren([${routeConfigChildrenText}])`\n\n const routeConfigFileContent = await prettier.format(\n [routeImports, routeTypes, routeOptions, routeConfig].join('\\n\\n'),\n {\n semi: false,\n parser: 'typescript',\n },\n )\n\n const routeTreeContent = await fs\n .readFile(path.resolve(config.generatedRouteTree), 'utf-8')\n .catch((err: any) => {\n if (err.code === 'ENOENT') {\n return undefined\n }\n throw err\n })\n\n if (!checkLatest()) return\n\n if (routeTreeContent !== routeConfigFileContent) {\n await fs.ensureDir(path.dirname(path.resolve(config.generatedRouteTree)))\n if (!checkLatest()) return\n await fs.writeFile(\n path.resolve(config.generatedRouteTree),\n routeConfigFileContent,\n )\n }\n\n console.log(\n `🌲 Processed ${routeNodes.length} routes in ${Date.now() - start}ms`,\n )\n}\n\nfunction fileToVariable(d: string): string {\n return (\n removeUnderscores(d)\n ?.replace(/\\$/g, '')\n ?.split(/[/-]/g)\n .map((d, i) => (i > 0 ? capitalize(d) : d))\n .join('')\n .replace(/([^a-zA-Z0-9]|[\\.])/gm, '') ?? ''\n )\n}\n\nexport function removeExt(d: string) {\n return d.substring(0, d.lastIndexOf('.')) || d\n}\n\nfunction spaces(d: number): string {\n return Array.from({ length: d })\n .map(() => ' ')\n .join('')\n}\n\nexport function multiSortBy<T>(\n arr: T[],\n accessors: ((item: T) => any)[] = [(d) => d],\n): T[] {\n return arr\n .map((d, i) => [d, i] as const)\n .sort(([a, ai], [b, bi]) => {\n for (const accessor of accessors) {\n const ao = accessor(a)\n const bo = accessor(b)\n\n if (typeof ao === 'undefined') {\n if (typeof bo === 'undefined') {\n continue\n }\n return 1\n }\n\n if (ao === bo) {\n continue\n }\n\n return ao > bo ? 1 : -1\n }\n\n return ai - bi\n })\n .map(([d]) => d)\n}\n\nfunction capitalize(s: string) {\n if (typeof s !== 'string') return ''\n return s.charAt(0).toUpperCase() + s.slice(1)\n}\n\nfunction sanitize(s?: string) {\n return replaceBackslash(s?.replace(/\\\\index/gi, ''))\n}\n\nfunction removeUnderscores(s?: string) {\n return s?.replace(/(^_|_$)/, '').replace(/(\\/_|_\\/)/, '/');\n}\n\nfunction replaceBackslash(s?: string) {\n return s?.replace(/\\\\/gi, '/')\n}\n\nfunction hasParentRoute(routes: RouteNode[], routeToCheck: string | undefined): RouteNode | null {\n if (!routeToCheck || routeToCheck === \"/\") {\n return null;\n }\n for (const route of routes) {\n if (route.routePath === '/') continue;\n if (routeToCheck.startsWith(`${route.routePath}/`) && route.routePath !== routeToCheck) {\n return route;\n }\n }\n const segments = routeToCheck.split(\"/\");\n segments.pop(); // Remove the last segment\n const parentRoute = segments.join(\"/\");\n return hasParentRoute(routes, parentRoute);\n}\n"],"names":["latestTask","rootPathId","fileRouteRegex","getRouteNodes","config","routeFilePrefix","routeFileIgnorePrefix","routeNodes","recurse","dir","fullDir","path","resolve","routesDirectory","dirList","fs","readdir","filter","d","startsWith","Promise","all","map","fileName","fullPath","join","relativePath","stat","isDirectory","filePath","filePathNoExt","removeExt","routePath","replaceBackslash","cleanPath","split","variableName","fileToVariable","endsWith","replace","push","first","skipMessage","generator","console","log","taskId","checkLatest","start","Date","now","multiSortBy","length","routeTree","forEach","node","parentRoute","hasParentRoute","parent","trimmedPath","trimPathLeft","isNonPath","isNonLayout","cleanedPath","removeUnderscores","children","buildRouteConfig","nodes","depth","routeCode","readFile","isRoot","escapedRoutePath","replaceAll","replaced","writeFile","route","childConfigs","spaces","Boolean","routeConfigChildrenText","routeImports","sanitize","relative","dirname","generatedRouteTree","includes","routeTypes","routeNode","routeOptions","routeConfig","routeConfigFileContent","prettier","format","semi","parser","routeTreeContent","catch","err","code","undefined","ensureDir","i","capitalize","substring","lastIndexOf","Array","from","arr","accessors","sort","a","ai","b","bi","accessor","ao","bo","s","charAt","toUpperCase","slice","routes","routeToCheck","segments","pop"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAMA,IAAIA,UAAU,GAAG,CAAC,CAAA;AACX,MAAMC,UAAU,GAAG,SAAQ;AAC3B,MAAMC,cAAc,GAAG,8BAA6B;AAgB3D,eAAeC,aAAaA,CAACC,MAAc,EAAE;EAC3C,MAAM;IAAEC,eAAe;AAAEC,IAAAA,qBAAAA;AAAsB,GAAC,GAAGF,MAAM,CAAA;EAEzD,IAAIG,UAAuB,GAAG,EAAE,CAAA;EAEhC,eAAeC,OAAOA,CAACC,GAAW,EAAE;IAClC,MAAMC,OAAO,GAAGC,wBAAI,CAACC,OAAO,CAACR,MAAM,CAACS,eAAe,EAAEJ,GAAG,CAAC,CAAA;IACzD,IAAIK,OAAO,GAAG,MAAMC,sBAAE,CAACC,OAAO,CAACN,OAAO,CAAC,CAAA;AAEvCI,IAAAA,OAAO,GAAGA,OAAO,CAACG,MAAM,CAAEC,CAAC,IAAK;AAC9B,MAAA,IACEA,CAAC,CAACC,UAAU,CAAC,GAAG,CAAC,I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
const filePath = path.join(dir, fileName)
|
|
59
59
|
const filePathNoExt = removeExt(filePath)
|
|
60
|
-
let routePath = cleanPath(`/${filePathNoExt.split('.').join('/')}`)
|
|
60
|
+
let routePath = replaceBackslash(cleanPath(`/${filePathNoExt.split('.').join('/')}`)) ?? ''
|
|
61
61
|
const variableName = fileToVariable(routePath)
|
|
62
62
|
|
|
63
63
|
// Remove the index from the route path and
|
|
64
64
|
// if the route path is empty, use `/'
|
|
65
|
-
if (routePath
|
|
66
|
-
routePath = routePath.replace(/\/index$/, '/')
|
|
67
|
-
} else if (routePath === 'index') {
|
|
65
|
+
if (routePath === 'index') {
|
|
68
66
|
routePath = '/'
|
|
67
|
+
} else if (routePath.endsWith('/index')) {
|
|
68
|
+
routePath = routePath.replace(/\/index$/, '/')
|
|
69
69
|
}
|
|
70
70
|
|
|
71
71
|
routeNodes.push({
|
|
@@ -129,14 +129,16 @@ export async function generator(config: Config) {
|
|
|
129
129
|
// Loop over the flat list of routeNodes and
|
|
130
130
|
// build up a tree based on the routeNodes' routePath
|
|
131
131
|
routeNodes.forEach((node) => {
|
|
132
|
-
routeNodes.forEach((existingNode) => {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
})
|
|
132
|
+
// routeNodes.forEach((existingNode) => {
|
|
133
|
+
// if (
|
|
134
|
+
// node.routePath?.startsWith(`${existingNode?.routePath ?? ''}/`)
|
|
135
|
+
// // node.routePath.length > existingNode.routePath!.length
|
|
136
|
+
// ) {
|
|
137
|
+
// node.parent = existingNode
|
|
138
|
+
// }
|
|
139
|
+
// })
|
|
140
|
+
const parentRoute = hasParentRoute(routeNodes, node.routePath)
|
|
141
|
+
if (parentRoute) node.parent = parentRoute
|
|
140
142
|
|
|
141
143
|
node.path = node.parent
|
|
142
144
|
? node.routePath?.replace(node.parent.routePath!, '') || '/'
|
|
@@ -173,9 +175,14 @@ export async function generator(config: Config) {
|
|
|
173
175
|
}
|
|
174
176
|
|
|
175
177
|
// Ensure that new FileRoute(anything?) is replace with FileRoute(${node.routePath})
|
|
178
|
+
// routePath can contain $ characters, which have special meaning when used in replace
|
|
179
|
+
// so we have to escape it by turning all $ into $$. But since we do it through a replace call
|
|
180
|
+
// we have to double escape it into $$$$. For more information, see
|
|
181
|
+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace#specifying_a_string_as_the_replacement
|
|
182
|
+
const escapedRoutePath = node.routePath?.replaceAll('$', '$$$$') ?? ''
|
|
176
183
|
const replaced = routeCode.replace(
|
|
177
184
|
fileRouteRegex,
|
|
178
|
-
`new FileRoute('${
|
|
185
|
+
`new FileRoute('${escapedRoutePath}')`,
|
|
179
186
|
)
|
|
180
187
|
|
|
181
188
|
if (replaced !== routeCode) {
|
|
@@ -198,22 +205,21 @@ export async function generator(config: Config) {
|
|
|
198
205
|
const routeConfigChildrenText = await buildRouteConfig(routeTree)
|
|
199
206
|
|
|
200
207
|
const routeImports = [
|
|
201
|
-
`import { route as rootRoute } from './${path.relative(
|
|
208
|
+
`import { route as rootRoute } from './${sanitize(path.relative(
|
|
202
209
|
path.dirname(config.generatedRouteTree),
|
|
203
|
-
path.resolve(config.routesDirectory, rootPathId)
|
|
204
|
-
)}'`,
|
|
210
|
+
path.resolve(config.routesDirectory, rootPathId)))}'`,
|
|
205
211
|
...multiSortBy(routeNodes, [
|
|
206
212
|
(d) => (d.routePath?.includes(`/${rootPathId}`) ? -1 : 1),
|
|
207
213
|
(d) => d.routePath?.split('/').length,
|
|
208
214
|
(d) => (d.routePath?.endsWith("index'") ? -1 : 1),
|
|
209
215
|
(d) => d,
|
|
210
216
|
]).map((node) => {
|
|
211
|
-
return `import { route as ${node.variableName}Route } from './${removeExt(
|
|
217
|
+
return `import { route as ${node.variableName}Route } from './${sanitize(removeExt(
|
|
212
218
|
path.relative(
|
|
213
219
|
path.dirname(config.generatedRouteTree),
|
|
214
220
|
path.resolve(config.routesDirectory, node.filePath),
|
|
215
221
|
),
|
|
216
|
-
)}'`
|
|
222
|
+
))}'`
|
|
217
223
|
}),
|
|
218
224
|
].join('\n')
|
|
219
225
|
|
|
@@ -225,7 +231,7 @@ export async function generator(config: Config) {
|
|
|
225
231
|
parentRoute: typeof ${routeNode.parent?.variableName ?? 'root'}Route
|
|
226
232
|
}`
|
|
227
233
|
})
|
|
228
|
-
.join('\n')}
|
|
234
|
+
.join('\n')}
|
|
229
235
|
}
|
|
230
236
|
}`
|
|
231
237
|
|
|
@@ -236,8 +242,7 @@ export async function generator(config: Config) {
|
|
|
236
242
|
routeNode.isNonPath
|
|
237
243
|
? `id: '${routeNode.cleanedPath}'`
|
|
238
244
|
: `path: '${routeNode.cleanedPath}'`,
|
|
239
|
-
`getParentRoute: () => ${
|
|
240
|
-
routeNode.parent?.variableName ?? 'root'
|
|
245
|
+
`getParentRoute: () => ${routeNode.parent?.variableName ?? 'root'
|
|
241
246
|
}Route`,
|
|
242
247
|
// `\n// ${JSON.stringify(
|
|
243
248
|
// {
|
|
@@ -350,6 +355,30 @@ function capitalize(s: string) {
|
|
|
350
355
|
return s.charAt(0).toUpperCase() + s.slice(1)
|
|
351
356
|
}
|
|
352
357
|
|
|
358
|
+
function sanitize(s?: string) {
|
|
359
|
+
return replaceBackslash(s?.replace(/\\index/gi, ''))
|
|
360
|
+
}
|
|
361
|
+
|
|
353
362
|
function removeUnderscores(s?: string) {
|
|
354
|
-
return s?.replace(/(^_|_$)/, '').replace(/(\/_|_\/)/, '/')
|
|
363
|
+
return s?.replace(/(^_|_$)/, '').replace(/(\/_|_\/)/, '/');
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
function replaceBackslash(s?: string) {
|
|
367
|
+
return s?.replace(/\\/gi, '/')
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
function hasParentRoute(routes: RouteNode[], routeToCheck: string | undefined): RouteNode | null {
|
|
371
|
+
if (!routeToCheck || routeToCheck === "/") {
|
|
372
|
+
return null;
|
|
373
|
+
}
|
|
374
|
+
for (const route of routes) {
|
|
375
|
+
if (route.routePath === '/') continue;
|
|
376
|
+
if (routeToCheck.startsWith(`${route.routePath}/`) && route.routePath !== routeToCheck) {
|
|
377
|
+
return route;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
const segments = routeToCheck.split("/");
|
|
381
|
+
segments.pop(); // Remove the last segment
|
|
382
|
+
const parentRoute = segments.join("/");
|
|
383
|
+
return hasParentRoute(routes, parentRoute);
|
|
355
384
|
}
|