Last active
November 12, 2023 17:47
-
-
Save anytizer/d89774f978a0da608500b42e94127996 to your computer and use it in GitHub Desktop.
Routes scanner for Angular
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | |
} | |
""") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output example