Skip to content

Commit

Permalink
fix: get port fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed Feb 24, 2024
1 parent 0d3b469 commit 188e7b9
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions packages/slidev/node/cli.ts
Expand Up @@ -16,7 +16,7 @@ import equal from 'fast-deep-equal'
import { verifyConfig } from '@slidev/parser'
import { injectPreparserExtensionLoader } from '@slidev/parser/fs'
import { uniq } from '@antfu/utils'
import { checkPort } from 'get-port-please'
import { getPort } from 'get-port-please'
import { version } from '../package.json'
import { createServer } from './server'
import type { ResolvedSlidevOptions } from './options'
Expand Down Expand Up @@ -117,15 +117,21 @@ cli.command(
if (server)
await server.close()
const options = await resolveOptions({ entry, remote, theme, inspect }, 'dev')
port = userPort || await findFreePort(3030)
const host = remote !== undefined ? bind : 'localhost'
port = userPort || await getPort({
port: 3030,
random: false,
portRange: [3030, 4000],
host,
})
server = (await createServer(
options,
{
server: {
port,
strictPort: true,
open,
host: remote !== undefined ? bind : 'localhost',
host,
// @ts-expect-error Vite <= 4
force,
},
Expand Down Expand Up @@ -396,7 +402,7 @@ cli.command(
async (args) => {
const { entry, theme } = args
const { exportSlides, getExportOptions } = await import('./export')
const port = await findFreePort(12445)
const port = await getPort(12445)

for (const entryFile of entry as unknown as string) {
const options = await resolveOptions({ entry: entryFile, theme }, 'export')
Expand Down Expand Up @@ -447,7 +453,7 @@ cli.command(
timeout,
}) => {
const { exportNotes } = await import('./export')
const port = await findFreePort(12445)
const port = await getPort(12445)

for (const entryFile of entry as unknown as string[]) {
const options = await resolveOptions({ entry: entryFile }, 'export')
Expand Down Expand Up @@ -602,9 +608,3 @@ function printInfo(
return lastRemoteUrl
}
}

async function findFreePort(start: number): Promise<number> {
if (await checkPort(start) !== false)
return start
return findFreePort(start + 1)
}

0 comments on commit 188e7b9

Please sign in to comment.