From 393832d6bba16e8e744266f677ac3eb2eadd5cc3 Mon Sep 17 00:00:00 2001 From: Florian Rappl Date: Tue, 11 May 2021 01:25:22 +0200 Subject: [PATCH] Follow up in client --- src/server/helpers/files.ts | 32 +++++++++++++++++++++++++++++-- src/server/management/client.ts | 29 +--------------------------- src/server/management/login.ts | 2 +- src/server/management/logs.ts | 2 +- src/server/management/overview.ts | 2 +- 5 files changed, 34 insertions(+), 33 deletions(-) diff --git a/src/server/helpers/files.ts b/src/server/helpers/files.ts index 0db3d03..87078c8 100644 --- a/src/server/helpers/files.ts +++ b/src/server/helpers/files.ts @@ -1,14 +1,42 @@ import * as readFile from 'send'; -import { dirname } from 'path'; +import { resolve, dirname } from 'path'; +import { existsSync } from 'fs'; import { KrasServer, KrasConfiguration } from '../types'; +// this only exists to trick "ncc" -> otherwise it tries to resolve it +// directly at compile-time +const indexHtml = [0].map(() => 'index.html').pop(); + +export function getClient(cwd: string, path: string) { + const fullPath = resolve(cwd, path); + + if (!existsSync(fullPath)) { + const indexPath = resolve(fullPath, indexHtml); + + if (existsSync(indexPath)) { + return indexPath; + } + + try { + const mainPath = require.resolve(path, { + paths: [__dirname, process.cwd(), cwd], + }); + const mainDir = dirname(mainPath); + return resolve(mainDir, indexHtml); + } catch {} + } + + return fullPath; +} + export function withFiles(server: KrasServer, config: KrasConfiguration) { const api = config.api; if (api !== false) { const prefix = `${api}/static/`; + const root = dirname(getClient(config.directory, config.client)); const options = { - root: dirname(config.client), + root, }; server.at(api, 'static/*').get((req, res) => { diff --git a/src/server/management/client.ts b/src/server/management/client.ts index 0f74a25..e824d43 100644 --- a/src/server/management/client.ts +++ b/src/server/management/client.ts @@ -1,34 +1,7 @@ -import { resolve, dirname } from 'path'; -import { existsSync } from 'fs'; import { Request, Response } from 'express'; +import { getClient } from '../helpers'; import { KrasConfiguration, KrasServer } from '../types'; -// this only exists to trick "ncc" -> otherwise it tries to resolve it -// directly at compile-time -const indexHtml = [0].map(() => 'index.html').pop(); - -function getClient(cwd: string, path: string) { - const fullPath = resolve(cwd, path); - - if (!existsSync(fullPath)) { - const indexPath = resolve(fullPath, indexHtml); - - if (existsSync(indexPath)) { - return indexPath; - } - - try { - const mainPath = require.resolve(path, { - paths: [__dirname, process.cwd(), cwd], - }); - const mainDir = dirname(mainPath); - return resolve(mainDir, indexHtml); - } catch {} - } - - return fullPath; -} - export function clientOf(server: KrasServer, config: KrasConfiguration) { const index = getClient(config.directory, config.client); const target = config.api + '/'; diff --git a/src/server/management/login.ts b/src/server/management/login.ts index 4b36589..a73577a 100644 --- a/src/server/management/login.ts +++ b/src/server/management/login.ts @@ -1,7 +1,7 @@ +import * as providers from '../auth'; import { Request, Response } from 'express'; import { parse } from 'url'; import { KrasServer, KrasConfiguration, KrasServerHandler, KrasServerMethods } from '../types'; -import * as providers from '../auth'; const bearer = 'Bearer '; diff --git a/src/server/management/logs.ts b/src/server/management/logs.ts index 68598af..3eb6c5b 100644 --- a/src/server/management/logs.ts +++ b/src/server/management/logs.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; -import { KrasServer, KrasWebSocket } from '../types'; import { filterReverse } from '../helpers'; +import { KrasServer, KrasWebSocket } from '../types'; const maxEntries = 50; diff --git a/src/server/management/overview.ts b/src/server/management/overview.ts index 192c1f1..b85219a 100644 --- a/src/server/management/overview.ts +++ b/src/server/management/overview.ts @@ -1,6 +1,6 @@ import { Request, Response } from 'express'; -import { KrasServer, KrasWebSocket, RecordedRequest, RecordedMessage, RecordedError } from '../types'; import { mapReverse } from '../helpers'; +import { KrasServer, KrasWebSocket, RecordedRequest, RecordedMessage, RecordedError } from '../types'; interface Item { id: string;