Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add NodeJS version #23

Merged
merged 11 commits into from
Dec 28, 2024
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Install Node
uses: actions/setup-node@v4
with:
node-version: "20.x"
node-version: "22"
registry-url: "https://registry.npmjs.org"
scope: "@gramio"

Expand Down
Binary file modified bun.lockb
Binary file not shown.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,15 @@
"@elysiajs/eden": "^1.1.3",
"@elysiajs/swagger": "^1.1.1",
"@types/bun": "^1.1.9",
"@types/node": "^22.7.4",
"elysia": "^1.1.13",
"pkgroll": "^2.5.0",
"typescript": "^5.6.2"
},
"peerDependencies": {
"elysia": "^1.1.0"
},
"engines": {
"node": ">=22"
}
}
59 changes: 30 additions & 29 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import fs from "node:fs";
import path from "node:path";
import { pathToFileURL } from "node:url";
import {
type BaseMacro,
Elysia,
Expand Down Expand Up @@ -117,24 +118,23 @@ export async function autoload(options: AutoloadOptions = {}) {
},
});

const glob = new Bun.Glob(pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}");
const globPattern = pattern || "**/*.{ts,tsx,js,jsx,mjs,cjs}";
const globOptions = { cwd: directoryPath };

const files = await Array.fromAsync(
glob.scan({
cwd: directoryPath,
}),
);
const files =
typeof Bun === "undefined"
? fs.globSync(globPattern, globOptions)
: await Array.fromAsync(new Bun.Glob(globPattern).scan(globOptions));
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)) {
for (const filePath of sortByNestedParams(files)) {
const fullPath = path.join(directoryPath, filePath);

const file = await import(fullPath);
const file = await import(pathToFileURL(fullPath).href);

const importName =
typeof getImportName === "string" ? getImportName : getImportName(file);
Expand Down Expand Up @@ -179,26 +179,27 @@ export async function autoload(options: AutoloadOptions = {}) {
)}";`,
);

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"),
);
const input = [
`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");
if (typeof Bun === "undefined") {
fs.writeFileSync(outputAbsolutePath, input);
} else {
await Bun.write(outputAbsolutePath, input);
}
}
}

Expand Down