elysia-autoload 0.1.8 → 0.1.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +57 -55
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -15,7 +15,7 @@ export interface IAutoloadOptions {
|
|
15
15
|
schema?: TSchemaHandler;
|
16
16
|
types?: ITypesOptions | true;
|
17
17
|
}
|
18
|
-
export declare function autoload(options?: IAutoloadOptions): Promise<Elysia<string, false, {
|
18
|
+
export declare function autoload(options?: IAutoloadOptions): () => Promise<Elysia<string, false, {
|
19
19
|
decorator: {};
|
20
20
|
store: {};
|
21
21
|
derive: {};
|
package/dist/index.js
CHANGED
@@ -21,62 +21,64 @@ const elysia_1 = require("elysia");
|
|
21
21
|
const utils_1 = require("./utils");
|
22
22
|
const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
|
23
23
|
const TYPES_TYPENAME_DEFAULT = "Routes";
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
const
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
if (types) {
|
58
|
-
const imports = paths.map((x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`);
|
59
|
-
for await (const outputPath of types === true || !types.output
|
60
|
-
? [TYPES_OUTPUT_DEFAULT]
|
61
|
-
: Array.isArray(types.output)
|
62
|
-
? types.output
|
63
|
-
: [types.output]) {
|
64
|
-
await Bun.write((0, utils_1.getPath)(outputPath), [
|
65
|
-
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
|
66
|
-
imports.join("\n"),
|
67
|
-
"",
|
68
|
-
types === true || !types.useExport ? "declare global {" : "",
|
69
|
-
` export type ${types === true || !types.typeName
|
70
|
-
? TYPES_TYPENAME_DEFAULT
|
71
|
-
: types.typeName} = ${paths
|
72
|
-
.map((x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ??
|
73
|
-
"") + (0, utils_1.transformToUrl)(x) || "/"}", ReturnType<typeof Route${index}>>`)
|
74
|
-
.join("\n & ")}`,
|
75
|
-
types === true || !types.useExport ? "}" : "",
|
76
|
-
].join("\n"));
|
24
|
+
function autoload(options = {}) {
|
25
|
+
return async () => {
|
26
|
+
const { pattern, dir, prefix, schema, types } = options;
|
27
|
+
const directoryPath = (0, utils_1.getPath)(dir || "./routes");
|
28
|
+
if (!(0, node_fs_1.existsSync)(directoryPath))
|
29
|
+
throw new Error(`Directory ${directoryPath} doesn't exists`);
|
30
|
+
if (!(0, node_fs_1.statSync)(directoryPath).isDirectory())
|
31
|
+
throw new Error(`${directoryPath} isn't a directory`);
|
32
|
+
const plugin = new elysia_1.Elysia({
|
33
|
+
name: "elysia-autoload",
|
34
|
+
prefix: (0, utils_1.fixSlashes)(prefix),
|
35
|
+
seed: {
|
36
|
+
pattern,
|
37
|
+
dir,
|
38
|
+
prefix,
|
39
|
+
types,
|
40
|
+
},
|
41
|
+
});
|
42
|
+
const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
|
43
|
+
const files = await Array.fromAsync(glob.scan({
|
44
|
+
cwd: directoryPath,
|
45
|
+
}));
|
46
|
+
const paths = [];
|
47
|
+
for await (const path of (0, utils_1.sortByNestedParams)(files)) {
|
48
|
+
const fullPath = (0, node_path_1.join)(directoryPath, path);
|
49
|
+
const file = await Promise.resolve(`${fullPath}`).then(s => require(s));
|
50
|
+
if (!file.default)
|
51
|
+
throw new Error(`${path} doesn't provide default export`);
|
52
|
+
const url = (0, utils_1.transformToUrl)(path);
|
53
|
+
const groupOptions = schema ? schema({ path, url }) : {};
|
54
|
+
plugin.group(url, groupOptions, file.default);
|
55
|
+
if (types)
|
56
|
+
paths.push(fullPath.replace(directoryPath, ""));
|
77
57
|
}
|
78
|
-
|
79
|
-
|
58
|
+
if (types) {
|
59
|
+
const imports = paths.map((x, index) => `import type Route${index} from "${(directoryPath + x.replace(".ts", "").replace(".tsx", "")).replace(/\\/gu, "/")}";`);
|
60
|
+
for await (const outputPath of types === true || !types.output
|
61
|
+
? [TYPES_OUTPUT_DEFAULT]
|
62
|
+
: Array.isArray(types.output)
|
63
|
+
? types.output
|
64
|
+
: [types.output]) {
|
65
|
+
await Bun.write((0, utils_1.getPath)(outputPath), [
|
66
|
+
`import type { ElysiaWithBaseUrl } from "elysia-autoload";`,
|
67
|
+
imports.join("\n"),
|
68
|
+
"",
|
69
|
+
types === true || !types.useExport ? "declare global {" : "",
|
70
|
+
` export type ${types === true || !types.typeName
|
71
|
+
? TYPES_TYPENAME_DEFAULT
|
72
|
+
: types.typeName} = ${paths
|
73
|
+
.map((x, index) => `ElysiaWithBaseUrl<"${((prefix?.endsWith("/") ? prefix.slice(0, -1) : prefix) ??
|
74
|
+
"") + (0, utils_1.transformToUrl)(x) || "/"}", ReturnType<typeof Route${index}>>`)
|
75
|
+
.join("\n & ")}`,
|
76
|
+
types === true || !types.useExport ? "}" : "",
|
77
|
+
].join("\n"));
|
78
|
+
}
|
79
|
+
}
|
80
|
+
return plugin;
|
81
|
+
};
|
80
82
|
}
|
81
83
|
exports.autoload = autoload;
|
82
84
|
__exportStar(require("./types"), exports);
|