elysia-autoload 1.5.2 → 1.6.0
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/README.md +21 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +25 -3
- package/package.json +1 -1
package/README.md
CHANGED
@@ -41,6 +41,26 @@ const app = new Elysia().use(await autoload()).listen(3000);
|
|
41
41
|
export type ElysiaApp = typeof app;
|
42
42
|
```
|
43
43
|
|
44
|
+
### With custom options
|
45
|
+
|
46
|
+
```ts
|
47
|
+
import { Elysia } from "elysia";
|
48
|
+
import { autoload } from "elysia-autoload";
|
49
|
+
|
50
|
+
const app = new Elysia()
|
51
|
+
.use(
|
52
|
+
await autoload({
|
53
|
+
dir: "./routes",
|
54
|
+
prefix: "/api",
|
55
|
+
// Ignore test files and spec files
|
56
|
+
ignore: ["**/*.test.ts", "**/*.spec.ts"]
|
57
|
+
})
|
58
|
+
)
|
59
|
+
.listen(3000);
|
60
|
+
|
61
|
+
export type ElysiaApp = typeof app;
|
62
|
+
```
|
63
|
+
|
44
64
|
> [!IMPORTANT]
|
45
65
|
> We strictly recommend use `await` when registering plugin
|
46
66
|
>
|
@@ -99,6 +119,7 @@ Guide how `elysia-autoload` match routes
|
|
99
119
|
| prefix? | string | | Prefix for routes |
|
100
120
|
| types? | boolean \| [Types Options](#types-options) | false | Options to configure type code-generation. if boolean - enables/disables generation |
|
101
121
|
| schema? | Function | | Handler for providing routes guard schema |
|
122
|
+
| ignore? | string \| string[] | | Glob pattern(s) to ignore when discovering files |
|
102
123
|
|
103
124
|
### Types Options
|
104
125
|
|
package/dist/index.d.ts
CHANGED
@@ -53,6 +53,10 @@ interface AutoloadOptions {
|
|
53
53
|
* @default false
|
54
54
|
*/
|
55
55
|
skipImportErrors?: boolean;
|
56
|
+
/**
|
57
|
+
* Glob pattern(s) to ignore when discovering files
|
58
|
+
*/
|
59
|
+
ignore?: string | string[];
|
56
60
|
}
|
57
61
|
declare function autoload(options?: AutoloadOptions): Promise<Elysia$1<string, {
|
58
62
|
decorator: {};
|
package/dist/index.js
CHANGED
@@ -30,6 +30,19 @@ const IS_BUN = typeof Bun !== "undefined";
|
|
30
30
|
function globSync(globPattern, globOptions) {
|
31
31
|
return IS_BUN ? Array.from(new Bun.Glob(globPattern).scanSync(globOptions)) : fs.globSync(globPattern, globOptions);
|
32
32
|
}
|
33
|
+
function matchesPattern(filePath, pattern) {
|
34
|
+
if (IS_BUN) {
|
35
|
+
const glob = new Bun.Glob(pattern);
|
36
|
+
return glob.match(filePath);
|
37
|
+
}
|
38
|
+
let regexPattern = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&").replace(/\\\*/g, "*").replace(/\\\?/g, "?");
|
39
|
+
regexPattern = regexPattern.replace(/\*\*/g, "\xA7\xA7\xA7").replace(/\*/g, "[^/]*").replace(/§§§/g, ".*").replace(/\?/g, ".").replace(/\{([^}]+)\}/g, (_, group) => {
|
40
|
+
const options = group.split(",").map((s) => s.trim());
|
41
|
+
return `(${options.join("|")})`;
|
42
|
+
});
|
43
|
+
const regex = new RegExp(`^${regexPattern}$`);
|
44
|
+
return regex.test(filePath);
|
45
|
+
}
|
33
46
|
|
34
47
|
const DIR_ROUTES_DEFAULT = "./routes";
|
35
48
|
const TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
|
@@ -39,7 +52,7 @@ const TYPES_OBJECT_DEFAULT = {
|
|
39
52
|
typeName: TYPES_TYPENAME_DEFAULT
|
40
53
|
};
|
41
54
|
async function autoload(options = {}) {
|
42
|
-
const { pattern, prefix, schema } = options;
|
55
|
+
const { pattern, prefix, schema, ignore } = options;
|
43
56
|
const failGlob = options.failGlob ?? true;
|
44
57
|
const getImportName = options?.import ?? "default";
|
45
58
|
const dir = options.dir ?? DIR_ROUTES_DEFAULT;
|
@@ -61,12 +74,21 @@ async function autoload(options = {}) {
|
|
61
74
|
pattern,
|
62
75
|
dir,
|
63
76
|
prefix,
|
64
|
-
types
|
77
|
+
types,
|
78
|
+
ignore
|
65
79
|
}
|
66
80
|
});
|
67
81
|
const globPattern = pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}";
|
68
82
|
const globOptions = { cwd: directoryPath };
|
69
|
-
|
83
|
+
let files = globSync(globPattern, globOptions);
|
84
|
+
if (ignore) {
|
85
|
+
const ignorePatterns = Array.isArray(ignore) ? ignore : [ignore];
|
86
|
+
files = files.filter((filePath) => {
|
87
|
+
return !ignorePatterns.some(
|
88
|
+
(pattern2) => matchesPattern(filePath, pattern2)
|
89
|
+
);
|
90
|
+
});
|
91
|
+
}
|
70
92
|
if (failGlob && files.length === 0)
|
71
93
|
throw new Error(
|
72
94
|
`No matches found in ${directoryPath}. You can disable this error by setting the failGlob parameter to false in the options of autoload plugin`
|