pliant
def → files → env → cli | params → { YOUR_CODE_HERE }
Usage
Let's walk through an example. To see the full example code, see the example project vigour-io/pliable
1. Create a configuration file to list the options our script may use
var path = var version = versionmoduleexports = exports = {} exportsversion = version /** * Give each option (item) * - `def`: a defaut value * - `env`: a name for its environment variable * - `cli`: a [commander option argument](https://www.npmjs.com/package/commander#option-parsing) * - `desc`: a description */exportsitems = "server.port": def: 8000 env: "MY_SCRIPT_PORT" cli: "-p, --port <port>" desc: "Port on which server should listen for HTTP requests" "server.gzip": def: true env: "MY_SCRIPT_GZIP" cli: "--no-gzip" desc: "Whether to gzip resources" /** * We can also provide a default value and environment variable name for the files option * `{ cli: "-c, --files <paths>", desc: "Comma-separated list of paths to config files" }` */exportsfiles = def: null env: "MY_APP_CONFIG_FILES"
2. Adhere to the pliant contract
We'll make the point of entry of our script a function which expects a single options argument
// Let's make a web server as an examplevar express = var compression = // Our main script should export a function expecting a single options argumentmoduleexports = { // This will be a simple web server var app = // Which may be configured not to gzip, but gzips by default if optsservergzip app else console // And which otherwise simply serves static files app // Let's have this server listen on the configured port var handle = app console return handle}
This sample script is a web server. For the sake of completeness, let's include an asset for it to serve:
I just got served
3. Create a requireable module
We just have to pass our function and the config object to pliant.fn
var pliant = main = config = moduleexports = exports = pliant
4. Create an executable
We just have to specify node
as the script runner and pass our function and the config object to pliant.bin
#!/usr/bin/env nodevar pliant = config = main = pliant
5. Now we can run our script and provide it with options in a variety of ways.
We can require it and pass it an options object
var http = var start = // We'll use the default port, but turn off gzipping
Try it out
$ node test.jsgzip deactivatedListening on port 8000SUCCESS$
We can call our executable from the command line and configure it via command line arguments
$ ./bin/index.js -p 8001Listening on port 8001
We can pass it a list of files to read configuration from.
package.json
looks like a good candidate
Try it out
$ ./bin/index.js -c package.jsonListening on port 8004
We can configure it via environment variables
$ export MY_SCRIPT_GZIP=false$ export MY_SCRIPT_PORT=8002$ ./bin/index.jsgzip deactivatedListening on port 8002
Priority: defaults < files < environment variables < command line arguments
Let's look at the port, for example:
- Default value is 8000 (from
config.js
) - File config makes it 8004 (from
package.json
) - The environment makes it 8002 (from
MY_SCRIPT_PORT
) - The
-p
command line argument can override all of the above
$ export MY_SCRIPT_GZIP=false$ export MY_SCRIPT_PORT=8002$ ./bin/index.js -p 8005gzip deactivatedListening on port 8005
6. Feedback
I'm looking for feedback on this project, feel free to open issues :)