From 4cbc73a25f5fff5e161fc4f9c83714436de4a72f Mon Sep 17 00:00:00 2001 From: Alice Gaudon Date: Wed, 24 Mar 2021 16:08:50 +0100 Subject: [PATCH] Fix ServeStaticDirectoryComponent while developping swaf Also move core version detection to Application --- src/Application.ts | 40 ++++++++++++++----- src/components/NunjucksComponent.ts | 15 +------ .../ServeStaticDirectoryComponent.ts | 11 +++-- 3 files changed, 40 insertions(+), 26 deletions(-) diff --git a/src/Application.ts b/src/Application.ts index 02db79c..3b90dfc 100644 --- a/src/Application.ts +++ b/src/Application.ts @@ -21,6 +21,7 @@ import TemplateError = lib.TemplateError; export default abstract class Application implements Extendable> { private readonly version: string; + private coreVersion: string = 'unknown'; private readonly ignoreCommandLine: boolean; private readonly controllers: Controller[] = []; private readonly webSocketListeners: { [p: string]: WebSocketListener } = {}; @@ -58,7 +59,20 @@ export default abstract class Application implements Extendable { - logger.info(`${config.get('app.name')} v${this.version} - hi`); + // Load core version + const file = this.isInNodeModules() ? + path.join(__dirname, '../../package.json') : + path.join(__dirname, '../package.json'); + + try { + this.coreVersion = JSON.parse(fs.readFileSync(file).toString()).version; + } catch (e) { + logger.warn('Couldn\'t determine coreVersion.', e); + } + + logger.info(`${config.get('app.name')} v${this.version} | swaf v${this.coreVersion}`); + + // Catch interrupt signals process.once('SIGINT', () => { this.stop().catch(console.error); }); @@ -264,14 +278,6 @@ export default abstract class Application implements Extendable } { return this.webSocketListeners; } @@ -292,4 +298,20 @@ export default abstract class Application implements Extendable listener.constructor === type); return module ? module as C : null; } + + public isInNodeModules(): boolean { + return fs.existsSync(path.join(__dirname, '../../package.json')); + } + + public isReady(): boolean { + return this.ready; + } + + public getVersion(): string { + return this.version; + } + + public getCoreVersion(): string { + return this.coreVersion; + } } diff --git a/src/components/NunjucksComponent.ts b/src/components/NunjucksComponent.ts index a96ea61..4381fb0 100644 --- a/src/components/NunjucksComponent.ts +++ b/src/components/NunjucksComponent.ts @@ -7,8 +7,6 @@ import * as querystring from "querystring"; import {ParsedUrlQueryInput} from "querystring"; import * as util from "util"; import * as path from "path"; -import * as fs from "fs"; -import {logger} from "../Logger"; import Middleware from "../Middleware"; export default class NunjucksComponent extends ApplicationComponent { @@ -21,17 +19,6 @@ export default class NunjucksComponent extends ApplicationComponent { } public async start(app: Express): Promise { - let coreVersion = 'unknown'; - const file = fs.existsSync(path.join(__dirname, '../../package.json')) ? - path.join(__dirname, '../../package.json') : - path.join(__dirname, '../package.json'); - - try { - coreVersion = JSON.parse(fs.readFileSync(file).toString()).version; - } catch (e) { - logger.warn('Couldn\'t determine coreVersion.', e); - } - const opts = { autoescape: true, noCache: !config.get('view.cache'), @@ -51,7 +38,7 @@ export default class NunjucksComponent extends ApplicationComponent { return Controller.route(route, params, query, absolute); }) .addGlobal('app_version', this.getApp().getVersion()) - .addGlobal('core_version', coreVersion) + .addGlobal('core_version', this.getApp().getCoreVersion()) .addGlobal('querystring', querystring) .addGlobal('app', config.get('app')) diff --git a/src/components/ServeStaticDirectoryComponent.ts b/src/components/ServeStaticDirectoryComponent.ts index 191ac28..d81c8a0 100644 --- a/src/components/ServeStaticDirectoryComponent.ts +++ b/src/components/ServeStaticDirectoryComponent.ts @@ -2,6 +2,7 @@ import ApplicationComponent from "../ApplicationComponent"; import express, {Router} from "express"; import {PathParams} from "express-serve-static-core"; import * as path from "path"; +import {logger} from "../Logger"; export default class ServeStaticDirectoryComponent extends ApplicationComponent { private readonly root: string; @@ -9,16 +10,20 @@ export default class ServeStaticDirectoryComponent extends ApplicationComponent public constructor(root: string, routePath?: PathParams) { super(); - this.root = path.join(__dirname, '../../../', root); + this.root = root; this.path = routePath; } public async init(router: Router): Promise { + const resolvedRoot = path.join(__dirname, this.getApp().isInNodeModules() ? '../../../' : '../../', this.root); + if (this.path) { - router.use(this.path, express.static(this.root, {maxAge: 1000 * 3600 * 72})); + router.use(this.path, express.static(resolvedRoot, {maxAge: 1000 * 3600 * 72})); } else { - router.use(express.static(this.root, {maxAge: 1000 * 3600 * 72})); + router.use(express.static(resolvedRoot, {maxAge: 1000 * 3600 * 72})); } + + logger.info('Serving static files in', resolvedRoot, ' on ', this.path || '/'); } }