diff --git a/.eslintrc.js b/.eslintrc.js new file mode 100644 index 0000000..0b76b29 --- /dev/null +++ b/.eslintrc.js @@ -0,0 +1,33 @@ +module.exports = { + 'env': { + 'commonjs': true, + 'es6': true, + 'node': true + }, + 'extends': 'eslint:recommended', + 'globals': { + 'Atomics': 'readonly', + 'SharedArrayBuffer': 'readonly' + }, + 'parserOptions': { + 'ecmaVersion': 2018 + }, + 'rules': { + 'indent': [ + 'error', + 4 + ], + 'linebreak-style': [ + 'error', + 'unix' + ], + 'quotes': [ + 'error', + 'single' + ], + 'semi': [ + 'error', + 'always' + ] + } +}; \ No newline at end of file diff --git a/.gitignore b/.gitignore index 25b70ae..bb14eca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,6 @@ node_modules/ coverage +npm-debug.log +.nyc_output/ +package-lock.json + diff --git a/.mocharc.js b/.mocharc.js new file mode 100644 index 0000000..1bb971f --- /dev/null +++ b/.mocharc.js @@ -0,0 +1,5 @@ +module.exports = { + extension: ['.spec.js'], + recursive: true, + reporter: 'spec' +} diff --git a/.travis.yml b/.travis.yml index 7ab9726..0570f30 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,5 @@ language: node_js node_js: - - 0.10 + - 6 script: "npm run-script travis" after_success: " var oconf = require('oconf'); + > oconf.load('config/some-public-settings.cjson', { public: true }); + { + "fancy-list": { + "scroll-timeout": 100 + } + } + +Of course you can also just grab the `#public` property from the result of `oconf.load`: + + > var oconf = require('oconf'); + > oconf.load('config/some-public-settings.cjson')['#public']; + { + "fancy-list": { + "scroll-timeout": 100 + } + } + +## Binary To help resolve configuration on the command line oconf exports a CLI -tool called oconf. In it's simplest form it takes a path to an cjson -file, and outputs the resolved JSON object. +tool called oconf. It takes a path to an cjson file, and outputs the +resolved JSON object. ``` $ oconf config.cjson @@ -80,42 +148,80 @@ file, and outputs the resolved JSON object. } ``` -Oconf also takes a `--lint` option. It will make oconf not output any -of the resolved configuration, but only exit with an error in case of -any formatting errors in the files. +You can lint your configuration files by using the `--lint` flag. It +will not output any of the resolved configuration, but only exit with +an error in case of any formatting errors in the files. ``` $ oconf --lint config.cjson ``` -You can supply a path to a value as well: +By using the `--extract-option` flag you can supply a path to a value +as well: ``` - $ oconf config.cjson obj.foo + $ oconf --extract-option obj.foo config.cjson +bar +``` + +The output from the above is the raw data. That is useful when you +need to pass the configuration to other CLI tools. If you need the +JSON formatted data, you can pass the `--option-as-json` option. + +``` + $ oconf --extract-option obj.foo --json config.cjson "bar" ``` -The output from the above is the JSON.stringified format. If you need -to pass it into another cli program, you'd most likely want it as a -string literal and not quoted. That can be achieved like this: +If the key is missing `oconf --extract-option` will exit with status +code 1. If you need to overwrite that behaviour you can pass the +`--allow-missing-option` flag to oconf which will make it exit with +status code 0 if no value is found at the given path. + +You can also filter out values in the `#public` blob with the `--public` flag. ``` - $ oconf --bare config.cjson obj.foo -bar + $ oconf --public some-public-settings.cjson +{ + "fancy-list": { + "scroll-timeout": 100 + } +} +``` + +## Support for relaxed JSON format +**Reference:** https://www.npmjs.com/package/relaxed-json + +```js +// Contents of config.rjson +{ + someConfig: 'someValue' +} ``` -If the key is missing oconf will exit with exit code 1. If you need to -overwrite that behaviour you can pass the `--allowmissing` flag to -oconf which will make it exit with status code 0 if no value is found -at the given path. +### In terminal: +```sh + $ oconf --relaxed config.rjson +{ + "someConfig": "someValue" +} +``` + +In Node JS: +```js +var oconf = require('oconf'); +var data = oconf.load('config.rjson', { relaxed: true }); +console.log(JSON.stringify(data)); + +// Output: +{"someConfig": "someValue"} +``` -Tests ------ +## Tests Download/clone, run `npm install` and then `npm test`. -License -------- +## License -The software is provided under the Modified BSD License; See LICENSE for -further details. +The software is provided under the Modified BSD License; See +[LICENSE](LICENSE) for further details. diff --git a/bin/oconf b/bin/oconf index 1d47a7b..26a9694 100755 --- a/bin/oconf +++ b/bin/oconf @@ -1,76 +1,119 @@ #!/usr/bin/env node -// ex: filetype=javascript - -var oconf = require('../lib/index'), - util = require('util'), - _ = require('underscore'), - argv = require('optimist') - .option('lint', { - type: 'boolean', - default: false - }) - .option('ignore', { - type: 'string' - }) - .option('bare', { - type: 'boolean' - }) - .option('allowmissing', { - type: 'boolean' - }) - .demand(1) - .usage('Usage: $0 [--lint] [--bare] [--allowmissing] [--ignore ] [path.to.value...]') - .argv; +const oconf = require('../lib/index'); +const { Command } = require('commander'); +const program = new Command(); -if (argv.lint) { - console.warn( - 'The option --lint is deprecated and will be replaced by', - 'a standalone binary with release 3.0.0 of oconf.' - ); +program + .name('oconf') + .usage('[--lint|--extract-option