Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions code/core/src/core-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ export {
fullTestProviderStore as internal_fullTestProviderStore,
universalTestProviderStore as internal_universalTestProviderStore,
} from './stores/test-provider';

export { getServerPort } from './utils/server-address';
14 changes: 11 additions & 3 deletions code/lib/create-storybook/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import {
PackageManagerName,
executeCommand,
} from 'storybook/internal/common';
import { withTelemetry } from 'storybook/internal/core-server';
import { getServerPort, withTelemetry } from 'storybook/internal/core-server';
import { logTracker, logger } from 'storybook/internal/node-logger';
import { ErrorCollector } from 'storybook/internal/telemetry';

import {
executeAddonConfiguration,
Expand Down Expand Up @@ -197,13 +196,22 @@ async function runStorybookDev(result: {
flags.push('--initial-path=/onboarding');
}

// Check if default port 6006 is available
const defaultPort = 6006;
const availablePort = await getServerPort(defaultPort);
const useAlternativePort = availablePort !== defaultPort;

if (useAlternativePort) {
flags.push(`--port=${availablePort}`);
}

flags.push('--quiet');

// instead of calling 'dev' automatically, we spawn a subprocess so that it gets
// executed directly in the user's project directory. This avoid potential issues
// with packages running in npxs' node_modules
const [command, ...args] = [...storybookCommand.split(' '), ...flags];
executeCommand({
await executeCommand({
command: command,
args,
stdio: 'inherit',
Expand Down