Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit 5a69240

Browse files
✨ Add support for route generator
1 parent b23828c commit 5a69240

File tree

5 files changed

+40
-24
lines changed

5 files changed

+40
-24
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,4 @@ typings/
134134
# Built files
135135
dist/
136136
docs/
137+
src/app.ts

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@
77
"license": "MIT",
88
"scripts": {
99
"build-babel": "touch .env && babel src -d dist --extensions '.ts' && cp .env dist/.env",
10-
"build": "touch .env && mkdir -p dist && cp .env dist/.env && tsc",
11-
"check-types": "tsc",
12-
"start": "npm run build-babel && node dist/index.js",
10+
"build": "touch .env && mkdir -p dist && cp .env dist/.env && npm run generate-routes && tsc",
11+
"start": "npm run build && node dist/index.js",
1312
"dev": "concurrently 'yarn watch-build' 'yarn watch-server'",
14-
"prettier-all": "prettier '{src,examples}/**/*.{ts,json}' --write",
13+
"prettier-all": "prettier '{src,examples,setup}/**/*.{ts,js,json}' --write",
1514
"watch-build": "onchange '{src,examples}/**/*.ts' -- yarn build-babel",
1615
"watch-prettier": "onchange '{src,examples}/**/*.{ts,json}' -- prettier --write {{changed}}",
1716
"watch-server": "cd dist && nodemon index.js",
18-
"generate-docs": "typedoc --out docs ./src"
17+
"generate-docs": "typedoc --out docs ./src",
18+
"generate-routes": "node setup/controllers.js"
1919
},
2020
"husky": {
2121
"hooks": {

setup/controllers.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fs = require("fs-extra");
2+
const path = require("path");
3+
4+
const SRC = path.join(__dirname, "..", "src");
5+
let server = fs.readFileSync(path.join(SRC, "server.ts")).toString();
6+
7+
// Find controllers
8+
const controllers = fs.readdirSync(path.join(SRC, "controllers"));
9+
const exportName = [];
10+
controllers.forEach(controller => {
11+
const controllerFile = fs
12+
.readFileSync(path.join(SRC, "controllers", controller))
13+
.toString();
14+
exportName.push(controllerFile.split("export class ")[1].split(" ")[0]);
15+
});
16+
17+
const importCode = `${exportName
18+
.map(
19+
(e, i) =>
20+
`import { ${e} } from "./controllers/${controllers[i].split(".ts")[0]}";`
21+
)
22+
.join("\n")}`;
23+
24+
const insertCode = `
25+
super.addControllers([${exportName.map(e => `new ${e}()`).join(", ")}]);
26+
`;
27+
28+
server = importCode + server.replace("// staart:setup/controllers", insertCode);
29+
30+
fs.writeFileSync(path.join(SRC, "app.ts"), server);
31+
console.log("Paths generated successfully!");
32+
process.exit(0);

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Staart } from "./server";
1+
import { Staart } from "./app";
22
import { PORT, SENTRY_DSN } from "./config";
33
import { init } from "@sentry/node";
44

src/server.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,12 @@ import rfs from "rotating-file-stream";
66
import responseTime from "response-time";
77
import { json, urlencoded } from "body-parser";
88
import { Server } from "@overnightjs/core";
9-
import { UserController } from "./controllers/user";
109
import {
1110
errorHandler,
1211
trackingHandler,
1312
rateLimitHandler,
1413
speedLimitHandler
1514
} from "./helpers/middleware";
16-
import { OrganizationController } from "./controllers/organization";
17-
import { AdminController } from "./controllers/admin";
18-
import { AuthController } from "./controllers/auth";
19-
import { MembershipController } from "./controllers/membership";
2015
import { mkdirSync, existsSync } from "fs";
2116
import { join } from "path";
2217

@@ -48,19 +43,7 @@ export class Staart extends Server {
4843
}
4944

5045
private setupControllers() {
51-
const authController = new AuthController();
52-
const userController = new UserController();
53-
const organizationController = new OrganizationController();
54-
const membershipController = new MembershipController();
55-
const adminController = new AdminController();
56-
57-
super.addControllers([
58-
authController,
59-
userController,
60-
organizationController,
61-
membershipController,
62-
adminController
63-
]);
46+
// staart:setup/controllers
6447
}
6548

6649
public start(port: number): void {

0 commit comments

Comments
 (0)