Skip to content

Commit

Permalink
Follow up in client
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianRappl committed May 10, 2021
1 parent 7beaa7c commit 393832d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 33 deletions.
32 changes: 30 additions & 2 deletions src/server/helpers/files.ts
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
29 changes: 1 addition & 28 deletions src/server/management/client.ts
Original file line number Diff line number Diff line change
@@ -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 + '/';
Expand Down
2 changes: 1 addition & 1 deletion src/server/management/login.ts
Original file line number Diff line number Diff line change
@@ -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 ';

Expand Down
2 changes: 1 addition & 1 deletion src/server/management/logs.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/server/management/overview.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 393832d

Please sign in to comment.