File tree Expand file tree Collapse file tree 2 files changed +20
-4
lines changed
Expand file tree Collapse file tree 2 files changed +20
-4
lines changed Original file line number Diff line number Diff 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 ( ) => {
Original file line number Diff line number Diff 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} ) ) ;
You can’t perform that action at this time.
0 commit comments