bun-router 0.7.4-experimental.3 → 0.7.4-experimental.5
Sign up to get free protection for your applications and to get access to all the features.
package/lib/fs/fsys.ts
CHANGED
@@ -32,5 +32,9 @@ function splitFilePath(p: string): string[] {
|
|
32
32
|
return p.split(path.sep);
|
33
33
|
}
|
34
34
|
|
35
|
+
function resolveModulePath(module: string) {
|
36
|
+
return path.join(process.cwd(), module);
|
37
|
+
}
|
38
|
+
|
35
39
|
|
36
|
-
export { readDir, ext, splitFilePath };
|
40
|
+
export { readDir, ext, splitFilePath, resolveModulePath };
|
package/lib/logger/logger.ts
CHANGED
@@ -9,7 +9,7 @@ _ _
|
|
9
9
|
|___|___|_|_| |_| |___|___|_| |___|_|
|
10
10
|
|
11
11
|
`;
|
12
|
-
const VERSION = '0.7.4-experimental';
|
12
|
+
const VERSION = '0.7.4-experimental.5';
|
13
13
|
const Logger = (): BunLogger => {
|
14
14
|
return {
|
15
15
|
info: async (statusCode: number, routePath: string, method: string, message?: string) => {
|
@@ -13,16 +13,31 @@ async function createFileTree(root: string) {
|
|
13
13
|
}
|
14
14
|
|
15
15
|
async function resolveModules(root: string) {
|
16
|
+
|
17
|
+
if (!await doesExist(root)) {
|
18
|
+
throw new Error(`Directory ${root} does not exist`);
|
19
|
+
}
|
20
|
+
|
16
21
|
const tree = await createFileTree(root);
|
17
22
|
const files = tree.getFilesByExtension('.tsx');
|
18
23
|
const modules: ComponentType[] = [];
|
19
24
|
|
20
25
|
for (const file of files) {
|
21
|
-
const module = await import(file)
|
26
|
+
const module = await import(file);
|
22
27
|
modules.push(module);
|
23
28
|
}
|
24
|
-
|
25
29
|
return modules;
|
26
30
|
}
|
27
31
|
|
28
|
-
|
32
|
+
async function doesExist(root: string): Promise<boolean> {
|
33
|
+
const file = Bun.file(root);
|
34
|
+
return await file.exists();
|
35
|
+
}
|
36
|
+
|
37
|
+
async function isDir(root: string): Promise<boolean> {
|
38
|
+
const file = Bun.file(root);
|
39
|
+
return await file.isDir();
|
40
|
+
}
|
41
|
+
|
42
|
+
|
43
|
+
export { resolveModules };
|
package/lib/router/router.ts
CHANGED
@@ -2,7 +2,7 @@ import path from 'path';
|
|
2
2
|
import { Database } from 'bun:sqlite';
|
3
3
|
import { Route, BunRouter, RouterOptions, Options, HttpHandler } from './router.d';
|
4
4
|
import { httpStatusCodes } from '../http/status';
|
5
|
-
import { readDir } from '../fs/fsys';
|
5
|
+
import { readDir, resolveModulePath } from '../fs/fsys';
|
6
6
|
import { Logger, startMessage } from '../logger/logger';
|
7
7
|
import { http } from '../http/http';
|
8
8
|
import { RouteTree } from './tree';
|
@@ -36,12 +36,11 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
36
36
|
let base = path.basename(fp);
|
37
37
|
|
38
38
|
//FIXME: this can be improved
|
39
|
-
if (ext === '.html') base = base.replace(ext, '');
|
40
|
-
if (ext === '.tsx') base = base.replace(ext, '');
|
39
|
+
if (ext === '.html' || ext === '.tsx') base = base.replace(ext, '');
|
41
40
|
|
42
41
|
if (pattern[0] !== '/') pattern = '/' + pattern;
|
43
42
|
|
44
|
-
let patternPath = pattern + base;
|
43
|
+
let patternPath = pattern + '/' + base;
|
45
44
|
|
46
45
|
if (base === 'index') patternPath = pattern;
|
47
46
|
|
@@ -55,7 +54,7 @@ const Router: BunRouter = (port?: number | string, options?: RouterOptions<Optio
|
|
55
54
|
method: 'GET',
|
56
55
|
handler: async () => {
|
57
56
|
if (ext === '.tsx') {
|
58
|
-
const component = await loadComponent(purePath.split('.')[0]);
|
57
|
+
const component = await loadComponent(resolveModulePath(purePath.split('.')[0]));
|
59
58
|
return await http.render(component());
|
60
59
|
} else {
|
61
60
|
return await http.file(200, fp);
|
package/package.json
CHANGED
package/tsconfig.json
CHANGED