Skip to content

Commit

Permalink
styleguidist#470 reuse react dev-tools
Browse files Browse the repository at this point in the history
  • Loading branch information
snmslavk committed Aug 29, 2017
1 parent 558f430 commit cd2ad5b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 70 deletions.
8 changes: 0 additions & 8 deletions scripts/utils/__tests__/devServerUtils.spec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import devServerUtils from '../devServerUtils';

it('should return a correct address', () => {
const result = devServerUtils.prepareUrls('http', '0.0.0.0', '6060');
expect(result.localUrlForTerminal).toBeTruthy();
expect(result.localUrlForBrowser).toBeTruthy();
expect(result.lanUrlForConfig).toBeTruthy();
expect(result.lanUrlForTerminal).toBeTruthy();
});

it('Main method doesn’t throw error', () => {
const fn = () => devServerUtils.printInstructions();
expect(fn).not.toThrowError();
Expand Down
66 changes: 4 additions & 62 deletions scripts/utils/devServerUtils.js
Original file line number Diff line number Diff line change
@@ -1,77 +1,19 @@
'use strict';

const address = require('address');
const url = require('url');
const chalk = require('chalk');
const logger = require('glogg')('rsg');

function prepareUrls(protocol, host, port) {
const formatUrl = hostname =>
url.format({
protocol,
hostname,
port,
pathname: '/',
});
const prettyPrintUrl = hostname =>
url.format({
protocol,
hostname,
port: chalk.bold(port),
pathname: '/',
});

const isUnspecifiedHost = host === '0.0.0.0' || host === '::';
let prettyHost;
let lanUrlForConfig;
let lanUrlForTerminal;
if (isUnspecifiedHost) {
prettyHost = 'localhost';
try {
// This can only return an IPv4 address
lanUrlForConfig = address.ip();
if (lanUrlForConfig) {
// Check if the address is a private ip
// https://en.wikipedia.org/wiki/Private_network#Private_IPv4_address_spaces
if (/^10[.]|^172[.](1[6-9]|2[0-9]|3[0-1])[.]|^192[.]168[.]/.test(lanUrlForConfig)) {
// Address is private, format it for later use
lanUrlForTerminal = prettyPrintUrl(lanUrlForConfig);
} else {
// Address is not private, so we will discard it
lanUrlForConfig = undefined;
}
}
} catch (_e) {
// ignored
}
} else {
prettyHost = host;
}
const localUrlForTerminal = prettyPrintUrl(prettyHost);
const localUrlForBrowser = formatUrl(prettyHost);
return {
lanUrlForConfig,
lanUrlForTerminal,
localUrlForTerminal,
localUrlForBrowser,
};
}
const webpackDevServerUtils = require('react-dev-utils/WebpackDevServerUtils');

function printInstructions(protocol, host, port) {
const urls = prepareUrls(protocol ? 'https' : 'http', host, port);
const urls = webpackDevServerUtils.prepareUrls(protocol ? 'https' : 'http', host, port);
logger.info(`You can now view style guide in the browser.`);

if (urls.lanUrlForTerminal) {
logger.info(` ${chalk.bold('Local:')} ${urls.localUrlForTerminal}`);
logger.info(` ${chalk.bold('On your network:')} ${urls.lanUrlForTerminal}`);
} else {
logger.info(` ${urls.localUrlForTerminal}`);
}
logger.info(` ${chalk.bold('Local:')} ${urls.localUrlForTerminal}`);
logger.info(` ${chalk.bold('On your network:')} ${urls.lanUrlForTerminal}`);

logger.info('Note that the development build is not optimized.');
}

module.exports = {
printInstructions,
prepareUrls,
};

0 comments on commit cd2ad5b

Please sign in to comment.