-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
205 lines (179 loc) · 5.28 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import fs from "node:fs";
import path from "node:path";
import {
type BaseMacro,
Elysia,
type InputSchema,
type LocalHook,
type RouteSchema,
type SingletonBase,
} from "elysia";
import type { SoftString } from "./types";
import {
addRelativeIfNotDot,
fixSlashes,
getPath,
sortByNestedParams,
transformToUrl,
} from "./utils";
export type * from "./types";
export type SchemaHandler = ({
path,
url,
}: {
path: string;
url: string;
}) => LocalHook<
InputSchema,
RouteSchema,
SingletonBase,
Record<string, Error>,
BaseMacro,
""
>;
export interface TypesOptions {
output?: string | string[];
typeName?: string;
useExport?: boolean;
}
export interface AutoloadOptions {
pattern?: string;
dir?: string;
prefix?: string;
schema?: SchemaHandler;
types?: TypesOptions | true;
/**
* Throws an error if no matches are found.
* @default true
*/
failGlob?: boolean;
/**
* import a specific `export` from a file
* @example import first export
* ```ts
* import: (file) => Object.keys(file).at(0) || "default",
* ```
* @default "default"
*/
// biome-ignore lint/suspicious/noExplicitAny: import return any
import?: SoftString<"default"> | ((file: any) => string);
/**
* Skip imports where needed `export` not defined
* @default false
*/
skipImportErrors?: boolean;
}
const DIR_ROUTES_DEFAULT = "./routes";
const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
const TYPES_TYPENAME_DEFAULT = "Routes";
const TYPES_OBJECT_DEFAULT = {
output: [TYPES_OUTPUT_DEFAULT],
typeName: TYPES_TYPENAME_DEFAULT,
} satisfies TypesOptions;
export async function autoload(options: AutoloadOptions = {}) {
const { pattern, prefix, schema } = options;
const failGlob = options.failGlob ?? true;
const getImportName = options?.import ?? "default";
const dir = options.dir ?? DIR_ROUTES_DEFAULT;
// some strange code to provide defaults
const types: (Omit<TypesOptions, "output"> & { output: string[] }) | false =
options.types
? options.types !== true
? {
...TYPES_OBJECT_DEFAULT,
...options.types,
// This code allows you to omit the output data or specify it as an string[] or string.
output: !options.types.output
? [TYPES_OUTPUT_DEFAULT]
: Array.isArray(options.types.output)
? options.types.output
: [options.types.output],
}
: TYPES_OBJECT_DEFAULT
: false;
const directoryPath = getPath(dir);
if (!fs.existsSync(directoryPath))
throw new Error(`Directory ${directoryPath} doesn't exists`);
if (!fs.statSync(directoryPath).isDirectory())
throw new Error(`${directoryPath} isn't a directory`);
const plugin = new Elysia({
name: "elysia-autoload",
prefix: fixSlashes(prefix),
seed: {
pattern,
dir,
prefix,
types,
},
});
const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
const files = await Array.fromAsync(
glob.scan({
cwd: directoryPath,
}),
);
if (failGlob && files.length === 0)
throw new Error(
`No matches found in ${directoryPath}. You can disable this error by setting the failGlob parameter to false in the options of autoload plugin`,
);
const paths: [path: string, exportName: string][] = [];
for await (const filePath of sortByNestedParams(files)) {
const fullPath = path.join(directoryPath, filePath);
const file = await import(fullPath);
const importName =
typeof getImportName === "string" ? getImportName : getImportName(file);
if (!file[importName] && options?.skipImportErrors) continue;
if (!file[importName])
throw new Error(`${filePath} don't provide export ${importName}`);
const url = transformToUrl(filePath);
const groupOptions = schema ? schema({ path: filePath, url }) : {};
const importedValue = file[importName];
// TODO: fix type-error later
if (typeof importedValue === "function" && importedValue.length)
// @ts-expect-error
plugin.group(url, groupOptions, importedValue);
if (typeof importedValue === "function" && !importedValue.length)
// @ts-expect-error
plugin.group(url, groupOptions, (app) => app.use(importedValue()));
if (importedValue instanceof Elysia)
// @ts-expect-error
plugin.group(url, groupOptions, (app) => app.use(importedValue));
if (types) paths.push([fullPath.replace(directoryPath, ""), importName]);
}
if (types) {
for await (const outputPath of types.output) {
const outputAbsolutePath = getPath(outputPath);
const imports: string[] = paths.map(
([x, exportName], index) =>
`import type ${exportName === "default" ? `Route${index}` : `{ ${exportName} as Route${index} }`} from "${addRelativeIfNotDot(
path
.relative(
path.dirname(outputAbsolutePath),
directoryPath + x.replace(".ts", "").replace(".tsx", ""),
)
.replace(/\\/gu, "/"),
)}";`,
);
await Bun.write(
outputAbsolutePath,
[
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
imports.join("\n"),
"",
!types.useExport ? "declare global {" : "",
` export type ${types.typeName} = ${paths
.map(
([x], index) =>
`ElysiaWithBaseUrl<"${
((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ??
"") + transformToUrl(x) || "/"
}", typeof Route${index}>`,
)
.join("\n & ")}`,
!types.useExport ? "}" : "",
].join("\n"),
);
}
}
return plugin;
}