From 90c24ef9ec843b8a01661d0d342cbb1fcb053c08 Mon Sep 17 00:00:00 2001 From: sauravniraula Date: Tue, 13 May 2025 04:08:01 +0545 Subject: [PATCH] Removes: nextjs server requiring npx to run on build --- app/main.ts | 11 ++++-- app/utils/servers.ts | 62 +++++++++++++++++++----------- package-lock.json | 90 +++++++++++++++++++++++++++++++++++++++++++- package.json | 3 +- 4 files changed, 137 insertions(+), 29 deletions(-) diff --git a/app/main.ts b/app/main.ts index 41325b25..72994e9f 100644 --- a/app/main.ts +++ b/app/main.ts @@ -6,10 +6,11 @@ import { startFastApiServer, startNextJsServer } from "./utils/servers"; import { ChildProcessByStdio } from "child_process"; import { baseDir, fastapiDir, isDev, localhost, nextjsDir, tempDir, userConfigPath, userDataDir } from "./utils/constants"; import { setupIpcHandlers } from "./ipc"; +import url from "url"; var win: BrowserWindow | undefined; var fastApiProcess: ChildProcessByStdio | undefined; -var nextjsProcess: ChildProcessByStdio | undefined; +var nextjsProcess: any; app.commandLine.appendSwitch('gtk-version', '3'); @@ -62,8 +63,12 @@ async function stopServers() { if (fastApiProcess?.pid) { await killProcess(fastApiProcess.pid); } - if (nextjsProcess?.pid) { - await killProcess(nextjsProcess.pid); + if (nextjsProcess) { + if (isDev) { + await killProcess(nextjsProcess.pid); + } else { + nextjsProcess.close(); + } } } diff --git a/app/utils/servers.ts b/app/utils/servers.ts index 2402bea7..a2e03193 100644 --- a/app/utils/servers.ts +++ b/app/utils/servers.ts @@ -1,6 +1,10 @@ import { spawn, exec } from "child_process"; import util from "util"; import { localhost } from "./constants"; +import http from "http"; + +// @ts-ignore +import handler from "serve-handler"; const execAsync = util.promisify(exec); @@ -45,31 +49,43 @@ export async function startNextJsServer( env: NextJsEnv, isDev: boolean, ) { - // Start NextJS development server - const startCommand = isDev ? [ - "npm", - ["run", "dev", "--", "-p", port.toString()], - ] : [ - "npx", - ["-y", "serve", "-p", port.toString()], - ]; + let nextjsProcess; + + if (isDev) { + // Start NextJS development server + nextjsProcess = spawn( + "npm", + ["run", "dev", "--", "-p", port.toString()], + { + cwd: directory, + stdio: ["inherit", "pipe", "pipe"], + env: { ...process.env, ...env }, + } + ); + nextjsProcess.stdout.on("data", (data: any) => { + console.log(`NextJS: ${data}`); + }); + nextjsProcess.stderr.on("data", (data: any) => { + console.error(`NextJS Error: ${data}`); + }); + } else { + // Start NextJS build server + nextjsProcess = startNextjsBuildServer(directory, port); + } - const nextjsProcess = spawn( - startCommand[0] as string, - startCommand[1] as string[], - { - cwd: directory, - stdio: ["inherit", "pipe", "pipe"], - env: { ...process.env, ...env }, - } - ); - nextjsProcess.stdout.on("data", (data: any) => { - console.log(`NextJS: ${data}`); - }); - nextjsProcess.stderr.on("data", (data: any) => { - console.error(`NextJS Error: ${data}`); - }); // Wait for NextJS server to start await execAsync(`npx wait-on ${localhost}:${port}`); return nextjsProcess; } + +async function startNextjsBuildServer(directory: string, port: number) { + const server = http.createServer((req, res) => { + return handler(req, res, { + public: directory, + cleanUrls: true, + }); + }); + + server.listen(port); + return server; +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 66f7b61f..12639648 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "dotenv": "^16.5.0", "electron-squirrel-startup": "^1.0.1", "puppeteer": "^24.8.2", + "serve-handler": "^6.1.6", "tailwindcss": "^4.1.5", "tree-kill": "^1.2.2" }, @@ -1526,7 +1527,6 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz", "integrity": "sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==", - "dev": true, "license": "MIT" }, "node_modules/bare-events": { @@ -1748,6 +1748,14 @@ "node": ">=12.0.0" } }, + "node_modules/bytes": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/bytes/-/bytes-3.0.0.tgz", + "integrity": "sha512-pMhOfFDPiv9t5jjIXkHosWmkSyQbvsgEVNkz0ERHbuLh2T/7j4Mqqpz523Fe8MVY89KC6Sh/QfS2sM+SjgFDcw==", + "engines": { + "node": ">= 0.8" + } + }, "node_modules/cacache": { "version": "16.1.3", "resolved": "https://registry.npmjs.org/cacache/-/cacache-16.1.3.tgz", @@ -2085,7 +2093,6 @@ "version": "0.0.1", "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", "integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==", - "dev": true, "license": "MIT" }, "node_modules/config-file-ts": { @@ -2146,6 +2153,14 @@ "node": ">=16 || 14 >=14.17" } }, + "node_modules/content-disposition": { + "version": "0.5.2", + "resolved": "https://registry.npmjs.org/content-disposition/-/content-disposition-0.5.2.tgz", + "integrity": "sha512-kRGRZw3bLlFISDBgwTSA1TMBFN6J6GWDeubmDE3AF+3+yXL8hTWv8r5rkLbqYXY4RjPk/EzHnClI3zQf1cFmHA==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/core-util-is": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", @@ -4654,6 +4669,11 @@ "node": ">=0.10.0" } }, + "node_modules/path-is-inside": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/path-is-inside/-/path-is-inside-1.0.2.tgz", + "integrity": "sha512-DUWJr3+ULp4zXmol/SZkFf3JGsS9/SIv+Y3Rt93/UjPpDpklB5f1er4O3POIbUuUJ3FXgqte2Q7SrU6zAqwk8w==" + }, "node_modules/path-key": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", @@ -4698,6 +4718,11 @@ "node": ">=16 || 14 >=14.17" } }, + "node_modules/path-to-regexp": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-3.3.0.tgz", + "integrity": "sha512-qyCH421YQPS2WFDxDjftfc1ZR5WKQzVzqsp4n9M2kQhVOo/ByahFoUNJfl58kOcEGfQ//7weFTDhm+ss8Ecxgw==" + }, "node_modules/pe-library": { "version": "0.4.1", "resolved": "https://registry.npmjs.org/pe-library/-/pe-library-0.4.1.tgz", @@ -4930,6 +4955,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/range-parser": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/range-parser/-/range-parser-1.2.0.tgz", + "integrity": "sha512-kA5WQoNVo4t9lNx2kQNFCxKeBl5IbbSNBl1M/tLkw9WCn+hxNBAW5Qh8gdhs63CJnhjJ2zQWFoqPJP2sK1AV5A==", + "engines": { + "node": ">= 0.6" + } + }, "node_modules/read-binary-file-arch": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/read-binary-file-arch/-/read-binary-file-arch-1.0.6.tgz", @@ -5146,6 +5179,59 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/serve-handler": { + "version": "6.1.6", + "resolved": "https://registry.npmjs.org/serve-handler/-/serve-handler-6.1.6.tgz", + "integrity": "sha512-x5RL9Y2p5+Sh3D38Fh9i/iQ5ZK+e4xuXRd/pGbM4D13tgo/MGwbttUk8emytcr1YYzBYs+apnUngBDFYfpjPuQ==", + "dependencies": { + "bytes": "3.0.0", + "content-disposition": "0.5.2", + "mime-types": "2.1.18", + "minimatch": "3.1.2", + "path-is-inside": "1.0.2", + "path-to-regexp": "3.3.0", + "range-parser": "1.2.0" + } + }, + "node_modules/serve-handler/node_modules/brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dependencies": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, + "node_modules/serve-handler/node_modules/mime-db": { + "version": "1.33.0", + "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.33.0.tgz", + "integrity": "sha512-BHJ/EKruNIqJf/QahvxwQZXKygOQ256myeN/Ew+THcAa5q+PjyTTMMeNQC4DZw5AwfvelsUrA6B67NKMqXDbzQ==", + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/serve-handler/node_modules/mime-types": { + "version": "2.1.18", + "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.18.tgz", + "integrity": "sha512-lc/aahn+t4/SWV/qcmumYjymLsWfN3ELhpmVuUFjgsORruuZPVSwAQryq+HHGvO/SI2KVX26bx+En+zhM8g8hQ==", + "dependencies": { + "mime-db": "~1.33.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/serve-handler/node_modules/minimatch": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.1.2.tgz", + "integrity": "sha512-J7p63hRiAjw1NDEww1W7i37+ByIrOWO5XQQAzZ3VOcL0PNybwpfmV/N05zFAzwQ9USyEcX6t3UO+K5aqBQOIHw==", + "dependencies": { + "brace-expansion": "^1.1.7" + }, + "engines": { + "node": "*" + } + }, "node_modules/shebang-command": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/shebang-command/-/shebang-command-2.0.0.tgz", diff --git a/package.json b/package.json index 2cb1d6c9..b139257f 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,8 @@ "dotenv": "^16.5.0", "electron-squirrel-startup": "^1.0.1", "puppeteer": "^24.8.2", + "serve-handler": "^6.1.6", "tailwindcss": "^4.1.5", "tree-kill": "^1.2.2" } -} \ No newline at end of file +}