From 5eaebd5d120372a232b7338a05e9a6e17098828f Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Mon, 25 Jan 2021 13:17:41 +0100 Subject: [PATCH] Upgrade dependencies and bump swaf to ^0.23.0 --- .gitignore | 2 ++ package.json | 18 +++++++++--------- src/App.ts | 27 +++++++++++++-------------- src/controllers/HomeController.ts | 2 +- src/main.ts | 10 +++++----- tsconfig.frontend.json | 1 + tsconfig.json | 1 + 7 files changed, 32 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 7626c60..570ee35 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ node_modules public dist yarn-error.log + +src/package.json diff --git a/package.json b/package.json index 1681a4a..2eefcc6 100644 --- a/package.json +++ b/package.json @@ -5,22 +5,22 @@ "repository": "https://eternae.ink/arisu/swaf-boilerplate", "author": "Alice Gaudon ", "private": true, - "main": "dist/src/main.js", + "main": "dist/main.js", "scripts": { - "dist-webpack": "webpack --mode production", + "test": "jest --verbose --runInBand", "clean": "(test ! -d dist || rm -r dist)", + "prepareSources": "cp package.json src/", "compile": "yarn clean && tsc", - "build": "yarn compile && yarn dist-webpack", - "lint": "eslint . --ext .js,.jsx,.ts,.tsx", - "dev": "concurrently -k -n \"Typescript,Node,Webpack,Maildev\" -p \"[{name}]\" -c \"blue,green,red,yellow\" \"tsc --watch\" \"nodemon\" \"webpack --watch --mode development\" \"maildev\"", - "start": "yarn build && node dist/src/main.js", - "test": "jest --verbose --runInBand" + "build": "yarn prepareSources && yarn compile && webpack --mode production", + "dev": "yarn prepareSources && concurrently -k -n \"Typescript,Node,Webpack,Maildev\" -p \"[{name}]\" -c \"blue,green,red,yellow\" \"tsc --watch\" \"nodemon\" \"webpack --watch --mode development\" \"maildev\"", + "start": "yarn build && node", + "lint": "eslint . --ext .js,.jsx,.ts,.tsx" }, "devDependencies": { "@babel/core": "^7.9.0", "@babel/preset-env": "^7.9.5", "@fortawesome/fontawesome-free": "^5.14.0", - "@types/config": "^0.0.36", + "@types/config": "^0.0.38", "@types/express": "^4.17.6", "@types/express-session": "^1.17.0", "@types/feather-icons": "^4.7.0", @@ -61,6 +61,6 @@ "dependencies": { "config": "^3.3.1", "express": "^4.17.1", - "swaf": "^0.22.5" + "swaf": "^0.23.0" } } diff --git a/src/App.ts b/src/App.ts index f645a64..6272da0 100644 --- a/src/App.ts +++ b/src/App.ts @@ -10,15 +10,15 @@ import ServeStaticDirectoryComponent from "swaf/components/ServeStaticDirectoryC import MaintenanceComponent from "swaf/components/MaintenanceComponent"; import MailComponent from "swaf/components/MailComponent"; import SessionComponent from "swaf/components/SessionComponent"; -import RedirectBackComponent from "swaf/components/RedirectBackComponent"; import FormHelperComponent from "swaf/components/FormHelperComponent"; import CsrfProtectionComponent from "swaf/components/CsrfProtectionComponent"; import WebSocketServerComponent from "swaf/components/WebSocketServerComponent"; import HomeController from "./controllers/HomeController"; import AutoUpdateComponent from "swaf/components/AutoUpdateComponent"; -import packageJson = require('../package.json'); import DummyMigration from "swaf/migrations/DummyMigration"; import DropLegacyLogsTable from "swaf/migrations/DropLegacyLogsTable"; +import PreviousUrlComponent from "swaf/components/PreviousUrlComponent"; +import packageJson = require('./package.json'); export default class App extends Application { public constructor( @@ -43,41 +43,40 @@ export default class App extends Application { } private registerComponents() { - const redisComponent = new RedisComponent(); - const mysqlComponent = new MysqlComponent(); - - const expressAppComponent = new ExpressAppComponent(this.addr, this.port); - this.use(expressAppComponent); - this.use(new NunjucksComponent()); + // Base + this.use(new ExpressAppComponent(this.addr, this.port)); this.use(new LogRequestsComponent()); // Static files this.use(new ServeStaticDirectoryComponent('public')); this.use(new ServeStaticDirectoryComponent('node_modules/feather-icons/dist', '/icons')); + // Dynamic views and routes + this.use(new NunjucksComponent()); + this.use(new PreviousUrlComponent()); + // Maintenance this.use(new MaintenanceComponent(this, () => { - return redisComponent.canServe() && mysqlComponent.canServe(); + return this.as(RedisComponent).canServe() && this.as(MysqlComponent).canServe(); })); this.use(new AutoUpdateComponent()); // Services - this.use(mysqlComponent); + this.use(new MysqlComponent()); this.use(new MailComponent()); // Session - this.use(redisComponent); - this.use(new SessionComponent(redisComponent)); + this.use(new RedisComponent()); + this.use(new SessionComponent(this.as(RedisComponent))); // Utils - this.use(new RedirectBackComponent()); this.use(new FormHelperComponent()); // Middlewares this.use(new CsrfProtectionComponent()); // WebSocket server - this.use(new WebSocketServerComponent(this, expressAppComponent, redisComponent)); + this.use(new WebSocketServerComponent(this, this.as(ExpressAppComponent), this.as(RedisComponent))); } private registerWebSocketListeners() { diff --git a/src/controllers/HomeController.ts b/src/controllers/HomeController.ts index d8a0379..b5e1740 100644 --- a/src/controllers/HomeController.ts +++ b/src/controllers/HomeController.ts @@ -20,6 +20,6 @@ export default class HomeController extends Controller { * This is to test and assert that swaf extended types are available */ protected async goBack(req: Request, res: Response): Promise { - res.redirectBack(); + res.redirect(req.getPreviousUrl() || Controller.route('home')); } } diff --git a/src/main.ts b/src/main.ts index 0033726..babc7df 100644 --- a/src/main.ts +++ b/src/main.ts @@ -2,19 +2,19 @@ import {delimiter} from "path"; // Load config from specified path or default + swaf/config (default defaults) process.env['NODE_CONFIG_DIR'] = - __dirname + '/../../node_modules/swaf/config/' + __dirname + '/../node_modules/swaf/config/' + delimiter - + (process.env['NODE_CONFIG_DIR'] || __dirname + '/../../config/'); + + (process.env['NODE_CONFIG_DIR'] || __dirname + '/../config/'); -import {log} from "swaf/Logger"; +import {logger} from "swaf/Logger"; import App from "./App"; import config from "config"; (async () => { - log.debug('Config path:', process.env['NODE_CONFIG_DIR']); + logger.debug('Config path:', process.env['NODE_CONFIG_DIR']); const app = new App(config.get('listen_addr'), config.get('port')); await app.start(); })().catch(err => { - log.error(err); + logger.error(err); }); diff --git a/tsconfig.frontend.json b/tsconfig.frontend.json index 4b5a3f5..ad74720 100644 --- a/tsconfig.frontend.json +++ b/tsconfig.frontend.json @@ -2,6 +2,7 @@ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "public/js", + "rootDir": "./assets", "target": "ES6", "strict": true, "lib": [ diff --git a/tsconfig.json b/tsconfig.json index 9034d02..12eec0a 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -3,6 +3,7 @@ "module": "CommonJS", "esModuleInterop": true, "outDir": "dist", + "rootDir": "./src", "target": "ES6", "strict": true, "lib": [