Skip to content

Commit

Permalink
Make it easier to add new options.
Browse files Browse the repository at this point in the history
  • Loading branch information
phBalance committed Oct 9, 2019
1 parent 28c5f79 commit 59b6b49
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions command_line.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,59 @@

const fs = require("fs");
const path = require("path");
const log = require("loglevel");

const log = require("loglevel");
const parseArgs = require("minimist");

const opts = {
string: ["blacklist", "cert", "copyToDir", "drop", "early", "fileExt", "key", "loglevel", "map", "mode", "port", "proxy", "public"],
boolean: ["compress", "headfull", "help", "noserver"]
};

const DEFAULT = {
PORT: 8080,
LOGLEVEL: log.levels.INFO
};

const options = [
{ name: "blacklist", param: "item", desc: `Regex describing files which should not be included in the rendering.` },
{ name: "cert", param: "cert", desc: `SSL Certificate to use.` },
{ name: "key", param: "key", desc: `SSL Key to use.` },
{ name: "drop", param: "toUid:toGid", desc: `The uid and gid that the process should become as soon as possible.` },
{ name: "early", param: "urlToEarlyRender", desc: `A URL that should be rendered before listening for requests. Specify multiple times for many URLs.` },
{ name: "loglevel", param: "0-5", desc: `Set log levels (according to the loglevel package) to this number. Default is ${DEFAULT.LOGLEVEL}.` },
{ name: "map", param: "hostMapping", desc: `Array of JSON objects with from and to properties.` },
{ name: "port", param: "thisServerListenPort", desc: `What port should this server listen to for requests. Default is ${DEFAULT.PORT}.` },
{ name: "proxy", param: "behindThisProxy", desc: `Indicate that this server is behind this particular proxy.` },
{ name: "public", param: "staticFileDirectory", desc: `Serve static files from this directory.` },
{ name: "copyToDir", param: "directoryToPutRenderedFile", desc: `Generate a file for each page that is rendered.` },
{ name: "fileExt", param: "extension", desc: `Add extension to all files that are generated. For instance, it might be useful to add ".html".` },
{ name: "mode", param: "fileMode", desc: `Sets the file mode to be set when files are written.` },

{ name: "compress", desc: `Allows on the fly compression of reponses.` },
{ name: "headfull", desc: `Show the normally headless chrome. Useful for debugging.` },
{ name: "help", desc: `Show the usage description.` },
{ name: "noserver", desc: `Don't start a server to listen for incoming requests. In other words, just run with files.` },
];

const opts = {
string: options.filter((option) => option.param).map((option) => option.name).sort(),
boolean: options.filter((option) => !option.param).map((option) => option.name).sort(),
}

// Process command line arguments
const cmdline = process.argv.slice(2);
const parsed = parseArgs(cmdline, opts);

if(parsed.help) {
console.log(`${process.execPath} --map "mapping key:value" [optional options]
string/string array options can be repeated any number of times (see minimist package):
[--blacklist item] Regex describing files which should not be included in the rendering.
[--cert cert] SSL Certificate.
[--key key] SSL Key.
[--drop toUid:toGid] The uid and gid that the process should become as soon as possible.
[--early urlToEarlyRender] A list of URLs that should be rendered before listening for requests.
[--loglevel 0-5] Set log levels according to the loglevel package. Default is ${DEFAULT.LOGLEVEL}
[--map hostMapping] Array of JSON objects with from and to properties.
[--port thisServerListenPort] What port should this server listen to for requests. Default is ${DEFAULT.PORT}.
[--proxy behindThisProxy] Indicate that this server is behind this particular proxy.
[--public staticFileDirectory] Serve static files from this directory.
[--copyToDir directoryToPutRenderedFile] Generate a file for each page that is rendered.
[--fileExt extension] Add extension to all files that are generated. For instance, it might be useful to add ".html".
[--mode fileMode] Sets the file mode to be set when files are written.
boolean options need only be provided once with no additional tokens:
[--compress] Allows on the fly compression of reponses
[--headfull] Show the normally headless chrome. Useful for debugging.
[--help] Show this help
[--noserver] Indicate that we don't need the server started. In other words, just run with files.`);
console.log(`${process.execPath} --map "mapping key:value" [optional options]\n`,
`Supported string options which can be repeated any number of times (see minimist package):\n`,
`${options.filter((option) => option.param).reduce((accum, option) => accum + `\t[--${option.name} ${option.param}] ${option.desc}\n`, "")}\n`,
`Supported boolean options are:`,
`${options.filter((option) => option.param).reduce((accum, option) => accum + `\n\t[--${option.name}] ${option.desc}`, "")}`);

// Indicate that we'd like the program to exit when it can.
process.exit(0);
}

// Must have --map provided
console.assert(parsed._.length === 0, "Unhandled arguments!", parsed._);
console.assert(parsed.map, "No mapping provided", parsed.map);
console.assert(parsed.map, "No mapping provided");

// If noserver is provided, ensure that copyToDir is provided.
console.assert(parsed.noserver ? parsed.copyToDir && parsed.early : true, "--noserver should have --copyToDir and --early provided", parsed.noserver, parsed.copyToDir, parsed.early);
Expand Down

0 comments on commit 59b6b49

Please sign in to comment.