-
-
Notifications
You must be signed in to change notification settings - Fork 616
add automatic port detection for Tauri dev server #695
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| const net = require('net'); | ||
| const { spawn } = require('child_process'); | ||
|
|
||
| // Range of ports to check (Vite's default + alternatives) | ||
| const PORT_RANGE_START = 5173; | ||
| const PORT_RANGE_END = 5182; | ||
|
|
||
| /** | ||
| * Check if a port is available | ||
| * @param {number} port - Port to check | ||
| * @returns {Promise<boolean>} - True if port is available | ||
| */ | ||
| function isPortAvailable(port) { | ||
| return new Promise((resolve) => { | ||
| const server = net.createServer(); | ||
|
|
||
| server.once('error', (err) => { | ||
| if (err.code === 'EADDRINUSE') { | ||
| resolve(false); | ||
| } else { | ||
| console.warn(`Unexpected error checking port ${port}:`, err.message); | ||
| resolve(false); | ||
| } | ||
| }); | ||
|
|
||
| server.once('listening', () => { | ||
| server.close(); | ||
| resolve(true); | ||
| }); | ||
|
|
||
| server.listen(port, 'localhost'); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Find the first available port in range | ||
| * @returns {Promise<number|null>} - Available port or null | ||
| */ | ||
| async function findAvailablePort() { | ||
| for (let port = PORT_RANGE_START; port <= PORT_RANGE_END; port++) { | ||
| if (await isPortAvailable(port)) { | ||
| return port; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Main function | ||
| */ | ||
| async function main() { | ||
| console.log('\n🔍 Detecting available port for Tauri development...'); | ||
|
|
||
| const port = await findAvailablePort(); | ||
|
|
||
| if (!port) { | ||
| console.error( | ||
| `\n❌ No available ports found in range ${PORT_RANGE_START}-${PORT_RANGE_END}`, | ||
| ); | ||
| console.error( | ||
| 'Please free up a port or manually configure the port in tauri.conf.json', | ||
| ); | ||
| process.exit(1); | ||
|
Comment on lines
+56
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Update the error message to reflect the current approach. The error message suggests manually configuring Apply this diff: if (!port) {
console.error(
`\n❌ No available ports found in range ${PORT_RANGE_START}-${PORT_RANGE_END}`,
);
console.error(
- 'Please free up a port or manually configure the port in tauri.conf.json',
+ 'Please close other dev servers or expand the PORT_RANGE in this script',
);
process.exit(1);
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| console.log(`✅ Found available port: ${port}`); | ||
| console.log('🚀 Starting Tauri dev server...\n'); | ||
|
|
||
| // Start Tauri dev with CLI flags (no config file modification) | ||
| const tauri = spawn( | ||
| 'npm', | ||
| [ | ||
| 'run', | ||
| 'tauri', | ||
| 'dev', | ||
| '--', | ||
| '--dev-url', | ||
| `http://localhost:${port}`, | ||
| '--port', | ||
| port.toString(), | ||
| ], | ||
| { | ||
| stdio: 'inherit', | ||
| shell: true, | ||
| env: { | ||
| ...process.env, | ||
| VITE_PORT: port.toString(), | ||
| TAURI_CLI_PORT: port.toString(), | ||
| TAURI_DEV_HOST: 'localhost', | ||
| }, | ||
| }, | ||
| ); | ||
|
|
||
| tauri.on('error', (error) => { | ||
| console.error('Failed to start Tauri dev server:', error); | ||
| process.exit(1); | ||
| }); | ||
|
|
||
| tauri.on('exit', (code) => { | ||
| process.exit(code || 0); | ||
| }); | ||
| } | ||
|
|
||
| main().catch((error) => { | ||
| console.error('Error:', error); | ||
| process.exit(1); | ||
| }); | ||
Uh oh!
There was an error while loading. Please reload this page.