-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigure.ts
More file actions
129 lines (106 loc) · 4.03 KB
/
configure.ts
File metadata and controls
129 lines (106 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import assert from "node:assert";
import { SyntaxKind } from "ts-morph";
import { CodeTransformer } from "@adonisjs/assembler/code_transformer";
import type Configure from "@adonisjs/core/commands/configure";
import { stubsRoot } from "./stubs/main.js";
type CodeTransformerProject = CodeTransformer["project"];
type SourceFile = ReturnType<CodeTransformerProject["createSourceFile"]>;
function addImportIfNotExists(
sourceFile: SourceFile,
importPath: string,
importName: string,
) {
const existingImports = sourceFile.getImportDeclarations();
const hasImport = existingImports.some(
(imp) =>
imp.getModuleSpecifierValue() === importPath &&
imp.getNamedImports().some((named) => named.getName() === importName),
);
if (!hasImport) {
sourceFile.addImportDeclaration({
moduleSpecifier: importPath,
namedImports: [{ name: importName }],
});
}
}
function addControllerImportIfNotExists(sourceFile: SourceFile) {
const existingVariables = sourceFile.getVariableDeclarations();
const hasControllerImport = existingVariables.some((imp) =>
imp.getText().includes("AuthController"),
);
if (!hasControllerImport) {
const importDeclarations = sourceFile.getImportDeclarations();
if (importDeclarations.length > 0) {
const lastImport = importDeclarations[importDeclarations.length - 1];
sourceFile.insertStatements(lastImport.getChildIndex() + 1, [
"", // Add empty line for spacing
'const AuthController = () => import("#controllers/auth_controller");',
]);
} else {
// If no imports, add at the beginning (after any comments)
sourceFile.insertStatements(0, [
'const AuthController = () => import("#controllers/auth_controller");',
]);
}
}
}
function addNewRoutes(sourceFile: SourceFile) {
// Find the last router statement
const routerStatements = sourceFile
.getDescendantsOfKind(SyntaxKind.CallExpression)
.filter((call) => call.getExpression().getText().startsWith("router."));
const lastRouterStatement = routerStatements[routerStatements.length - 1];
// Add new routes after the last existing route
const newRoutes = `
router.get("/auth/login", [AuthController, "login"]).use(middleware.guest());
router.get("/auth/callback", [AuthController, "callback"]).use(middleware.guest());
router.post("/auth/logout", [AuthController, "logout"]).use(middleware.auth());
`;
if (lastRouterStatement) {
sourceFile.insertText(lastRouterStatement.getEnd() + 1, newRoutes);
} else {
sourceFile.addStatements(newRoutes);
}
}
export async function configure(command: Configure) {
const codemods = await command.createCodemods();
codemods.overwriteExisting = true;
command.logger.info("Konfiguracja @solvro/auth");
command.logger.info(
"Żeby dostać CLIENT_ID i CLIENT_SECRET, zapytaj na #main i zpinguj @Bartosz Gotowski 😍",
);
const clientId = await command.prompt.ask("Jaki masz CLIENT_ID? ", {
hint: "web-planer",
});
const clientSecret = await command.prompt.ask("Jaki masz CLIENT_SECRET? ", {
hint: "a17c54tH8AmWC0yq7FSQbNpPp8wELqeN",
});
await codemods.makeUsingStub(
stubsRoot,
"controllers/auth_controller.stub",
{},
);
await codemods.makeUsingStub(stubsRoot, "config/ally.stub", {});
await codemods.defineEnvVariables({
APP_DOMAIN: "http://localhost:3333",
SOLVRO_AUTH_CLIENT_ID: clientId,
SOLVRO_AUTH_CLIENT_SECRET: clientSecret,
});
await codemods.defineEnvValidations({
variables: {
APP_DOMAIN: `Env.schema.string()`,
SOLVRO_AUTH_CLIENT_ID: "Env.schema.string()",
SOLVRO_AUTH_CLIENT_SECRET: "Env.schema.string()",
},
leadingComment: "Variables for @solvro/auth",
});
const action = command.logger.action("update start/routes.ts");
const project = await codemods.getTsMorphProject();
assert(project);
const file = project?.getSourceFileOrThrow("start/routes.ts");
addImportIfNotExists(file, "./kernel.js", "middleware");
addControllerImportIfNotExists(file);
addNewRoutes(file);
await file.save();
action.succeeded();
}