diff --git a/bin/serve.js b/bin/serve.js index 27125475..f18d3c57 100755 --- a/bin/serve.js +++ b/bin/serve.js @@ -17,6 +17,7 @@ const nodeVersion = require('node-version') const pkg = require('../package') const listening = require('../lib/listening') const serverHandler = require('../lib/server') +const { options, minimist } = require('../lib/options') // Throw an error if node version is too low if (nodeVersion.major < 6) { @@ -34,49 +35,13 @@ if (process.env.NODE_ENV !== 'production' && pkg.dist) { updateNotifier({ pkg }).notify() } -args - .option('port', 'Port to listen on', process.env.PORT || 5000) - .option( - 'cache', - 'Time in milliseconds for caching files in the browser', - 3600 - ) - .option('single', 'Serve single page apps with only one index.html') - .option('unzipped', 'Disable GZIP compression') - .option('ignore', 'Files and directories to ignore') - .option('auth', 'Serve behind basic auth') - .option( - 'cors', - 'Setup * CORS headers to allow requests from any origin', - false - ) - .option('silent', `Don't log anything to the console`) - .option(['n', 'clipless'], `Don't copy address to clipboard`, false) - .option('open', 'Open local address in browser', false) - -const flags = args.parse(process.argv, { - minimist: { - alias: { - a: 'auth', - C: 'cors', - S: 'silent', - s: 'single', - u: 'unzipped', - n: 'clipless', - o: 'open' - }, - boolean: [ - 'auth', - 'cors', - 'silent', - 'single', - 'unzipped', - 'clipless', - 'open' - ] - } -}) +// Register the list of options +args.options(options) + +// And initialize `args` +const flags = args.parse(process.argv, { minimist }) +// Figure out the content directory const directory = args.sub[0] // Don't log anything to the console if silent mode is enabled diff --git a/lib/options.js b/lib/options.js new file mode 100644 index 00000000..a1ef38fe --- /dev/null +++ b/lib/options.js @@ -0,0 +1,60 @@ +exports.options = [ + { + name: 'port', + description: 'Port to listen on', + defaultValue: process.env.PORT || 5000 + }, + { + name: 'cache', + description: 'Time in milliseconds for caching files in the browser', + defaultValue: 3600 + }, + { + name: 'single', + description: 'Serve single page apps with only one index.html' + }, + { + name: 'unzipped', + description: 'Disable GZIP compression' + }, + { + name: 'ignore', + description: 'Files and directories to ignore' + }, + { + name: 'auth', + description: 'Serve behind basic auth' + }, + { + name: 'cors', + description: 'Setup * CORS headers to allow requests from any origin', + defaultValue: false + }, + { + name: 'silent', + description: `Don't log anything to the console` + }, + { + name: ['n', 'clipless'], + description: `Don't copy address to clipboard`, + defaultValue: false + }, + { + name: 'open', + description: 'Open local address in browser', + defaultValue: false + } +] + +exports.minimist = { + alias: { + a: 'auth', + C: 'cors', + S: 'silent', + s: 'single', + u: 'unzipped', + n: 'clipless', + o: 'open' + }, + boolean: ['auth', 'cors', 'silent', 'single', 'unzipped', 'clipless', 'open'] +}