1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- const url = require('url')
- const chalk = require('chalk')
- const address = require('address')
- module.exports = function prepareUrls (protocol, host, port, pathname = '/') {
- 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, lanUrlForConfig
- let lanUrlForTerminal = chalk.gray('unavailable')
- if (isUnspecifiedHost) {
- prettyHost = 'localhost'
- try {
-
- lanUrlForConfig = address.ip()
- if (lanUrlForConfig) {
-
-
- if (
- /^10[.]|^172[.](1[6-9]|2[0-9]|3[0-1])[.]|^192[.]168[.]/.test(
- lanUrlForConfig
- )
- ) {
-
- lanUrlForTerminal = prettyPrintUrl(lanUrlForConfig)
- } else {
-
- lanUrlForConfig = undefined
- }
- }
- } catch (_e) {
-
- }
- } else {
- prettyHost = host
- lanUrlForConfig = host
- lanUrlForTerminal = prettyPrintUrl(lanUrlForConfig)
- }
- const localUrlForTerminal = prettyPrintUrl(prettyHost)
- const localUrlForBrowser = formatUrl(prettyHost)
- return {
- lanUrlForConfig,
- lanUrlForTerminal,
- localUrlForTerminal,
- localUrlForBrowser
- }
- }
|