diff --git a/lib/core/src/server/build-dev.js b/lib/core/src/server/build-dev.js index 54762cb6429f..8ee8062ac87e 100644 --- a/lib/core/src/server/build-dev.js +++ b/lib/core/src/server/build-dev.js @@ -16,6 +16,8 @@ import semver from 'semver'; import { stripIndents } from 'common-tags'; import Table from 'cli-table3'; import prettyTime from 'pretty-hrtime'; +import inquirer from 'inquirer'; +import detectFreePort from 'detect-port'; import storybook from './dev-server'; import { getDevCli } from './cli'; @@ -35,6 +37,12 @@ const writeStats = async (name, stats) => { ); }; +const getFreePort = port => + detectFreePort(port).catch(error => { + logger.error(error); + process.exit(-1); + }); + async function getServer(app, options) { if (!options.https) { return http.createServer(app); @@ -238,7 +246,24 @@ function openInBrowser(address) { export async function buildDevStandalone(options) { try { - const { port, host, extendServer } = options; + const { host, extendServer } = options; + + const port = await getFreePort(options.port); + + if (!options.ci && !options.smokeTest && options.port != null && port !== options.port) { + const { shouldChangePort } = await inquirer.prompt({ + type: 'confirm', + default: true, + name: 'shouldChangePort', + message: `Port ${ + options.port + } is not available. Would you like to run Storybook on port ${port} instead?`, + }); + + if (!shouldChangePort) { + process.exit(1); + } + } // Used with `app.listen` below const listenAddr = [port]; diff --git a/lib/core/src/server/cli/dev.js b/lib/core/src/server/cli/dev.js index fb6ce8f3270e..71136e3c6b1c 100644 --- a/lib/core/src/server/cli/dev.js +++ b/lib/core/src/server/cli/dev.js @@ -1,16 +1,9 @@ import program from 'commander'; import chalk from 'chalk'; -import detectFreePort from 'detect-port'; import inquirer from 'inquirer'; import { logger } from '@storybook/node-logger'; import { parseList, getEnvConfig } from './utils'; -const getFreePort = port => - detectFreePort(port).catch(error => { - logger.error(error); - process.exit(-1); - }); - async function getCLI(packageJson) { process.env.NODE_ENV = process.env.NODE_ENV || 'development'; @@ -68,22 +61,7 @@ async function getCLI(packageJson) { program.port = parseInt(program.port, 10); } - const port = await getFreePort(program.port); - - if (!program.ci && !program.smokeTest && program.port != null && port !== program.port) { - const { shouldChangePort } = await inquirer.prompt({ - type: 'confirm', - default: true, - name: 'shouldChangePort', - message: `Port ${program.port} is not available. -Would you like to run Storybook on port ${port} instead?`, - }); - if (!shouldChangePort) { - process.exit(1); - } - } - - return { ...program, port }; + return { ...program }; } export default getCLI;