|
4 | 4 | * @license MIT |
5 | 5 | * @copyright Romain Lanz <[email protected]> |
6 | 6 | */ |
| 7 | +import assert from "node:assert"; |
| 8 | + |
| 9 | +import { CodeTransformer } from "@adonisjs/assembler/code_transformer"; |
7 | 10 | import type Configure from "@adonisjs/core/commands/configure"; |
8 | 11 |
|
9 | 12 | import { stubsRoot } from "./stubs/main.js"; |
10 | 13 |
|
| 14 | +type CodeTransformerProject = CodeTransformer["project"]; |
| 15 | + |
| 16 | +type SourceFile = ReturnType<CodeTransformerProject["createSourceFile"]>; |
| 17 | + |
| 18 | +function addImportIfNotExists( |
| 19 | + sourceFile: SourceFile, |
| 20 | + importPath: string, |
| 21 | + importName: string, |
| 22 | +) { |
| 23 | + const existingImports = sourceFile.getImportDeclarations(); |
| 24 | + const hasImport = existingImports.some( |
| 25 | + (imp) => |
| 26 | + imp.getModuleSpecifierValue() === importPath && |
| 27 | + imp.getNamedImports().some((named) => named.getName() === importName), |
| 28 | + ); |
| 29 | + |
| 30 | + if (!hasImport) { |
| 31 | + sourceFile.addImportDeclaration({ |
| 32 | + moduleSpecifier: importPath, |
| 33 | + namedImports: [{ name: importName }], |
| 34 | + }); |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +function addControllerImportIfNotExists(sourceFile: SourceFile) { |
| 39 | + const existingVariables = sourceFile.getVariableDeclarations(); |
| 40 | + const hasControllerImport = existingVariables.some((imp) => |
| 41 | + imp.getText().includes("AuthController"), |
| 42 | + ); |
| 43 | + |
| 44 | + if (!hasControllerImport) { |
| 45 | + sourceFile.addVariableStatement({ |
| 46 | + declarations: [ |
| 47 | + { |
| 48 | + name: "AuthController", |
| 49 | + initializer: '() => import("#controllers/auth_controller")', |
| 50 | + }, |
| 51 | + ], |
| 52 | + }); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +function addNewRoutes(sourceFile: SourceFile) { |
| 57 | + // Find the last router statement |
| 58 | + const routerStatements = sourceFile |
| 59 | + // @ts-expect-error ?? |
| 60 | + .getDescendantsOfKind(SyntaxKind.CallExpression) |
| 61 | + // @ts-expect-error ?? |
| 62 | + .filter((call) => call.getExpression().getText().startsWith("router.")); |
| 63 | + |
| 64 | + const lastRouterStatement = routerStatements[routerStatements.length - 1]; |
| 65 | + |
| 66 | + // Add new routes after the last existing route |
| 67 | + const newRoutes = ` |
| 68 | +router.get("/auth/login", [AuthController, "login"]).use(middleware.guest()); |
| 69 | +router.get("/auth/callback", [AuthController, "callback"]).use(middleware.guest()); |
| 70 | +router.post("/auth/logout", [AuthController, "logout"]).use(middleware.auth()); |
| 71 | +`; |
| 72 | + if (lastRouterStatement) { |
| 73 | + sourceFile.insertText(lastRouterStatement.getEnd() + 1, newRoutes); |
| 74 | + } else { |
| 75 | + sourceFile.addStatements(newRoutes); |
| 76 | + } |
| 77 | +} |
| 78 | + |
11 | 79 | export async function configure(command: Configure) { |
12 | 80 | const codemods = await command.createCodemods(); |
13 | 81 |
|
@@ -46,4 +114,15 @@ export async function configure(command: Configure) { |
46 | 114 | }, |
47 | 115 | leadingComment: "Variables for @solvro/auth", |
48 | 116 | }); |
| 117 | + |
| 118 | + const project = await codemods.getTsMorphProject(); |
| 119 | + assert(project); |
| 120 | + |
| 121 | + const file = project?.getSourceFileOrThrow("start/routes.ts"); |
| 122 | + |
| 123 | + addImportIfNotExists(file, "./kernel.js", "middleware"); |
| 124 | + addControllerImportIfNotExists(file); |
| 125 | + addNewRoutes(file); |
| 126 | + |
| 127 | + await file.save(); |
49 | 128 | } |
0 commit comments