Skip to content

Instantly share code, notes, and snippets.

@mattmcmanus
Created February 8, 2023 15:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmcmanus/b50259fa97d8e24920e2eaa95c5e0fb2 to your computer and use it in GitHub Desktop.
Save mattmcmanus/b50259fa97d8e24920e2eaa95c5e0fb2 to your computer and use it in GitHub Desktop.
An alternative class to print the full route tree in the console

Router Printer

  1. Add router-printer.ts to you app
  2. Update your router.js to import it:
-import EmberRouter from '@ember/routing/router';
+import Router from './router-printer'

-export default class Router extends EmberRouter {
-	location = config.locationType;
-	rootURL = config.rootURL;
-}
  1. Run the script
cd app
ts-node ./router.js
interface Options {
resetNamespace?: boolean;
path?: string;
}
type Callback = (this: Router) => void;
interface Route {
name: string;
path: string;
}
class Router {
parent: Route | undefined;
routes: Route[] = [];
constructor(parent?: Route) {
this.parent = parent;
}
static map(cb: Callback): void {
const self = new Router();
cb.call(self);
self.print();
}
print(): void {
this.routes.forEach((route) => {
// eslint-disable-next-line no-console
console.log(`${route.name}\t${route.path}`);
});
}
route(localName: string, optionsOrCb?: Options | Callback, cb?: Callback): void {
let callback: Callback | undefined;
let options: Options = {};
if (cb) {
callback = cb;
if (optionsOrCb && typeof optionsOrCb === 'object') {
options = optionsOrCb;
}
} else if (typeof optionsOrCb === 'function') {
callback = optionsOrCb;
} else if (typeof optionsOrCb === 'object') {
options = optionsOrCb;
}
// Merge with parent route name
const name = this.parent ? `${this.parent.name}.${localName}` : localName;
const localPath = `/${options.path ?? localName}`;
let path = this.parent ? `${this.parent.path}${localPath}` : localPath;
path = path.replace(/\/{2,}/, '/'); // Clean up
const route = { name, path };
if (callback) {
const childRouter = new Router(route);
callback.call(childRouter);
this.routes = this.routes.concat(...childRouter.routes);
} else {
this.routes.push(route);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment