✨ create startup process
create a startup process before the main elysia process starts. aims to check the main program such as env check and so on.
This commit is contained in:
@ -1,5 +1,8 @@
|
|||||||
import { Elysia } from "elysia";
|
import { validateEnv } from "./utils/startups/validateEnv";
|
||||||
import { routes } from "./routes";
|
validateEnv();
|
||||||
|
|
||||||
|
const { Elysia } = await import("elysia");
|
||||||
|
const { routes } = await import("./routes");
|
||||||
|
|
||||||
const app = new Elysia().use(routes).listen(process.env.PORT || 3000);
|
const app = new Elysia().use(routes).listen(process.env.PORT || 3000);
|
||||||
|
|
||||||
|
|||||||
37
src/utils/startups/validateEnv.ts
Normal file
37
src/utils/startups/validateEnv.ts
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
import fs from "fs";
|
||||||
|
|
||||||
|
export const validateEnv = () => {
|
||||||
|
if (!fs.existsSync(".env")) {
|
||||||
|
if (fs.existsSync(".env.example")) {
|
||||||
|
console.error("⚠️ .env file not found");
|
||||||
|
console.warn("📝 Creating .env file from .env.example, please wait...");
|
||||||
|
fs.copyFileSync(".env.example", ".env");
|
||||||
|
console.warn(
|
||||||
|
"🖊️ .env file successfully created please fill in the value in each key needed"
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
|
} else {
|
||||||
|
console.error(
|
||||||
|
`❌ Can't validate environment variable because can't find .env.example file. seems to be missing files please re-pull with “git pull main”`
|
||||||
|
);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const exampleKeys = fs
|
||||||
|
.readFileSync(".env.example", "utf-8")
|
||||||
|
.split("\n")
|
||||||
|
.map((line) => line.split("=")[0].trim())
|
||||||
|
.filter((key) => key && !key.startsWith("#"));
|
||||||
|
|
||||||
|
const missingKeys = exampleKeys.filter(
|
||||||
|
(key) => !process.env[key] || process.env[key].trim() === ""
|
||||||
|
);
|
||||||
|
|
||||||
|
if (missingKeys.length > 0) {
|
||||||
|
console.error("❌ ENV error - missing key:");
|
||||||
|
missingKeys.forEach((k) => console.error(` - ${k}`));
|
||||||
|
console.error(`check your .env file and make sure all keys are filled in`);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user