elysia-autoload 0.2.4 → 1.0.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 +20 -1
- package/dist/index.cjs +29 -17
- package/dist/index.js +19 -17
- package/package.json +1 -1
package/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# elysia-autoload
|
2
2
|
|
3
|
-
Plugin for [Elysia](https://elysiajs.com/) which autoload all routes in directory and code-generate types for [Eden](https://elysiajs.com/eden/overview.html)
|
3
|
+
Plugin for [Elysia](https://elysiajs.com/) which autoload all routes in directory and code-generate types for [Eden](https://elysiajs.com/eden/overview.html) with [`Bun.build`](https://bun.sh/docs/bundler) support!
|
4
4
|
|
5
5
|
**Currently, Eden types generation is broken!!**
|
6
6
|
|
@@ -131,6 +131,25 @@ console.log(data);
|
|
131
131
|
|
132
132
|
Example of app with types code-generation you can see in [example](https://github.com/kravetsone/elysia-autoload/tree/main/example)
|
133
133
|
|
134
|
+
### Bun build usage
|
135
|
+
|
136
|
+
You can use this plugin with `Bun.build`, thanks to [esbuild-plugin-autoload](https://github.com/kravetsone/esbuild-plugin-autoload)!
|
137
|
+
|
138
|
+
```ts
|
139
|
+
// @filename: build.ts
|
140
|
+
import { autoload } from "esbuild-plugin-autoload"; // default import also supported
|
141
|
+
|
142
|
+
await Bun.build({
|
143
|
+
entrypoints: ["src/index.ts"],
|
144
|
+
outdir: "out",
|
145
|
+
plugins: [autoload()],
|
146
|
+
}).then(console.log);
|
147
|
+
```
|
148
|
+
|
149
|
+
Then, build it with `bun build.ts` and run with `bun out/index.ts`.
|
150
|
+
|
151
|
+
[Read more](https://github.com/kravetsone/esbuild-plugin-autoload)
|
152
|
+
|
134
153
|
### Usage of schema handler
|
135
154
|
|
136
155
|
```ts
|
package/dist/index.cjs
CHANGED
@@ -1,7 +1,9 @@
|
|
1
1
|
"use strict";
|
2
|
+
var __create = Object.create;
|
2
3
|
var __defProp = Object.defineProperty;
|
3
4
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
4
5
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
5
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
6
8
|
var __export = (target, all) => {
|
7
9
|
for (var name in all)
|
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
15
17
|
}
|
16
18
|
return to;
|
17
19
|
};
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
26
|
+
mod
|
27
|
+
));
|
18
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
19
29
|
|
20
30
|
// src/index.ts
|
@@ -23,18 +33,19 @@ __export(src_exports, {
|
|
23
33
|
autoload: () => autoload
|
24
34
|
});
|
25
35
|
module.exports = __toCommonJS(src_exports);
|
26
|
-
var import_node_fs = require("fs");
|
27
|
-
var import_node_path2 = require("path");
|
36
|
+
var import_node_fs = __toESM(require("fs"), 1);
|
37
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
28
38
|
var import_elysia = require("elysia");
|
29
39
|
|
30
40
|
// src/utils.ts
|
31
|
-
var import_node_path = require("path");
|
41
|
+
var import_node_path = __toESM(require("path"), 1);
|
32
42
|
function getPath(dir) {
|
33
|
-
if (
|
34
|
-
if (
|
35
|
-
|
43
|
+
if (import_node_path.default.isAbsolute(dir)) return dir;
|
44
|
+
if (import_node_path.default.isAbsolute(process.argv[1]))
|
45
|
+
return import_node_path.default.join(process.argv[1], "..", dir);
|
46
|
+
return import_node_path.default.join(process.cwd(), process.argv[1], "..", dir);
|
36
47
|
}
|
37
|
-
function transformToUrl(
|
48
|
+
function transformToUrl(path3) {
|
38
49
|
const replacements = [
|
39
50
|
// Clean the url extensions
|
40
51
|
{ regex: /\.(ts|tsx|js|jsx|mjs|cjs)$/u, replacement: "" },
|
@@ -56,14 +67,14 @@ function transformToUrl(path) {
|
|
56
67
|
// remove index from end of path
|
57
68
|
{ regex: /\/?index$/, replacement: "" }
|
58
69
|
];
|
59
|
-
let url =
|
70
|
+
let url = path3;
|
60
71
|
for (const { regex, replacement } of replacements) {
|
61
72
|
url = url.replace(regex, replacement);
|
62
73
|
}
|
63
74
|
return url;
|
64
75
|
}
|
65
|
-
function getParamsCount(
|
66
|
-
return
|
76
|
+
function getParamsCount(path3) {
|
77
|
+
return path3.match(/\[(.*?)\]/gu)?.length || 0;
|
67
78
|
}
|
68
79
|
function sortByNestedParams(routes) {
|
69
80
|
return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
|
@@ -77,11 +88,12 @@ function fixSlashes(prefix) {
|
|
77
88
|
var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
|
78
89
|
var TYPES_TYPENAME_DEFAULT = "Routes";
|
79
90
|
async function autoload(options = {}) {
|
91
|
+
const fileSources = {};
|
80
92
|
const { pattern, dir, prefix, schema, types } = options;
|
81
93
|
const directoryPath = getPath(dir || "./routes");
|
82
|
-
if (!
|
94
|
+
if (!import_node_fs.default.existsSync(directoryPath))
|
83
95
|
throw new Error(`Directory ${directoryPath} doesn't exists`);
|
84
|
-
if (!
|
96
|
+
if (!import_node_fs.default.statSync(directoryPath).isDirectory())
|
85
97
|
throw new Error(`${directoryPath} isn't a directory`);
|
86
98
|
const plugin = new import_elysia.Elysia({
|
87
99
|
name: "elysia-autoload",
|
@@ -100,13 +112,13 @@ async function autoload(options = {}) {
|
|
100
112
|
})
|
101
113
|
);
|
102
114
|
const paths = [];
|
103
|
-
for await (const
|
104
|
-
const fullPath =
|
115
|
+
for await (const filePath of sortByNestedParams(files)) {
|
116
|
+
const fullPath = import_node_path2.default.join(directoryPath, filePath);
|
105
117
|
const file = await import(fullPath);
|
106
118
|
if (!file.default)
|
107
|
-
throw new Error(`${
|
108
|
-
const url = transformToUrl(
|
109
|
-
const groupOptions = schema ? schema({ path, url }) : {};
|
119
|
+
throw new Error(`${filePath} doesn't provide default export`);
|
120
|
+
const url = transformToUrl(filePath);
|
121
|
+
const groupOptions = schema ? schema({ path: filePath, url }) : {};
|
110
122
|
plugin.group(url, groupOptions, file.default);
|
111
123
|
if (types) paths.push(fullPath.replace(directoryPath, ""));
|
112
124
|
}
|
package/dist/index.js
CHANGED
@@ -1,18 +1,19 @@
|
|
1
1
|
// src/index.ts
|
2
|
-
import
|
3
|
-
import
|
2
|
+
import fs from "node:fs";
|
3
|
+
import path2 from "node:path";
|
4
4
|
import {
|
5
5
|
Elysia
|
6
6
|
} from "elysia";
|
7
7
|
|
8
8
|
// src/utils.ts
|
9
|
-
import
|
9
|
+
import path from "node:path";
|
10
10
|
function getPath(dir) {
|
11
|
-
if (isAbsolute(dir)) return dir;
|
12
|
-
if (isAbsolute(process.argv[1]))
|
13
|
-
|
11
|
+
if (path.isAbsolute(dir)) return dir;
|
12
|
+
if (path.isAbsolute(process.argv[1]))
|
13
|
+
return path.join(process.argv[1], "..", dir);
|
14
|
+
return path.join(process.cwd(), process.argv[1], "..", dir);
|
14
15
|
}
|
15
|
-
function transformToUrl(
|
16
|
+
function transformToUrl(path3) {
|
16
17
|
const replacements = [
|
17
18
|
// Clean the url extensions
|
18
19
|
{ regex: /\.(ts|tsx|js|jsx|mjs|cjs)$/u, replacement: "" },
|
@@ -34,14 +35,14 @@ function transformToUrl(path) {
|
|
34
35
|
// remove index from end of path
|
35
36
|
{ regex: /\/?index$/, replacement: "" }
|
36
37
|
];
|
37
|
-
let url =
|
38
|
+
let url = path3;
|
38
39
|
for (const { regex, replacement } of replacements) {
|
39
40
|
url = url.replace(regex, replacement);
|
40
41
|
}
|
41
42
|
return url;
|
42
43
|
}
|
43
|
-
function getParamsCount(
|
44
|
-
return
|
44
|
+
function getParamsCount(path3) {
|
45
|
+
return path3.match(/\[(.*?)\]/gu)?.length || 0;
|
45
46
|
}
|
46
47
|
function sortByNestedParams(routes) {
|
47
48
|
return routes.sort((a, b) => getParamsCount(a) - getParamsCount(b));
|
@@ -55,11 +56,12 @@ function fixSlashes(prefix) {
|
|
55
56
|
var TYPES_OUTPUT_DEFAULT = "./routes-types.ts";
|
56
57
|
var TYPES_TYPENAME_DEFAULT = "Routes";
|
57
58
|
async function autoload(options = {}) {
|
59
|
+
const fileSources = {};
|
58
60
|
const { pattern, dir, prefix, schema, types } = options;
|
59
61
|
const directoryPath = getPath(dir || "./routes");
|
60
|
-
if (!existsSync(directoryPath))
|
62
|
+
if (!fs.existsSync(directoryPath))
|
61
63
|
throw new Error(`Directory ${directoryPath} doesn't exists`);
|
62
|
-
if (!statSync(directoryPath).isDirectory())
|
64
|
+
if (!fs.statSync(directoryPath).isDirectory())
|
63
65
|
throw new Error(`${directoryPath} isn't a directory`);
|
64
66
|
const plugin = new Elysia({
|
65
67
|
name: "elysia-autoload",
|
@@ -78,13 +80,13 @@ async function autoload(options = {}) {
|
|
78
80
|
})
|
79
81
|
);
|
80
82
|
const paths = [];
|
81
|
-
for await (const
|
82
|
-
const fullPath =
|
83
|
+
for await (const filePath of sortByNestedParams(files)) {
|
84
|
+
const fullPath = path2.join(directoryPath, filePath);
|
83
85
|
const file = await import(fullPath);
|
84
86
|
if (!file.default)
|
85
|
-
throw new Error(`${
|
86
|
-
const url = transformToUrl(
|
87
|
-
const groupOptions = schema ? schema({ path, url }) : {};
|
87
|
+
throw new Error(`${filePath} doesn't provide default export`);
|
88
|
+
const url = transformToUrl(filePath);
|
89
|
+
const groupOptions = schema ? schema({ path: filePath, url }) : {};
|
88
90
|
plugin.group(url, groupOptions, file.default);
|
89
91
|
if (types) paths.push(fullPath.replace(directoryPath, ""));
|
90
92
|
}
|