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 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
- async function autoload(options = {}) {
25
- const { pattern, dir, prefix, schema, types } = options;
26
- const directoryPath = (0, utils_1.getPath)(dir || "./routes");
27
- if (!(0, node_fs_1.existsSync)(directoryPath))
28
- throw new Error(`Directory ${directoryPath} doesn't exists`);
29
- if (!(0, node_fs_1.statSync)(directoryPath).isDirectory())
30
- throw new Error(`${directoryPath} isn't a directory`);
31
- const plugin = new elysia_1.Elysia({
32
- name: "elysia-autoload",
33
- prefix: (0, utils_1.fixSlashes)(prefix),
34
- seed: {
35
- pattern,
36
- dir,
37
- prefix,
38
- types,
39
- },
40
- });
41
- const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
42
- const files = await Array.fromAsync(glob.scan({
43
- cwd: directoryPath,
44
- }));
45
- const paths = [];
46
- for await (const path of (0, utils_1.sortByNestedParams)(files)) {
47
- const fullPath = (0, node_path_1.join)(directoryPath, path);
48
- const file = await Promise.resolve(`${fullPath}`).then(s => require(s));
49
- if (!file.default)
50
- throw new Error(`${path} doesn't provide default export`);
51
- const url = (0, utils_1.transformToUrl)(path);
52
- const groupOptions = schema ? schema({ path, url }) : {};
53
- plugin.group(url, groupOptions, file.default);
54
- if (types)
55
- paths.push(fullPath.replace(directoryPath, ""));
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
- return plugin;
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);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "elysia-autoload",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "author": "kravetsone",
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",