Skip to content

Commit 4806d08

Browse files
committed
feat: add routes on configure
1 parent a929857 commit 4806d08

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

packages/ally-solvro-auth/configure.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,78 @@
44
* @license MIT
55
* @copyright Romain Lanz <[email protected]>
66
*/
7+
import assert from "node:assert";
8+
9+
import { CodeTransformer } from "@adonisjs/assembler/code_transformer";
710
import type Configure from "@adonisjs/core/commands/configure";
811

912
import { stubsRoot } from "./stubs/main.js";
1013

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+
1179
export async function configure(command: Configure) {
1280
const codemods = await command.createCodemods();
1381

@@ -46,4 +114,15 @@ export async function configure(command: Configure) {
46114
},
47115
leadingComment: "Variables for @solvro/auth",
48116
});
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();
49128
}

packages/ally-solvro-auth/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"eslint": "^9.20.1",
6464
"prettier": "^3.5.1",
6565
"semantic-release": "^24.2.2",
66+
"ts-morph": "^25.0.1",
6667
"ts-node": "^10.9.2",
6768
"typescript": "^5.7.3"
6869
},

0 commit comments

Comments
 (0)