Verified Commit bb140c3e authored by Aral Balkan's avatar Aral Balkan
Browse files

Use minimist for command-line arguments

parent 85e26817
#!/usr/bin/env node
const fs = require('fs')
var ansi = require('ansi-escape-sequences')
const httpsServer = require('../index.js')
const arguments = process.argv
const arguments = require('minimist')(process.argv.slice(2))
if (arguments._.length > 1 || arguments.help === true) {
const usageFolderToServe = clr('folder-to-serve', 'green')
const usagePortOption = `${clr('--port', 'yellow')} ${clr('N', 'cyan')}`
const usageHttp2Option = clr('--http2', 'yellow')
const usage = `
${clr('Usage:', 'underline')}
${clr('https-server', 'bold')} [${usageFolderToServe}] [${usagePortOption}] [${usageHttp2Option}]
if (arguments.length > 4) {
console.log('Usage: https-server [folder-to-serve (default=.)] [port (default=443)]')
${usageFolderToServe}\t${clr('string', 'italic')}\tPath to the folder to serve (optional; defaults to current folder).
${usagePortOption}\t\t${clr('number', 'italic')}\tThe port to start the server on (optional; defaults to 443).
${usageHttp2Option}\t\t${clr('flag', 'italic')}\tRequests an HTTP2 server (optional; defaults to HTTP1).
`.replace(/\n$/, '').replace(/^\n/, '')
console.log(usage)
process.exit()
}
// If no path is passed, serve the current folder.
// If there is a path, serve that.
let pathToServe = '.'
if (arguments.length >= 3) {
pathToServe = arguments[2]
if (arguments._.length === 1) {
pathToServe = arguments._[0]
}
// If a port is specified, use it. Otherwise use the default port (443).
let port = 443
// If a port is specified, use that instead.
if (arguments.length === 4) {
port = parseInt(arguments[3])
if (arguments.port !== undefined) {
port = parseInt(arguments.port)
}
if (!fs.existsSync(pathToServe)) {
console.log(`\n 🤔 Error: could not find path ${pathToServe}\n`)
console.log(` 🤔 Error: could not find path ${pathToServe}\n`)
process.exit(1)
}
httpsServer.serve(pathToServe, port)
// Start the server.
httpsServer.serve(pathToServe, {port})
// Helpers.
// Format ansi strings.
// Courtesy Bankai (https://github.com/choojs/bankai/blob/master/bin.js#L142)
function clr (text, color) {
return process.stdout.isTTY ? ansi.format(text, color) : text
}
......@@ -56,14 +56,13 @@ class HttpsServer {
serve(pathToServe = '.', callback = null, options = {}) {
// Can be called as serve(pathToServe, callback) also.
if (typeof options === 'function') {
callback = options
options = {}
if (typeof callback === 'object') {
options = callback
callback = null
}
const port = options.port || 443
const isHTTP2 = options.isHTTP2 || false
const serverCreationMethod = isHTTP2 ? this.createSecureServer : this.createServer
this.ensureWeCanBindToPort(port, pathToServe)
......
{
"name": "@ind.ie/https-server",
"version": "1.0.7",
"version": "2.0.0",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
......@@ -21,6 +21,22 @@
"negotiator": "0.6.1"
}
},
"ansi-escape-sequences": {
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/ansi-escape-sequences/-/ansi-escape-sequences-4.0.1.tgz",
"integrity": "sha512-G3Aona26cXv8nWIwID6MP11WSishqnyOPQjYaVJ7CfY2Xgu5sHOXM39nQg6XtyfF9++oLV6l2uFGojBb4zglGA==",
"requires": {
"array-back": "^2.0.0"
}
},
"array-back": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/array-back/-/array-back-2.0.0.tgz",
"integrity": "sha512-eJv4pLLufP3g5kcZry0j6WXpIbzYw9GUB4mVJZno9wfwiBxbizTnHCw3VJb07cBihbFX48Y7oSrW9y+gt4glyw==",
"requires": {
"typical": "^2.6.1"
}
},
"array-flatten": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz",
......@@ -407,8 +423,7 @@
"minimist": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz",
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=",
"dev": true
"integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ="
},
"morgan": {
"version": "1.9.1",
......@@ -639,6 +654,11 @@
"mime-types": "~2.1.18"
}
},
"typical": {
"version": "2.6.1",
"resolved": "https://registry.npmjs.org/typical/-/typical-2.6.1.tgz",
"integrity": "sha1-XAgOXWYcu+OCWdLnCjxyU+hziB0="
},
"unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
......
......@@ -20,7 +20,9 @@
"license": "AGPL-3.0-or-later",
"dependencies": {
"@ind.ie/nodecert": "^1.0.6",
"ansi-escape-sequences": "^4.0.1",
"express": "^4.16.4",
"minimist": "^1.2.0",
"morgan": "^1.9.1"
},
"devDependencies": {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment