Skip to content

Commit 101fb86

Browse files
committed
use next available port if the default 3000 is taken
1 parent 76f1dce commit 101fb86

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

bin/dory.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ const commands = {
9999
console.log('👀 Starting your docs preview...');
100100
const currentDir = process.cwd();
101101
const distDir = resolve(currentDir, 'dist');
102-
const port = process.env.PORT || 3000;
102+
let port = parseInt(process.env.PORT || '3000', 10);
103103

104104
const serve = sirv(distDir, {
105105
dev: false,
@@ -113,9 +113,23 @@ const commands = {
113113
serve(req, res);
114114
});
115115

116-
server.listen(port, () => {
117-
console.log(`🚀 Your docs are live at http://localhost:${port}`);
118-
});
116+
const tryPort = (currentPort: number) => {
117+
server.listen(currentPort)
118+
.on('listening', () => {
119+
console.log(`🚀 Your docs are live at http://localhost:${currentPort}`);
120+
})
121+
.on('error', (err: any) => {
122+
if (err.code === 'EADDRINUSE') {
123+
console.log(`⚠️ Port ${currentPort} is busy, trying ${currentPort + 1}...`);
124+
tryPort(currentPort + 1);
125+
} else {
126+
console.error('❌ Failed to start server:', err.message);
127+
process.exit(1);
128+
}
129+
});
130+
};
131+
132+
tryPort(port);
119133
},
120134

121135
'verify:content': async () => {

vite.config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,7 @@ export default defineConfig(({ command }) => ({
3030
publicDir: "docs",
3131
server: {
3232
allowedHosts: true,
33+
port: 3000,
34+
strictPort: false,
3335
},
3436
}));

0 commit comments

Comments
 (0)