Skip to content

Commit

Permalink
Move the free port logic so that loadOptions don't override it (#7237)
Browse files Browse the repository at this point in the history
Move the free port logic so that loadOptions don't override it
  • Loading branch information
ndelangen authored Jul 1, 2019
2 parents 7693cad + 9027070 commit 117160c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 24 deletions.
27 changes: 26 additions & 1 deletion lib/core/src/server/build-dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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);
Expand Down Expand Up @@ -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];
Expand Down
24 changes: 1 addition & 23 deletions lib/core/src/server/cli/dev.js
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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;

1 comment on commit 117160c

@vercel
Copy link

@vercel vercel bot commented on 117160c Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.