Skip to content
4 changes: 4 additions & 0 deletions code/lib/create-storybook/src/initiate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ const getCliIntegrationFromAncestry =

vi.mock('storybook/internal/telemetry');

vi.mock('storybook/internal/core-server', () => ({
getServerPort: vi.fn().mockResolvedValue(6006),
}));

describe('getStorybookVersionFromAncestry', () => {
it('possible storybook path', () => {
const ancestry = [{ command: 'node' }, { command: 'storybook@7.0.0' }, { command: 'npm' }];
Expand Down
50 changes: 25 additions & 25 deletions code/lib/create-storybook/src/initiate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,45 +176,45 @@ async function runStorybookDev(result: {
try {
const supportsOnboarding = FeatureCompatibilityService.supportsOnboarding(projectType);

const flags = [];
const parts = storybookCommand.split(' ');

if (packageManager.type === 'npm') {
flags.push('--silent');
parts.push('--silent');
}

// npm needs extra -- to pass flags to the command
// in the case of Angular, we are calling `ng run` which doesn't need the extra `--`
const doesNeedExtraDash =
packageManager.type === PackageManagerName.NPM ||
packageManager.type === PackageManagerName.BUN;
const supportSbFlags = projectType !== ProjectType.ANGULAR;

if (doesNeedExtraDash && projectType !== ProjectType.ANGULAR) {
flags.push('--');
}
if (supportSbFlags) {
// npm needs extra -- to pass flags to the command
// in the case of Angular, we are calling `ng run` which doesn't need the extra `--`
const doesNeedExtraDash =
packageManager.type === PackageManagerName.NPM ||
packageManager.type === PackageManagerName.BUN;

if (supportsOnboarding && shouldOnboard) {
flags.push('--initial-path=/onboarding');
}
if (doesNeedExtraDash) {
parts.push('--');
}

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

const portFlag = flags.findIndex((flag) => flag.startsWith('-p '));
if (useAlternativePort) {
parts.push(`-p`, `${availablePort}`);

if (useAlternativePort && portFlag === -1) {
flags.push(`-p ${availablePort}`);
} else if (useAlternativePort && portFlag !== -1) {
flags[portFlag] = `-p ${availablePort}`;
}
if (supportsOnboarding && shouldOnboard) {
parts.push('--initial-path=/onboarding');
}

flags.push('--quiet');
parts.push('--quiet');
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
}

// 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];
const [command, ...args] = [...parts];

await executeCommand({
command: command,
args,
Expand Down