diff --git a/lib/nextron-dev.ts b/lib/nextron-dev.ts index 86dce798..8f1fcf9a 100755 --- a/lib/nextron-dev.ts +++ b/lib/nextron-dev.ts @@ -4,6 +4,7 @@ import webpack from 'webpack' import * as logger from './logger' import { getNextronConfig } from './configs/getNextronConfig' import { config } from './configs/webpack.config.development' +import { waitForPort } from 'get-port-please' import type { ChildProcess } from 'child_process' const args = arg({ @@ -42,7 +43,8 @@ if (args['--inspect']) { const nextronConfig = getNextronConfig() const rendererPort = args['--renderer-port'] || 8888 -const startupDelay = nextronConfig.startupDelay || args['--startup-delay'] || 0 +const startupDelay = + nextronConfig.startupDelay || args['--startup-delay'] || 10_000 let electronOptions = args['--electron-options'] || '' if (!electronOptions.includes('--remote-debugging-port')) { @@ -115,9 +117,16 @@ const execaOptions: execa.Options = { rendererProcess = startRendererProcess() // wait until renderer process is ready - await new Promise((resolve) => - setTimeout(() => resolve(), startupDelay) - ) + await waitForPort(rendererPort, { + delay: 500, + retries: startupDelay / 500, + }).catch(() => { + logger.error( + `Failed to start renderer process with port ${rendererPort} in ${startupDelay}ms` + ) + killWholeProcess() + process.exit(1) + }) // wait until main process is ready await new Promise((resolve) => { diff --git a/package.json b/package.json index 5712348b..0c98bbec 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "chalk": "4.1.2", "execa": "5.1.1", "fs-extra": "11.2.0", + "get-port-please": "^3.1.2", "terser-webpack-plugin": "5.3.10", "tsconfig-paths-webpack-plugin": "4.1.0", "webpack": "5.92.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 1dc6aa26..d4dba43c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -50,6 +50,9 @@ importers: fs-extra: specifier: 11.2.0 version: 11.2.0 + get-port-please: + specifier: ^3.1.2 + version: 3.1.2 terser-webpack-plugin: specifier: 5.3.10 version: 5.3.10(webpack@5.92.0) @@ -1943,6 +1946,9 @@ packages: resolution: {integrity: sha512-5uYhsJH8VJBTv7oslg4BznJYhDoRI6waYCxMmCdnTrcCrHA/fCFKoTFz2JKKE0HdDFUF7/oQuhzumXJK7paBRQ==} engines: {node: '>= 0.4'} + get-port-please@3.1.2: + resolution: {integrity: sha512-Gxc29eLs1fbn6LQ4jSU4vXjlwyZhF5HsGuMAa7gqBP4Rw4yxxltyDUuF5MBclFzDTXO+ACchGQoeela4DSfzdQ==} + get-stream@6.0.1: resolution: {integrity: sha512-ts6Wi+2j3jQjqi70w5AlN8DFnkSwC+MqmxEzdEALB2qXZYV3X/b1CTfgPLGJNMeAWxdPfU8FO1ms3NUfaHCPYg==} engines: {node: '>=10'} @@ -5458,6 +5464,8 @@ snapshots: has-symbols: 1.0.3 hasown: 2.0.2 + get-port-please@3.1.2: {} + get-stream@6.0.1: {} get-stream@8.0.1: {}