├── src ├── .eslintrc ├── bin │ └── index.js └── lib │ ├── browser.js │ ├── index.js │ └── utils.js ├── test ├── .eslintrc ├── defaults.json ├── duplicate-config │ └── .thingrc ├── utils.test.js ├── ini.test.js ├── project-config │ └── package.json ├── project │ └── package.json ├── cli.test.js └── test.js ├── .npmignore ├── .travis.yml ├── .babelrc ├── .eslintignore ├── .gitignore ├── .cz-config.js ├── package.json ├── README.md ├── lib └── utils.js └── LICENSE /src/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "garthdb/cli" 3 | } 4 | -------------------------------------------------------------------------------- /test/.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "garthdb/ava" 3 | } 4 | -------------------------------------------------------------------------------- /test/defaults.json: -------------------------------------------------------------------------------- 1 | { 2 | "option": false, 3 | "envOption": 24 4 | } 5 | -------------------------------------------------------------------------------- /test/duplicate-config/.thingrc: -------------------------------------------------------------------------------- 1 | { 2 | "option": false, 3 | "envOption": 24 4 | } 5 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | test/ 2 | src/ 3 | .* 4 | coverage 5 | inch.json 6 | docs.json 7 | node_modules/ 8 | npm-debug.log 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: "6" 3 | before_script: 4 | - npm link 5 | after_success: 6 | - npm run codecov 7 | - npm run release 8 | -------------------------------------------------------------------------------- /src/bin/index.js: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env node 2 | import config from '../lib'; 3 | 4 | console.log( 5 | JSON.stringify(config(process.argv[2]), false, 2) 6 | ); 7 | -------------------------------------------------------------------------------- /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": ["es2015"], 3 | "plugins": ["add-module-exports"], 4 | "env": { 5 | "test": { 6 | "plugins": [ "istanbul" ] 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | docs.json 4 | *.log 5 | npm-debug.log* 6 | .DS_Store 7 | /lib 8 | /bin 9 | inch.json 10 | /coverage 11 | /.nyc_output 12 | /test/fixtures 13 | -------------------------------------------------------------------------------- /test/utils.test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import cc from '../src/lib/utils'; 3 | 4 | test('file expects string', t => { 5 | t.throws(() => { 6 | cc.file({ option: false }); 7 | }, /Path must be a string\./); 8 | }); 9 | -------------------------------------------------------------------------------- /test/ini.test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import INI from 'ini'; 3 | import cc from '../src/lib/utils'; 4 | 5 | test('INI parser', t => { 6 | const fixture = { hello: true }; 7 | const json = cc.parse(JSON.stringify(fixture)); 8 | const ini = cc.parse(INI.stringify(fixture)); 9 | t.deepEqual(json, ini); 10 | }); 11 | -------------------------------------------------------------------------------- /test/project-config/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "project", 3 | "version": "1.0.0", 4 | "description": "A package.json file used to test added functionality to rc in config-attendant", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "license": "Apache-2.0", 10 | "config": { 11 | "foo": { 12 | "option": false, 13 | "envOption": 22 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /test/project/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "project", 3 | "version": "1.0.0", 4 | "description": "A package.json file used to test added functionality to rc in config-attendant", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "license": "Apache-2.0", 10 | "foo": { 11 | "package": 21 12 | }, 13 | "config": { 14 | "foo": { 15 | "package": 22 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/lib/browser.js: -------------------------------------------------------------------------------- 1 | // when this is loaded into the browser, 2 | // just use the defaults... 3 | 4 | /** 5 | * Public: when used in browser, it just returns the defaults and doesn't look for config files. 6 | * 7 | * * `name` {String} the name of the app, not used in the browser. 8 | * * `defaults` {Object} default config values. 9 | * 10 | * ## Examples 11 | * 12 | * In the browser using browserify: 13 | * 14 | * ```js 15 | * var config = require('config-attendant'); 16 | * config('foo',{options: true}); // {options: true} 17 | * ``` 18 | * 19 | * Returns {Object} default config values. 20 | */ 21 | export default (name, defaults) => defaults; 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules/ 2 | tmp 3 | logs 4 | docs.json 5 | *.log 6 | npm-debug.log* 7 | .DS_Store 8 | /lib 9 | /bin 10 | 11 | # Runtime data 12 | pids 13 | *.pid 14 | *.seed 15 | 16 | # Directory for instrumented libs generated by jscoverage/JSCover 17 | lib-cov 18 | 19 | # Coverage directory used by tools like istanbul 20 | coverage 21 | 22 | # nyc test coverage 23 | .nyc_output 24 | 25 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 26 | .grunt 27 | 28 | # node-waf configuration 29 | .lock-wscript 30 | 31 | # Compiled binary addons (http://nodejs.org/api/addons.html) 32 | build/Release 33 | 34 | # Dependency directories 35 | node_modules 36 | jspm_packages 37 | 38 | # Optional npm cache directory 39 | .npm 40 | 41 | # Optional REPL history 42 | .node_repl_history 43 | -------------------------------------------------------------------------------- /.cz-config.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | 5 | types: [ 6 | {value: 'feat', name: 'feat: A new feature'}, 7 | {value: 'fix', name: 'fix: A bug fix'}, 8 | {value: 'docs', name: 'docs: Documentation only changes'}, 9 | {value: 'style', name: 'style: Changes that do not affect the meaning of the code\n (white-space, formatting, missing semi-colons, etc)'}, 10 | {value: 'refactor', name: 'refactor: A code change that neither fixes a bug nor adds a feature'}, 11 | {value: 'perf', name: 'perf: A code change that improves performance'}, 12 | {value: 'test', name: 'test: Adding missing tests'}, 13 | {value: 'chore', name: 'chore: Changes to the build process or auxiliary tools\n and libraries such as documentation generation'}, 14 | {value: 'revert', name: 'revert: Revert to a commit'}, 15 | {value: 'WIP', name: 'WIP: Work in progress'} 16 | ], 17 | 18 | allowBreakingChanges: ['feat', 'fix', 'perf'] 19 | 20 | }; 21 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "config-attendant", 3 | "description": "rc + package.json config data + custom data", 4 | "main": "./lib", 5 | "browserify": "./lib/browser.js", 6 | "scripts": { 7 | "prepublish": "npm run compile", 8 | "compile": "babel -d ./ ./src/", 9 | "test": "nyc ava", 10 | "coverage": "nyc npm test", 11 | "lint": "eslint .", 12 | "codecov": "nyc report -r lcovonly && codecov", 13 | "checkdocs": "atomdoc src", 14 | "validate": "npm run lint && npm run test && npm run checkdocs", 15 | "release": "semantic-release pre && npm publish && semantic-release post" 16 | }, 17 | "repository": { 18 | "type": "git", 19 | "url": "https://github.com/GarthDB/config-attendant.git" 20 | }, 21 | "license": "Apache-2.0", 22 | "keywords": [ 23 | "config", 24 | "rc", 25 | "unix", 26 | "defaults", 27 | "package.json" 28 | ], 29 | "bin": { 30 | "config": "./bin/index.js" 31 | }, 32 | "author": "Garth Braithwaite (http://garthdb.com)", 33 | "contributors": [ 34 | "Dominic Tarr (dominictarr.com)" 35 | ], 36 | "eslintConfig": { 37 | "extends": "garthdb" 38 | }, 39 | "ava": { 40 | "files": [ 41 | "test/*.js" 42 | ], 43 | "require": [ 44 | "babel-register" 45 | ], 46 | "babel": "inherit" 47 | }, 48 | "nyc": { 49 | "include": [ 50 | "src/lib/*.js", 51 | "bin/index.js" 52 | ], 53 | "require": [ 54 | "babel-register" 55 | ] 56 | }, 57 | "dependencies": { 58 | "deep-extend": "^0.4.0", 59 | "ini": "^1.3.0", 60 | "minimist": "^1.2.0", 61 | "strip-json-comments": "^2.0.1" 62 | }, 63 | "devDependencies": { 64 | "ava": "^0.16.0", 65 | "babel-cli": "^6.16.0", 66 | "babel-core": "^6.17.0", 67 | "babel-plugin-add-module-exports": "^0.2.1", 68 | "babel-plugin-istanbul": "^2.0.1", 69 | "babel-preset-es2015": "^6.16.0", 70 | "babel-register": "^6.16.3", 71 | "codecov": "^1.0.1", 72 | "cz-customizable": "^4.0.0", 73 | "eslint": "^3.7.1", 74 | "eslint-config-garthdb": "^0.1.0", 75 | "nixt": "^0.5.0", 76 | "nyc": "^8.3.1", 77 | "semantic-release": "^4.3.5" 78 | }, 79 | "config": { 80 | "commitizen": { 81 | "path": "./node_modules/cz-customizable" 82 | } 83 | }, 84 | "version": "0.1.0" 85 | } 86 | -------------------------------------------------------------------------------- /test/cli.test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import nixt from 'nixt'; 3 | import path from 'path'; 4 | 5 | test.cb('should use package.json data', t => { 6 | const dir = path.resolve(__dirname, 'project'); 7 | const expected = { 8 | package: 21, 9 | _: [ 10 | 'foo', 11 | ], 12 | packageFile: path.resolve(dir, 'package.json'), 13 | }; 14 | nixt() 15 | .expect(result => { 16 | const resultObj = JSON.parse(result.stdout); 17 | t.deepEqual(resultObj, expected); 18 | }) 19 | .cwd(dir) 20 | .run('config foo') 21 | .end(t.end); 22 | }); 23 | 24 | test.cb('should use config in package.json data', t => { 25 | const dir = path.resolve(__dirname, 'project-config'); 26 | const expected = { 27 | option: false, 28 | envOption: 22, 29 | _: [ 30 | 'foo', 31 | ], 32 | packageFile: path.resolve(dir, 'package.json'), 33 | }; 34 | nixt() 35 | .expect(result => { 36 | const resultObj = JSON.parse(result.stdout); 37 | t.deepEqual(resultObj, expected); 38 | }) 39 | .cwd(dir) 40 | .run('config foo') 41 | .end(t.end); 42 | }); 43 | 44 | test.cb('should throw an error when name is not specified', t => { 45 | nixt() 46 | .expect(result => { 47 | const errorIndex = result.stderr.indexOf('Error: rc(name): name *must* be string'); 48 | t.true(Boolean(errorIndex > -1)); 49 | }) 50 | .run('config') 51 | .end(t.end); 52 | }); 53 | 54 | test.cb('Should not return packageFile if none is found', t => { 55 | const dir = path.resolve(__dirname, '../../'); 56 | const expected = { 57 | _: [ 58 | 'foo', 59 | ], 60 | }; 61 | nixt() 62 | .expect(result => { 63 | t.deepEqual(JSON.parse(result.stdout), expected); 64 | }) 65 | .cwd(dir) 66 | .run('config foo') 67 | .end(t.end); 68 | }); 69 | 70 | test.cb('should accept command line arguments', t => { 71 | const dir = path.resolve(__dirname, 'project-config'); 72 | const configPath = '../defaults.json'; 73 | const expected = { 74 | option: false, 75 | envOption: 24, 76 | _: [ 77 | 'foo', 78 | ], 79 | config: configPath, 80 | configs: [ 81 | configPath, 82 | ], 83 | packageFile: path.resolve(dir, 'package.json'), 84 | }; 85 | nixt() 86 | .expect(result => { 87 | const resultObj = JSON.parse(result.stdout); 88 | t.deepEqual(resultObj, expected); 89 | }) 90 | .cwd(dir) 91 | .run(`config foo --config ${configPath}`) 92 | .end(t.end); 93 | }); 94 | -------------------------------------------------------------------------------- /src/lib/index.js: -------------------------------------------------------------------------------- 1 | import { join } from 'path'; 2 | import deepExtend from 'deep-extend'; 3 | import cc from './utils'; 4 | 5 | const etc = '/etc'; 6 | const win = process.platform === 'win32'; 7 | const home = win 8 | ? process.env.USERPROFILE 9 | : process.env.HOME; 10 | 11 | const minimist = require('minimist')(process.argv.slice(2)); 12 | 13 | /** 14 | * Private: reads and parses config files, and adds them to the configs array. 15 | * 16 | * * `file` {String} path to file to read and parse. 17 | * * `configs` {Array} of config {Objects}. 18 | * * `configFiles` {Array} of config file paths {Strings}. 19 | * * `parse` {Function} used to parse config files. 20 | */ 21 | function _addConfigFile(file, configs, configFiles, parse) { 22 | if (!configFiles.includes(file)) { 23 | const fileConfig = cc.file(file); 24 | if (fileConfig) { 25 | configs.push(parse(fileConfig)); 26 | configFiles.push(file); 27 | } 28 | } 29 | } 30 | 31 | /** 32 | * Private: finds the closest package.json file, and adds corresponding config 33 | * data to the configs array. 34 | * 35 | * * `places` {Array} of {Strings} that correspond potential properties in the 36 | * package.json data that contain config data. 37 | * * `configs` {Array} of config {Objects}. 38 | * 39 | * Returns {String} of path to package.json file. 40 | */ 41 | function _addPackageJSON(places, configs) { 42 | const packageFile = cc.find('package.json'); 43 | if (packageFile) { 44 | const packageJSON = cc.json(packageFile); 45 | places.forEach(place => { 46 | if ({}.hasOwnProperty.call(packageJSON, 'config') && 47 | {}.hasOwnProperty.call(packageJSON.config, place)) { 48 | configs.push(packageJSON.config[place]); 49 | } 50 | if ({}.hasOwnProperty.call(packageJSON, place)) { 51 | configs.push(packageJSON[place]); 52 | } 53 | }); 54 | } 55 | return packageFile; 56 | } 57 | 58 | /** 59 | * Public: loads config from multiple sources and combines them in an expected order. 60 | * 61 | * * `name` {String} name of the app or process that is used to identify which 62 | * config files to load. 63 | * * `defaults` (optional) {Object} default values to load in. 64 | * * `argv` (optional) {Function} custom argv parser, defaults to 65 | * [minimist](https://www.npmjs.com/package/minimist) if none is provided. 66 | * * `parse` (optional) {Function} custom parser, if none is provided, defaults 67 | * to utils.parse. 68 | * 69 | * ## Examples 70 | * 71 | * ```js 72 | * var config = require('config-attendant')('foo', { defaultOpts: 'value' }); 73 | * ``` 74 | * 75 | * Returns {Object} combined config properties. 76 | */ 77 | export default function config(name, defaults = {}, argv = minimist, parse = cc.parse) { 78 | if (typeof name !== 'string') throw new Error('rc(name): name *must* be string'); 79 | let _defaults = (typeof defaults === 'string') ? cc.json(defaults) : defaults; 80 | if (!_defaults) _defaults = {}; 81 | const env = cc.env(`${name}_`); 82 | 83 | const configs = [_defaults]; 84 | const configFiles = []; 85 | 86 | // which files do we look at? 87 | if (!win) { 88 | [join(etc, name, 'config'), join(etc, `${name}rc`)].forEach((configFile) => { 89 | _addConfigFile(configFile, configs, configFiles, parse); 90 | }); 91 | } 92 | if (home) { 93 | [join(home, '.config', name, 'config'), 94 | join(home, '.config', name), 95 | join(home, `.${name}`, 'config'), 96 | join(home, `.${name}rc`)].forEach((configFile) => { 97 | _addConfigFile(configFile, configs, configFiles, parse); 98 | }); 99 | } 100 | const packageFile = _addPackageJSON([name], configs); 101 | _addConfigFile(cc.find(`.${name}rc`), configs, configFiles, parse); 102 | if (env.config) _addConfigFile(env.config, configs, configFiles, parse); 103 | if (argv.config) _addConfigFile(argv.config, configs, configFiles, parse); 104 | return deepExtend(...configs.concat([ 105 | env, 106 | argv, 107 | configFiles.length ? { configs: configFiles, 108 | config: configFiles[configFiles.length - 1] } : undefined, 109 | packageFile ? { packageFile } : undefined, 110 | ])); 111 | } 112 | -------------------------------------------------------------------------------- /src/lib/utils.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import ini from 'ini'; 3 | import path from 'path'; 4 | import stripJsonComments from 'strip-json-comments'; 5 | 6 | const utils = { 7 | /** 8 | * Public: parses content if it is json or ini data. 9 | * 10 | * * `content` {String} configuration data. 11 | * 12 | * ## Examples 13 | * 14 | * ```js 15 | * var cc = require('config-attendant/lib/utils'); 16 | * var configObj = cc.parse(fs.readFileSync('file.json', 'utf-8')); 17 | * ``` 18 | * 19 | * Returns {Object} configuration keys and values. 20 | */ 21 | parse: (content) => { 22 | // if it ends in .json or starts with { then it must be json. 23 | // must be done this way, because ini accepts everything. 24 | // can't just try and parse it and let it throw if it's not ini. 25 | // everything is ini. even json with a syntax error. 26 | 27 | if (/^\s*{/.test(content)) return JSON.parse(stripJsonComments(content)); 28 | return ini.parse(content); 29 | }, 30 | 31 | /** 32 | * Public: joins segments and reads file. 33 | * 34 | * * `segments` {String} or {Array} of {Strings} passed to `path.join` 35 | * 36 | * ## Examples 37 | * 38 | * ```js 39 | * var cc = require('config-attendant/lib/utils'); 40 | * var content = cc.file('string-to-file.ini'); 41 | * ``` 42 | * 43 | * Returns {String} file contents. 44 | * Return {null} if file is not found. 45 | */ 46 | file: (segments) => { 47 | if (!segments) return null; 48 | const filepath = path.join(segments); 49 | try { 50 | return fs.readFileSync(filepath, 'utf-8'); 51 | } catch (err) { 52 | return null; 53 | } 54 | }, 55 | 56 | /** 57 | * Public: reads and parses json file. 58 | * 59 | * * `filepath` {String} or {Array} of {Strings} passed to `path.join` 60 | * 61 | * ## Examples 62 | * 63 | * ```js 64 | * var cc = require('config-attendant/lib/utils'); 65 | * var jsonObj = cc.json('string-to-file.json'); 66 | * ``` 67 | * 68 | * Returns parsed config {Object} 69 | */ 70 | json: (filepath) => { 71 | const content = utils.file(filepath); 72 | return content ? utils.parse(content) : null; 73 | }, 74 | 75 | /** 76 | * Public: gathers all the env variables containing the correct prefix and parses 77 | * sub objects based on env name strings. 78 | * 79 | * * `prefix` {String} config appname. 80 | * * `envObj` (Optional) {Object} containing the user environment variables. 81 | * 82 | * ## Examples 83 | * 84 | * ```js 85 | * process.env['app_someOpt__a'] = 42; 86 | * process.env['app_someOpt__x__'] = 99; 87 | * process.env['app_someOpt__a__b'] = 186; 88 | * process.env['app_someOpt__a__b__c'] = 243; 89 | * process.env['app_someOpt__x__y'] = 1862; 90 | * process.env['app_someOpt__z'] = 186577; 91 | * var cc = require('./lib/utils'); 92 | * console.log(cc.env('app'+'_')); 93 | * // { someOpt: { a: '42', x: '99', z: '186577' } } 94 | * ``` 95 | * 96 | * Returns {Object} of env variables together. 97 | */ 98 | env: (prefix, envObj = process.env) => { 99 | const obj = {}; 100 | const l = prefix.length; 101 | 102 | Object.keys(envObj).forEach((k) => { 103 | if ((k.indexOf(prefix)) === 0) { 104 | const keypath = k.substring(l).split('__'); 105 | // Trim empty strings from keypath array 106 | let _emptyStringIndex = keypath.indexOf(''); 107 | while (_emptyStringIndex > -1) { 108 | keypath.splice(_emptyStringIndex, 1); 109 | _emptyStringIndex = keypath.indexOf(''); 110 | } 111 | let cursor = obj; 112 | keypath.forEach((_subkey, i) => { 113 | // (check for _subkey first so we ignore empty strings) 114 | // (check for cursor to avoid assignment to primitive objects) 115 | if (!_subkey || typeof cursor !== 'object') return; 116 | // If this is the last key, just stuff the value in there 117 | // Assigns actual value from env variable to final key 118 | // (unless it's just an empty string- in that case use the last valid key) 119 | if (i === keypath.length - 1) cursor[_subkey] = envObj[k]; 120 | // Build sub-object if nothing already exists at the keypath 121 | if (cursor[_subkey] === undefined) cursor[_subkey] = {}; 122 | // Increment cursor used to track the object at the current depth 123 | cursor = cursor[_subkey]; 124 | }); 125 | } 126 | }); 127 | return obj; 128 | }, 129 | 130 | /** 131 | * Public: finds file in current directory or in any parent directory until arriving at root. 132 | * 133 | * * `filename` {String} of file to find. 134 | * * `start` (Optional) {String} of starting directory. 135 | * 136 | * ## Examples 137 | * 138 | * ```js 139 | * var cc = require('config-attendant/lib/utils'); 140 | * var configFilePath = cc.find('string-to-file.ini'); 141 | * ``` 142 | * 143 | * Returns {String} of path to file if found. 144 | * Returns {null} if file not found. 145 | */ 146 | find: (filename, start = process.cwd()) => { 147 | const filepath = path.join(start, filename); 148 | try { 149 | fs.statSync(filepath); 150 | return filepath; 151 | } catch (err) { 152 | if (path.dirname(start) !== start) { // root 153 | return utils.find(filename, path.dirname(start)); 154 | } 155 | } 156 | return null; 157 | }, 158 | }; 159 | 160 | export default utils; 161 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | import fs from 'fs'; 2 | import path from 'path'; 3 | import test from 'ava'; 4 | import configAttendant from '../src/lib/index'; 5 | 6 | let n; 7 | 8 | function removeProps(obj, props) { 9 | for (let i = 0; i < props.length; i++) { 10 | delete obj[props[i]]; 11 | } 12 | return obj; 13 | } 14 | 15 | test.before(() => { 16 | n = `rc${Math.random()}`; 17 | }); 18 | 19 | test('Throws error if appname not a string', t => { 20 | t.throws(() => { 21 | configAttendant({ option: true }); 22 | }, /rc\(name\): name \*must\* be string/); 23 | }); 24 | 25 | test('should use package.json data', t => { 26 | const dir = path.resolve(__dirname, 'project'); 27 | const expected = { 28 | option: true, 29 | package: 21, 30 | }; 31 | const initDir = process.cwd(); 32 | process.chdir(dir); 33 | const config = configAttendant('foo', { 34 | option: true, 35 | }); 36 | removeProps(config, ['_', 'packageFile']); 37 | t.deepEqual(config, expected); 38 | process.chdir(initDir); 39 | }); 40 | 41 | test('should use config in package.json data', t => { 42 | const dir = path.resolve(__dirname, 'project-config'); 43 | const expected = { 44 | option: false, 45 | envOption: 22, 46 | }; 47 | const initDir = process.cwd(); 48 | process.chdir(dir); 49 | const config = configAttendant('foo', { 50 | option: true, 51 | }); 52 | removeProps(config, ['_', 'packageFile']); 53 | t.deepEqual(config, expected); 54 | process.chdir(initDir); 55 | }); 56 | 57 | test('Env variable overrides default', t => { 58 | process.env[`${n}_envOption`] = 42; 59 | const config = configAttendant(n, { 60 | option: true, 61 | }); 62 | removeProps(config, ['_', 'packageFile']); 63 | const expected = { option: true, envOption: '42' }; 64 | t.deepEqual(config, expected); 65 | delete process.env[`${n}_envOption`]; 66 | }); 67 | 68 | test('Custom Argv', t => { 69 | const config = configAttendant(n, { 70 | option: true, 71 | }, { 72 | option: false, 73 | envOption: 24, 74 | argv: { 75 | remain: [], 76 | cooked: ['--no-option', '--envOption', '24'], 77 | original: ['--no-option', '--envOption=24'], 78 | }, 79 | }); 80 | removeProps(config, ['argv', 'packageFile']); 81 | const expected = { option: false, envOption: 24 }; 82 | t.deepEqual(config, expected); 83 | }); 84 | 85 | test('Json rc', t => { 86 | const jsonrc = path.resolve(`.${n}rc`); 87 | fs.writeFileSync(jsonrc, [ 88 | '{', 89 | '// json overrides default', 90 | '"option": false,', 91 | '/* env overrides json */', 92 | '"envOption": 24', 93 | '}', 94 | ].join('\n')); 95 | const config = configAttendant(n, { 96 | option: true, 97 | }); 98 | fs.unlinkSync(jsonrc); 99 | removeProps(config, ['_', 'packageFile']); 100 | const expected = { option: false, envOption: 24, config: jsonrc, configs: [jsonrc] }; 101 | t.deepEqual(config, expected); 102 | }); 103 | 104 | test('JSON string defaults', t => { 105 | const config = configAttendant(n, path.resolve('defaults.json')); 106 | removeProps(config, ['_', 'packageFile']); 107 | const expected = { option: false, envOption: 24 }; 108 | t.deepEqual(config, expected); 109 | }); 110 | 111 | test('JSON string defaults, missing file', t => { 112 | const config = configAttendant(n, path.resolve('missing.json')); 113 | removeProps(config, ['_', 'packageFile']); 114 | t.deepEqual(config, {}); 115 | }); 116 | 117 | test('Use env variable to set config default values', t => { 118 | process.env[`${n}_config`] = './defaults.json'; 119 | const config = configAttendant(n, { 120 | option: true, 121 | }); 122 | const expected = { 123 | option: false, 124 | envOption: 24, 125 | config: './defaults.json', 126 | configs: ['./defaults.json'], 127 | }; 128 | removeProps(config, ['_', 'packageFile']); 129 | t.deepEqual(config, expected); 130 | delete process.env[`${n}_config`]; 131 | }); 132 | 133 | test('Nested env variables', t => { 134 | // Basic usage 135 | process.env[`${n}_someOpt__a`] = 42; 136 | process.env[`${n}_someOpt__x__`] = 99; 137 | process.env[`${n}_someOpt__a__b`] = 186; 138 | process.env[`${n}_someOpt__a__b__c`] = 243; 139 | process.env[`${n}_someOpt__x__y`] = 1862; 140 | process.env[`${n}_someOpt__z`] = 186577; 141 | 142 | // Should ignore empty strings from orphaned '__' 143 | process.env[`${n}_someOpt__z__x__`] = 18629; 144 | process.env[`${n}_someOpt__w__w__`] = 18629; 145 | 146 | // Leading '__' should ignore everything up to 'z' 147 | process.env[`${n}___z__i__`] = 9999; 148 | 149 | const config = configAttendant(n, { 150 | option: true, 151 | }); 152 | 153 | const expected = { 154 | option: true, 155 | someOpt: { 156 | a: '42', 157 | x: '99', 158 | z: '186577', 159 | w: { 160 | w: '18629', 161 | }, 162 | }, 163 | z: { i: '9999' }, 164 | }; 165 | removeProps(config, ['_', 'packageFile']); 166 | t.deepEqual(config, expected); 167 | delete process.env[`${n}_someOpt__a`]; 168 | delete process.env[`${n}_someOpt__x__`]; 169 | delete process.env[`${n}_someOpt__a__b`]; 170 | delete process.env[`${n}_someOpt__a__b__c`]; 171 | delete process.env[`${n}_someOpt__x__y`]; 172 | delete process.env[`${n}_someOpt__z`]; 173 | delete process.env[`${n}_someOpt__z__x__`]; 174 | delete process.env[`${n}_someOpt__w__w__`]; 175 | delete process.env[`${n}___z__i__`]; 176 | }); 177 | 178 | test('should handle config file added more than once', t => { 179 | const dir = path.resolve(__dirname, 'duplicate-config'); 180 | const configPath = path.resolve(__dirname, 'duplicate-config', '.thingrc'); 181 | const expected = { 182 | option: false, 183 | envOption: 24, 184 | config: configPath, 185 | configs: [configPath], 186 | packageFile: path.resolve(__dirname, '..', 'package.json'), 187 | }; 188 | process.env.thing_config = configPath; 189 | const initDir = process.cwd(); 190 | process.chdir(dir); 191 | const config = configAttendant('thing'); 192 | removeProps(config, ['_']); 193 | t.deepEqual(config, expected); 194 | process.chdir(initDir); 195 | delete process.env.thing_config; 196 | }); 197 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Config Attendant 2 | 3 | [![Build Status](https://travis-ci.org/GarthDB/config-attendant.svg?branch=master)](https://travis-ci.org/GarthDB/config-attendant) [![codecov](https://codecov.io/gh/GarthDB/config-attendant/branch/master/graph/badge.svg)](https://codecov.io/gh/GarthDB/config-attendant) [![Dependency Status](https://david-dm.org/GarthDB/config-attendant.svg)](https://david-dm.org/GarthDB/config-attendant) [![npm version](https://badge.fury.io/js/config-attendant.svg)](https://badge.fury.io/js/config-attendant) 4 | 5 | --- 6 | 7 | ## Usage 8 | 9 | ### JS 10 | 11 | The only option is to pass Config Attendant the name of your app, and your default configuration. 12 | 13 | ```js 14 | var conf = require('config-attendant')(appname, { 15 | //defaults go here. 16 | port: 2468, 17 | 18 | //defaults which are objects will be merged, not replaced 19 | views: { 20 | engine: 'jade' 21 | } 22 | }); 23 | ``` 24 | 25 | Config Attendant will return your configuration options merged with the defaults you specify. 26 | If you pass in a predefined defaults object, it will be mutated: 27 | 28 | ```js 29 | var conf = {}; 30 | require('config-attendant')(appname, conf); 31 | ``` 32 | 33 | If Config Attendant finds any config files for your app, the returned config object will have 34 | a `configs` array containing their paths: 35 | 36 | ```js 37 | var appCfg = require('config-attendant')(appname, conf); 38 | appCfg.configs[0] // /etc/appnamerc 39 | appCfg.configs[1] // /home/dominictarr/.config/appname 40 | appCfg.config // same as appCfg.configs[appCfg.configs.length - 1] 41 | ``` 42 | 43 | ### CLI 44 | 45 | #### Installation 46 | 47 | To use Config Attendant as a command line tool (nice for testing out configurations), install it globally via npm. 48 | 49 | ```sh 50 | npm install -g config-attendant 51 | ``` 52 | 53 | Parameters passed as strings are interpreted as app names, flags and values are interpreted as configuration values. 54 | 55 | ```sh 56 | $ config appname --prop 'value' 57 | { 58 | "_": [ 59 | "appname" 60 | ], 61 | "prop": "value" 62 | } 63 | ``` 64 | 65 | The only reserved flag is `--config` which can be used to point to json config files to load as well. 66 | 67 | ```sh 68 | $ config appname --prop 'value' --config config-attendant/test/defaults.json 69 | { 70 | "option": false, 71 | "envOption": 24, 72 | "_": [ 73 | "appname" 74 | ], 75 | "prop": "value", 76 | "config": "config-attendant/test/defaults.json", 77 | "configs": [ 78 | "config-attendant/test/defaults.json" 79 | ] 80 | } 81 | ``` 82 | 83 | ## Standards 84 | 85 | Given your application name (`appname`), Config Attendant will look in all the obvious places for configuration. 86 | 87 | * command line arguments (parsed by minimist) 88 | * environment variables prefixed with `${appname}_` 89 | * or use "\_\_" to indicate nested properties 90 | _(e.g. `appname_foo__bar__baz` => `foo.bar.baz`)_ 91 | * if you passed an option `--config file` then from that file 92 | * a local `.${appname}rc` or the first found looking in `./ ../ ../../ ../../../` etc. 93 | * a local `package.json` file or the first found looking in `./ ../ ../../ ../../../` etc. 94 | * within the `package.json` the `${appname}` value. 95 | * within the `package.json`, `config.${appname}`. 96 | * `$HOME/.${appname}rc` 97 | * `$HOME/.${appname}/config` 98 | * `$HOME/.config/${appname}` 99 | * `$HOME/.config/${appname}/config` 100 | * `/etc/${appname}rc` 101 | * `/etc/${appname}/config` 102 | * the defaults object you passed in. 103 | 104 | All configuration sources that were found will be flattened into one object, 105 | so that sources **earlier** in this list override later ones. 106 | 107 | ## Configuration File Formats 108 | 109 | Configuration files (e.g. `.appnamerc`) may be in either [json](http://json.org/example) or [ini](http://en.wikipedia.org/wiki/INI_file) format. The example configurations below are equivalent: 110 | 111 | ### Formatted as `ini` 112 | 113 | ```ini 114 | ; You can include comments in `ini` format if you want. 115 | 116 | dependsOn=0.10.0 117 | 118 | 119 | ; Config Attendant has built-in support for ini sections, see? 120 | 121 | [commands] 122 | www = ./commands/www 123 | console = ./commands/repl 124 | 125 | 126 | ; You can even do nested sections 127 | 128 | [generators.options] 129 | engine = ejs 130 | 131 | [generators.modules] 132 | new = generate-new 133 | engine = generate-backend 134 | 135 | ``` 136 | 137 | ### Formatted as `json` 138 | 139 | ```js 140 | { 141 | // You can even comment your JSON, if you want 142 | "dependsOn": "0.10.0", 143 | "commands": { 144 | "www": "./commands/www", 145 | "console": "./commands/repl" 146 | }, 147 | "generators": { 148 | "options": { 149 | "engine": "ejs" 150 | }, 151 | "modules": { 152 | "new": "generate-new", 153 | "backend": "generate-backend" 154 | } 155 | } 156 | } 157 | ``` 158 | 159 | Comments are stripped from JSON config via [strip-json-comments](https://github.com/sindresorhus/strip-json-comments). 160 | 161 | > Since ini, and env variables do not have a standard for types, your application needs be prepared for strings. 162 | 163 | ## Advanced Usage 164 | 165 | ### Pass in your own `argv` 166 | 167 | You may pass in your own `argv` as the third argument to Config Attendant. This is in case you want to [use your own command-line opts parser](https://github.com/dominictarr/rc/pull/12). 168 | 169 | ```js 170 | require('config-attendant')(appname, defaults, customArgvParser); 171 | ``` 172 | 173 | ## Pass in your own parser 174 | 175 | If you have a special need to use a non-standard parser, 176 | you can do so by passing in the parser as the 4th argument. 177 | (leave the 3rd as null to get the default args parser) 178 | 179 | ```js 180 | require('config-attendant')(appname, defaults, null, parser); 181 | ``` 182 | 183 | This may also be used to force a more strict format, 184 | such as strict, valid JSON only. 185 | 186 | ## Note on Performance 187 | 188 | Config Attendant is running `fs.statSync`-- so make sure you don't use it in a hot code path (e.g. a request handler) 189 | 190 | ## License 191 | 192 | Multi-licensed under the two-clause BSD License, MIT License, or Apache License, version 2.0 193 | -------------------------------------------------------------------------------- /lib/utils.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.defineProperty(exports, "__esModule", { 4 | value: true 5 | }); 6 | 7 | var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; 8 | 9 | var _fs = require('fs'); 10 | 11 | var _fs2 = _interopRequireDefault(_fs); 12 | 13 | var _ini = require('ini'); 14 | 15 | var _ini2 = _interopRequireDefault(_ini); 16 | 17 | var _path = require('path'); 18 | 19 | var _path2 = _interopRequireDefault(_path); 20 | 21 | var _stripJsonComments = require('strip-json-comments'); 22 | 23 | var _stripJsonComments2 = _interopRequireDefault(_stripJsonComments); 24 | 25 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 26 | 27 | var utils = { 28 | /** 29 | * Public: parses content if it is json or ini data. 30 | * 31 | * * `content` {String} configuration data. 32 | * 33 | * ## Examples 34 | * 35 | * ```js 36 | * var cc = require('config-attendant/lib/utils'); 37 | * var configObj = cc.parse(fs.readFileSync('file.json', 'utf-8')); 38 | * ``` 39 | * 40 | * Returns {Object} configuration keys and values. 41 | */ 42 | parse: function parse(content) { 43 | // if it ends in .json or starts with { then it must be json. 44 | // must be done this way, because ini accepts everything. 45 | // can't just try and parse it and let it throw if it's not ini. 46 | // everything is ini. even json with a syntax error. 47 | 48 | if (/^\s*{/.test(content)) return JSON.parse((0, _stripJsonComments2.default)(content)); 49 | return _ini2.default.parse(content); 50 | }, 51 | 52 | /** 53 | * Public: joins segments and reads file. 54 | * 55 | * * `segments` {String} or {Array} of {Strings} passed to `path.join` 56 | * 57 | * ## Examples 58 | * 59 | * ```js 60 | * var cc = require('config-attendant/lib/utils'); 61 | * var content = cc.file('string-to-file.ini'); 62 | * ``` 63 | * 64 | * Returns {String} file contents. 65 | * Return {null} if file is not found. 66 | */ 67 | file: function file(segments) { 68 | if (!segments) return null; 69 | var filepath = _path2.default.join(segments); 70 | try { 71 | return _fs2.default.readFileSync(filepath, 'utf-8'); 72 | } catch (err) { 73 | return null; 74 | } 75 | }, 76 | 77 | /** 78 | * Public: reads and parses json file. 79 | * 80 | * * `filepath` {String} or {Array} of {Strings} passed to `path.join` 81 | * 82 | * ## Examples 83 | * 84 | * ```js 85 | * var cc = require('config-attendant/lib/utils'); 86 | * var jsonObj = cc.json('string-to-file.json'); 87 | * ``` 88 | * 89 | * Returns parsed config {Object} 90 | */ 91 | json: function json(filepath) { 92 | var content = utils.file(filepath); 93 | return content ? utils.parse(content) : null; 94 | }, 95 | 96 | /** 97 | * Public: gathers all the env variables containing the correct prefix and parses 98 | * sub objects based on env name strings. 99 | * 100 | * * `prefix` {String} config appname. 101 | * * `envObj` (Optional) {Object} containing the user environment variables. 102 | * 103 | * ## Examples 104 | * 105 | * ```js 106 | * process.env['app_someOpt__a'] = 42; 107 | * process.env['app_someOpt__x__'] = 99; 108 | * process.env['app_someOpt__a__b'] = 186; 109 | * process.env['app_someOpt__a__b__c'] = 243; 110 | * process.env['app_someOpt__x__y'] = 1862; 111 | * process.env['app_someOpt__z'] = 186577; 112 | * var cc = require('./lib/utils'); 113 | * console.log(cc.env('app'+'_')); 114 | * // { someOpt: { a: '42', x: '99', z: '186577' } } 115 | * ``` 116 | * 117 | * Returns {Object} of env variables together. 118 | */ 119 | env: function env(prefix) { 120 | var envObj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : process.env; 121 | 122 | var obj = {}; 123 | var l = prefix.length; 124 | 125 | Object.keys(envObj).forEach(function (k) { 126 | if (k.indexOf(prefix) === 0) { 127 | (function () { 128 | var keypath = k.substring(l).split('__'); 129 | // Trim empty strings from keypath array 130 | var _emptyStringIndex = keypath.indexOf(''); 131 | while (_emptyStringIndex > -1) { 132 | keypath.splice(_emptyStringIndex, 1); 133 | _emptyStringIndex = keypath.indexOf(''); 134 | } 135 | var cursor = obj; 136 | keypath.forEach(function (_subkey, i) { 137 | // (check for _subkey first so we ignore empty strings) 138 | // (check for cursor to avoid assignment to primitive objects) 139 | if (!_subkey || (typeof cursor === 'undefined' ? 'undefined' : _typeof(cursor)) !== 'object') return; 140 | // If this is the last key, just stuff the value in there 141 | // Assigns actual value from env variable to final key 142 | // (unless it's just an empty string- in that case use the last valid key) 143 | if (i === keypath.length - 1) cursor[_subkey] = envObj[k]; 144 | // Build sub-object if nothing already exists at the keypath 145 | if (cursor[_subkey] === undefined) cursor[_subkey] = {}; 146 | // Increment cursor used to track the object at the current depth 147 | cursor = cursor[_subkey]; 148 | }); 149 | })(); 150 | } 151 | }); 152 | return obj; 153 | }, 154 | 155 | /** 156 | * Public: finds file in current directory or in any parent directory until arriving at root. 157 | * 158 | * * `filename` {String} of file to find. 159 | * * `start` (Optional) {String} of starting directory. 160 | * 161 | * ## Examples 162 | * 163 | * ```js 164 | * var cc = require('config-attendant/lib/utils'); 165 | * var configFilePath = cc.find('string-to-file.ini'); 166 | * ``` 167 | * 168 | * Returns {String} of path to file if found. 169 | * Returns {null} if file not found. 170 | */ 171 | find: function find(filename) { 172 | var start = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : process.cwd(); 173 | 174 | var filepath = _path2.default.join(start, filename); 175 | try { 176 | _fs2.default.statSync(filepath); 177 | return filepath; 178 | } catch (err) { 179 | if (_path2.default.dirname(start) !== start) { 180 | // root 181 | return utils.find(filename, _path2.default.dirname(start)); 182 | } 183 | } 184 | return null; 185 | } 186 | }; 187 | 188 | exports.default = utils; 189 | module.exports = exports['default']; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------