From 6e4b06e9a8b8b14766ac4e0561a7a71ce9848a9e Mon Sep 17 00:00:00 2001 From: Rik Hoffbauer <62353999+rikhoffbauer@users.noreply.github.com> Date: Fri, 27 Mar 2020 05:19:23 +0100 Subject: [PATCH 1/2] feat: allow for more ways to store cli configuration use cosmiconfig to load cli configuration instead of using a manual/custom implementation closes issue #71 --- README.md | 23 +++++++++++++++++------ lib/cli.js | 9 +++++---- package.json | 1 + 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 4daf437..9ed3869 100644 --- a/README.md +++ b/README.md @@ -38,20 +38,31 @@ Nollup provides a dev server which can be used as a CLI command. See "Nollup Options" for list of available flags. -## .nolluprc +## Configuration -Configuration file that can be used to pass configuration instead of as flags through the CLI. -``` +You can use a configuration file as alternative to CLI flags or to compliement them. + +Configuration files are read using [cosmiconfig](https://github.com/davidtheclark/cosmiconfig). + +This means configuration will be looked for in the following places in the order listed: + +1. A `nollup` property in a `package.json` file. +2. A `.nolluprc` file with `JSON` or `YAML` syntax. +3. A `.nolluprc.json` file. +4. A `.nolluprc.yaml`, `.nolluprc.yml`, or `.nolluprc.js` file. +5. A `nollup.config.js` JS file exporting the object. + +`.nolluprc` +```json { "hot": true, "contentBase": "./public" } ``` -A JavaScript file called ```.nolluprc.js``` can be used instead. - -``` +`.nolluprc.js` +```javascript module.exports = { hot: true, contentBase: './public' diff --git a/lib/cli.js b/lib/cli.js index 96e8b26..17c40ce 100644 --- a/lib/cli.js +++ b/lib/cli.js @@ -13,6 +13,7 @@ let path = require('path'); let express = require('express'); let fallback = require('express-history-api-fallback'); let proxy = require('express-http-proxy'); +let cosmiconfig = require('cosmiconfig'); let nollupDevServer = require('./dev-middleware'); let ConfigLoader = require('./impl/ConfigLoader'); let app = express(); @@ -90,10 +91,10 @@ for (let i = 0; i < process.argv.length; i++) { } (async function () { - if (fs.existsSync('.nolluprc')) { - options = Object.assign({}, options, JSON.parse(fs.readFileSync('.nolluprc'))); - } else if (fs.existsSync('.nolluprc.js')) { - options = Object.assign({}, options, require(path.resolve(process.cwd(), './.nolluprc.js'))); + const cosmiconfigResult = cosmiconfig.cosmiconfigSync('nollup').search() + + if (cosmiconfigResult && cosmiconfigResult.config) { + options = Object.assign({}, options, cosmiconfigResult.config); } if (options.before) { diff --git a/package.json b/package.json index c22d364..350e922 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "acorn": "^7.0.0", "chokidar": "^2.0.2", "convert-source-map": "^1.5.1", + "cosmiconfig": "^6.0.0", "express": "^4.16.3", "express-history-api-fallback": "^2.2.1", "express-http-proxy": "^1.5.1", From a8a954b7b609a0d1c1f2a73041c0419ef5693b8e Mon Sep 17 00:00:00 2001 From: Rik Hoffbauer <62353999+rikhoffbauer@users.noreply.github.com> Date: Thu, 2 Apr 2020 13:00:50 +0200 Subject: [PATCH 2/2] docs: fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ed3869..2a73567 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ See "Nollup Options" for list of available flags. ## Configuration -You can use a configuration file as alternative to CLI flags or to compliement them. +You can use a configuration file as alternative to CLI flags or to complement them. Configuration files are read using [cosmiconfig](https://github.com/davidtheclark/cosmiconfig).