fix: load .env before imports and improve dev startup
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import "./load-env.js";
|
||||
import cors from "cors";
|
||||
import dotenv from "dotenv";
|
||||
import express from "express";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { MulterError } from "multer";
|
||||
import { buildsRouter } from "./routes/builds.js";
|
||||
|
||||
dotenv.config({ path: path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../.env") });
|
||||
|
||||
const app = express();
|
||||
const port = Number(process.env.PORT ?? "3001");
|
||||
|
||||
@@ -19,6 +18,28 @@ app.get("/api/health", (_req, res) => {
|
||||
|
||||
app.use("/api/builds", buildsRouter);
|
||||
|
||||
app.use((err: unknown, _req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||
if (res.headersSent) {
|
||||
next(err);
|
||||
return;
|
||||
}
|
||||
|
||||
if (err instanceof MulterError) {
|
||||
const status = err.code === "LIMIT_FILE_SIZE" ? 413 : 400;
|
||||
res.status(status).json({ error: err.message });
|
||||
return;
|
||||
}
|
||||
|
||||
if (err instanceof Error) {
|
||||
console.error(err);
|
||||
res.status(500).json({ error: err.message });
|
||||
return;
|
||||
}
|
||||
|
||||
console.error(err);
|
||||
res.status(500).json({ error: "Internal server error" });
|
||||
});
|
||||
|
||||
const webDist = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../web/dist");
|
||||
app.use(express.static(webDist));
|
||||
app.get("*", (_req, res, next) => {
|
||||
@@ -31,6 +52,6 @@ app.get("*", (_req, res, next) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.listen(port, () => {
|
||||
console.log(`Web2App server listening on http://localhost:${port}`);
|
||||
app.listen(port, "127.0.0.1", () => {
|
||||
console.log(`Web2App server listening on http://127.0.0.1:${port}`);
|
||||
});
|
||||
|
||||
13
server/load-env.ts
Normal file
13
server/load-env.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import dotenv from "dotenv";
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
const serverDir = path.dirname(fileURLToPath(import.meta.url));
|
||||
const rootEnv = path.resolve(serverDir, "../.env");
|
||||
|
||||
if (fs.existsSync(rootEnv)) {
|
||||
dotenv.config({ path: rootEnv });
|
||||
} else {
|
||||
dotenv.config();
|
||||
}
|
||||
@@ -4,8 +4,12 @@
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"postinstall": "node -e \"try{require('better-sqlite3')}catch{process.exit(1)}\" || npm rebuild better-sqlite3",
|
||||
"predev": "npm rebuild better-sqlite3",
|
||||
"dev": "tsx watch index.ts",
|
||||
"prebuild": "npm rebuild better-sqlite3",
|
||||
"build": "tsc",
|
||||
"prestart": "npm rebuild better-sqlite3",
|
||||
"start": "node dist/index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import "../load-env.js";
|
||||
import { Octokit } from "octokit";
|
||||
|
||||
const owner = process.env.GITHUB_OWNER ?? "";
|
||||
const repo = process.env.GITHUB_REPO ?? "";
|
||||
const token = process.env.GITHUB_TOKEN ?? "";
|
||||
function getGitHubConfig() {
|
||||
return {
|
||||
owner: (process.env.GITHUB_OWNER ?? "").trim(),
|
||||
repo: (process.env.GITHUB_REPO ?? "").trim(),
|
||||
token: (process.env.GITHUB_TOKEN ?? "").trim(),
|
||||
};
|
||||
}
|
||||
|
||||
function getOctokit(): Octokit {
|
||||
const { owner, repo, token } = getGitHubConfig();
|
||||
if (!owner || !repo || !token) {
|
||||
throw new Error(
|
||||
"Missing GITHUB_OWNER, GITHUB_REPO, or GITHUB_TOKEN environment variables",
|
||||
@@ -14,10 +20,12 @@ function getOctokit(): Octokit {
|
||||
}
|
||||
|
||||
export function getRepoInfo() {
|
||||
const { owner, repo } = getGitHubConfig();
|
||||
return { owner, repo };
|
||||
}
|
||||
|
||||
export async function uploadSiteZip(jobId: string, zipBuffer: Buffer): Promise<void> {
|
||||
const { owner, repo } = getGitHubConfig();
|
||||
const octokit = getOctokit();
|
||||
const path = `builds/${jobId}/site.zip`;
|
||||
const content = zipBuffer.toString("base64");
|
||||
@@ -55,6 +63,7 @@ export async function triggerBuildWorkflow(input: {
|
||||
appName: string;
|
||||
appIdentifier: string;
|
||||
}): Promise<number> {
|
||||
const { owner, repo } = getGitHubConfig();
|
||||
const octokit = getOctokit();
|
||||
|
||||
await octokit.rest.actions.createWorkflowDispatch({
|
||||
@@ -95,6 +104,7 @@ export async function triggerBuildWorkflow(input: {
|
||||
}
|
||||
|
||||
export async function getWorkflowRun(runId: number) {
|
||||
const { owner, repo } = getGitHubConfig();
|
||||
const octokit = getOctokit();
|
||||
const { data } = await octokit.rest.actions.getWorkflowRun({
|
||||
owner,
|
||||
@@ -108,6 +118,7 @@ export async function getReleaseAssets(jobId: string): Promise<{
|
||||
windowsUrl: string | null;
|
||||
androidUrl: string | null;
|
||||
}> {
|
||||
const { owner, repo } = getGitHubConfig();
|
||||
const octokit = getOctokit();
|
||||
const tag = `build-${jobId}`;
|
||||
|
||||
@@ -146,6 +157,7 @@ export async function getReleaseAssets(jobId: string): Promise<{
|
||||
}
|
||||
|
||||
export function getActionsRunUrl(runId: number | null): string | null {
|
||||
const { owner, repo } = getGitHubConfig();
|
||||
if (!runId || !owner || !repo) return null;
|
||||
return `https://github.com/${owner}/${repo}/actions/runs/${runId}`;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user