Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"test:watch": "jest --watch",
"preview": "vite preview",
"tauri": "tauri",
"tauri:dev": "node scripts/tauri-dev-auto-port.js",
"setup:linux": "cd scripts && ./setup_env.sh",
"lint:check": "eslint --max-warnings 0 --config .eslintrc.json .",
"lint:fix": "eslint --max-warnings 0 --config .eslintrc.json . --fix",
Expand Down
109 changes: 109 additions & 0 deletions frontend/scripts/tauri-dev-auto-port.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
const net = require('net');
const fs = require('fs');
const path = require('path');

// 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 {
resolve(false);
}
});

server.once('listening', () => {
server.close();
resolve(true);
});

server.listen(port);
});
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

/**
* 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;
}

/**
* Update tauri.conf.json with the selected port
* @param {number} port - Port to use
*/
function updateTauriConfig(port) {
const configPath = path.join(__dirname, '..', 'src-tauri', 'tauri.conf.json');

try {
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));

// Update devUrl and beforeDevCommand
config.build.devUrl = `http://localhost:${port}`;
config.build.beforeDevCommand = `npm run dev -- --port ${port}`;

fs.writeFileSync(configPath, JSON.stringify(config, null, 2));
console.log(`✓ Updated Tauri config to use port ${port}`);
} catch (error) {
console.error('Error updating Tauri config:', error);
process.exit(1);
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

/**
* Main function
*/
async function main() {
console.log('\nDetecting 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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Update the error message to reflect the current approach.

The error message suggests manually configuring tauri.conf.json, but the PR's goal is to avoid modifying that file. Consider updating the message to suggest alternatives like closing other dev servers or adjusting the port range in this script.

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
In frontend/scripts/tauri-dev-auto-port.js around lines 56 to 63, the error text
incorrectly tells users to edit tauri.conf.json; update the messages to reflect
the new non-invasive approach by telling users to close other running dev
servers or adjust the PORT_RANGE_START/PORT_RANGE_END in this script (or run
with a different port), and replace the two console.error calls accordingly so
they guide users to those alternatives instead of editing tauri.conf.json.

}

console.log(`Found available port: ${port}`);
updateTauriConfig(port);

// Set environment variables
process.env.VITE_PORT = port.toString();
process.env.TAURI_MODE = 'development';

console.log('Starting Tauri dev server...\n');

// Start Tauri dev
const { spawn } = require('child_process');
const tauri = spawn('npm', ['run', 'tauri', 'dev'], {
stdio: 'inherit',
shell: true,
env: { ...process.env, VITE_PORT: port.toString() }
});

tauri.on('exit', (code) => {
process.exit(code);
});
}

main().catch((error) => {
console.error('Error:', error);
process.exit(1);
});