Skip to content

Commit

Permalink
refactor(bin): move options and helpers into separate files (#1470)
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-ciniawsky committed Aug 27, 2018
1 parent 2ee13ab commit 1896fcf
Show file tree
Hide file tree
Showing 3 changed files with 518 additions and 323 deletions.
164 changes: 164 additions & 0 deletions bin/options.js
@@ -0,0 +1,164 @@
'use strict';

/* eslint-disable
global-require,
multiline-ternary,
space-before-function-paren
*/
const ADVANCED_GROUP = 'Advanced options:';
const DISPLAY_GROUP = 'Stats options:';
const SSL_GROUP = 'SSL options:';
const CONNECTION_GROUP = 'Connection options:';
const RESPONSE_GROUP = 'Response options:';
const BASIC_GROUP = 'Basic options:';

const options = {
bonjour: {
type: 'boolean',
describe: 'Broadcasts the server via ZeroConf networking on start'
},
lazy: {
type: 'boolean',
describe: 'Lazy'
},
inline: {
type: 'boolean',
default: true,
describe: 'Inline mode (set to false to disable including client scripts like livereload)'
},
progress: {
type: 'boolean',
describe: 'Print compilation progress in percentage',
group: BASIC_GROUP
},
'hot-only': {
type: 'boolean',
describe: 'Do not refresh page if HMR fails',
group: ADVANCED_GROUP
},
stdin: {
type: 'boolean',
describe: 'close when stdin ends'
},
open: {
type: 'string',
describe: 'Open the default browser, or optionally specify a browser name'
},
useLocalIp: {
type: 'boolean',
describe: 'Open default browser with local IP'
},
'open-page': {
type: 'string',
describe: 'Open default browser with the specified page',
requiresArg: true
},
color: {
type: 'boolean',
alias: 'colors',
default: function supportsColor() {
return require('supports-color');
},
group: DISPLAY_GROUP,
describe: 'Enables/Disables colors on the console'
},
info: {
type: 'boolean',
group: DISPLAY_GROUP,
default: true,
describe: 'Info'
},
quiet: {
type: 'boolean',
group: DISPLAY_GROUP,
describe: 'Quiet'
},
'client-log-level': {
type: 'string',
group: DISPLAY_GROUP,
default: 'info',
describe: 'Log level in the browser (info, warning, error or none)'
},
https: {
type: 'boolean',
group: SSL_GROUP,
describe: 'HTTPS'
},
key: {
type: 'string',
describe: 'Path to a SSL key.',
group: SSL_GROUP
},
cert: {
type: 'string',
describe: 'Path to a SSL certificate.',
group: SSL_GROUP
},
cacert: {
type: 'string',
describe: 'Path to a SSL CA certificate.',
group: SSL_GROUP
},
pfx: {
type: 'string',
describe: 'Path to a SSL pfx file.',
group: SSL_GROUP
},
'pfx-passphrase': {
type: 'string',
describe: 'Passphrase for pfx file.',
group: SSL_GROUP
},
'content-base': {
type: 'string',
describe: 'A directory or URL to serve HTML content from.',
group: RESPONSE_GROUP
},
'watch-content-base': {
type: 'boolean',
describe: 'Enable live-reloading of the content-base.',
group: RESPONSE_GROUP
},
'history-api-fallback': {
type: 'boolean',
describe: 'Fallback to /index.html for Single Page Applications.',
group: RESPONSE_GROUP
},
compress: {
type: 'boolean',
describe: 'Enable gzip compression',
group: RESPONSE_GROUP
},
port: {
describe: 'The port',
group: CONNECTION_GROUP
},
'disable-host-check': {
type: 'boolean',
describe: 'Will not check the host',
group: CONNECTION_GROUP
},
socket: {
type: 'String',
describe: 'Socket to listen',
group: CONNECTION_GROUP
},
public: {
type: 'string',
describe: 'The public hostname/ip address of the server',
group: CONNECTION_GROUP
},
host: {
type: 'string',
default: 'localhost',
describe: 'The hostname/ip address the server will bind to',
group: CONNECTION_GROUP
},
'allowed-hosts': {
type: 'string',
describe: 'A comma-delimited string of hosts that are allowed to access the dev server',
group: CONNECTION_GROUP
}
};

module.exports = options;
114 changes: 114 additions & 0 deletions bin/utils.js
@@ -0,0 +1,114 @@
'use strict';

/* eslint-disable
no-shadow,
global-require,
multiline-ternary,
array-bracket-spacing,
space-before-function-paren
*/
const open = require('opn');

const colors = {
info (useColor, msg) {
if (useColor) {
// Make text blue and bold, so it *pops*
return `\u001b[1m\u001b[34m${msg}\u001b[39m\u001b[22m`;
}

return msg;
},
error (useColor, msg) {
if (useColor) {
// Make text red and bold, so it *pops*
return `\u001b[1m\u001b[31m${msg}\u001b[39m\u001b[22m`;
}

return msg;
}
};

// eslint-disable-next-line
const defaultTo = (value, def) => {
return value == null ? def : value;
};

function version () {
return `webpack-dev-server ${require('../package.json').version}\n` +
`webpack ${require('webpack/package.json').version}`;
}

function status (uri, options, log, useColor) {
const contentBase = Array.isArray(options.contentBase)
? options.contentBase.join(', ')
: options.contentBase;

if (options.socket) {
log.info(`Listening to socket at ${colors.info(useColor, options.socket)}`);
} else {
log.info(`Project is running at ${colors.info(useColor, uri)}`);
}

log.info(
`webpack output is served from ${colors.info(useColor, options.publicPath)}`
);

if (contentBase) {
log.info(
`Content not from webpack is served from ${colors.info(useColor, contentBase)}`
);
}

if (options.historyApiFallback) {
log.info(
`404s will fallback to ${colors.info(useColor, options.historyApiFallback.index || '/index.html')}`
);
}

if (options.bonjour) {
log.info(
'Broadcasting "http" with subtype of "webpack" via ZeroConf DNS (Bonjour)'
);
}

if (options.open) {
let openOptions = {};
let openMessage = 'Unable to open browser';

if (typeof options.open === 'string') {
openOptions = { app: options.open };
openMessage += `: ${options.open}`;
}

open(uri + (options.openPage || ''), openOptions).catch(() => {
log.warn(
`${openMessage}. If you are running in a headless environment, please do not use the --open flag`
);
});
}
}

function bonjour (options) {
const bonjour = require('bonjour')();

bonjour.publish({
name: 'Webpack Dev Server',
port: options.port,
type: 'http',
subtypes: [ 'webpack' ]
});

process.on('exit', () => {
bonjour.unpublishAll(() => {
bonjour.destroy();
});
});
}

module.exports = {
status,
colors,
version,
bonjour,
defaultTo
};

0 comments on commit 1896fcf

Please sign in to comment.