Skip to content

Instantly share code, notes, and snippets.

@anytizer
Last active November 12, 2023 17:47
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 anytizer/d89774f978a0da608500b42e94127996 to your computer and use it in GitHub Desktop.
Save anytizer/d89774f978a0da608500b42e94127996 to your computer and use it in GitHub Desktop.
Routes scanner for Angular
import glob
import mariadb
import sys
# Semi automated - routing file generator for Angular
#
# Usage:
# pythoh routes-scanner.py > app-routing.module.ts
fourzerofour_handler = "front/front-pagenotfound"
fourzerofour_component = "FrontPagenotfoundComponent"
app = "client/app"
files = glob.glob(app+"/*/*.component.ts")
files.extend(glob.glob(app+"/*/*/*.component.ts"))
routes = []
for file in files:
identifier = file.replace("\\", "/").replace(app+"/", "").replace(".component.ts", "")
importpath = "./"+identifier+".component"
chunks = identifier.split("/")
if(len(chunks)==3):
pathname = chunks[0]+"/"+chunks[1]
component = "".join([c.title() for c in (chunks[2]+"-component").split("-")])
routes.append([pathname, component, importpath])
print(""""use strict";
/**
* Generated by Routes Scanner:
* python routes-scanner.py > app-routing.module.ts
*/
import { NgModule } from '@angular/core';
import { Route, RouterModule } from '@angular/router';
""")
for r in routes:
print(f"import {{ {r[1]} }} from '"+r[2]+"';")
routex = []
routex.append(f"{{'path': '', redirectTo: '{fourzerofour_handler}', 'pathMatch': 'full'}},")
for r in routes:
routex.append(f"{{'path': '{r[0]}', 'component': {r[1]}}},")
routex.append(f"{{'path': '**', 'component': {fourzerofour_component}}},")
print()
print("let routes: Route[] = [")
for r in routex:
print(" "+r)
print("""];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {
}
""")
import mariadb
"""
CREATE TABLE `system_routes_ng` (
`ng_path_name` VARCHAR(255) NOT NULL,
`ng_component_name` VARCHAR(255) NOT NULL,
`ng_redirect_to` VARCHAR(255) NOT NULL,
`ng_import_path` VARCHAR(255) NOT NULL,
PRIMARY KEY (`ng_path_name`)
);
"""
conn = mariadb.connect(user="user", password="password", host="host", port=3306, database="database")
curr = conn.cursor()
curr.execute("DELETE FROM system_routes_ng;")
curr.executemany("INSERT IGNORE INTO system_routes_ng (ng_path_name, ng_component_name, ng_import_path) VALUES (%s, %s, %s);", routes)
conn.commit()
conn.close()
@anytizer
Copy link
Author

anytizer commented Nov 11, 2023

Output example

"use strict";

/**
 * Generated by Routes Scanner:
 *   python routes-scanner.py > app-routing.module.ts
 */

import { NgModule } from '@angular/core';
import { Route, RouterModule } from '@angular/router';

import { AccountingComponent } from './accounting/accounting/accounting.component';
import { AdminComponent } from './admin/admin/admin.component';
import { AdminAccessLogsComponent } from './admin/admin-access-logs/admin-access-logs.component';
import { AdminApplicationsComponent } from './admin/admin-applications/admin-applications.component';
import { AdminDashboardComponent } from './admin/admin-dashboard/admin-dashboard.component';
import { AdminPasswordComponent } from './admin/admin-password/admin-password.component';
import { AdminProfileComponent } from './admin/admin-profile/admin-profile.component';
import { AdminRegistrationsComponent } from './admin/admin-registrations/admin-registrations.component';
import { AgentComponent } from './agent/agent/agent.component';
import { BankingComponent } from './banking/banking/banking.component';
import { DeveloperComponent } from './developer/developer/developer.component';
import { FinanceComponent } from './finance/finance/finance.component';
import { FrontComponent } from './front/front/front.component';
import { FrontLoginComponent } from './front/front-login/front-login.component';
import { FrontLogoutComponent } from './front/front-logout/front-logout.component';
import { FrontPagenotfoundComponent } from './front/front-pagenotfound/front-pagenotfound.component';
import { FrontRegistrationComponent } from './front/front-registration/front-registration.component';
import { FrontSuccessComponent } from './front/front-success/front-success.component';
import { SystemComponent } from './system/system/system.component';
import { SystemRolesComponent } from './system/system-roles/system-roles.component';
import { SystemRolesMenusComponent } from './system/system-roles-menus/system-roles-menus.component';
import { WwwComponent } from './www/www/www.component';
import { WwwAboutusComponent } from './www/www-aboutus/www-aboutus.component';
import { WwwBlogComponent } from './www/www-blog/www-blog.component';
import { WwwIndexComponent } from './www/www-index/www-index.component';
import { WwwServicesComponent } from './www/www-services/www-services.component';

let routes: Route[] = [
    {'path': '', redirectTo: 'front/front-pagenotfound', 'pathMatch': 'full'},
    {'path': 'accounting/accounting', 'component': AccountingComponent},
    {'path': 'admin/admin', 'component': AdminComponent},
    {'path': 'admin/admin-access-logs', 'component': AdminAccessLogsComponent},
    {'path': 'admin/admin-applications', 'component': AdminApplicationsComponent},
    {'path': 'admin/admin-dashboard', 'component': AdminDashboardComponent},
    {'path': 'admin/admin-password', 'component': AdminPasswordComponent},
    {'path': 'admin/admin-profile', 'component': AdminProfileComponent},
    {'path': 'admin/admin-registrations', 'component': AdminRegistrationsComponent},
    {'path': 'agent/agent', 'component': AgentComponent},
    {'path': 'banking/banking', 'component': BankingComponent},
    {'path': 'developer/developer', 'component': DeveloperComponent},
    {'path': 'finance/finance', 'component': FinanceComponent},
    {'path': 'front/front', 'component': FrontComponent},
    {'path': 'front/front-login', 'component': FrontLoginComponent},
    {'path': 'front/front-logout', 'component': FrontLogoutComponent},
    {'path': 'front/front-pagenotfound', 'component': FrontPagenotfoundComponent},
    {'path': 'front/front-registration', 'component': FrontRegistrationComponent},
    {'path': 'front/front-success', 'component': FrontSuccessComponent},
    {'path': 'system/system', 'component': SystemComponent},
    {'path': 'system/system-roles', 'component': SystemRolesComponent},
    {'path': 'system/system-roles-menus', 'component': SystemRolesMenusComponent},
    {'path': 'www/www', 'component': WwwComponent},
    {'path': 'www/www-aboutus', 'component': WwwAboutusComponent},
    {'path': 'www/www-blog', 'component': WwwBlogComponent},
    {'path': 'www/www-index', 'component': WwwIndexComponent},
    {'path': 'www/www-services', 'component': WwwServicesComponent},
    {'path': '**', 'component': FrontPagenotfoundComponent},
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule],
})
export class AppRoutingModule {
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment