├── .editorconfig ├── .eslintrc.json ├── .gitattributes ├── .gitignore ├── .travis.yml ├── .verb.md ├── LICENSE ├── README.md ├── bower.json ├── index.js ├── package.json └── test ├── fixtures ├── .configrc └── .configrc.yml └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_style = space 6 | end_of_line = lf 7 | charset = utf-8 8 | indent_size = 2 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.md] 13 | trim_trailing_whitespace = false 14 | insert_final_newline = false 15 | 16 | [test/**] 17 | trim_trailing_whitespace = false 18 | insert_final_newline = false 19 | 20 | [templates/**] 21 | trim_trailing_whitespace = false 22 | insert_final_newline = false 23 | -------------------------------------------------------------------------------- /.eslintrc.json: -------------------------------------------------------------------------------- 1 | { 2 | "ecmaFeatures": { 3 | "modules": true, 4 | "experimentalObjectRestSpread": true 5 | }, 6 | "env": { 7 | "browser": false, 8 | "es6": true, 9 | "node": true, 10 | "mocha": true 11 | }, 12 | "globals": { 13 | "document": false, 14 | "navigator": false, 15 | "window": false 16 | }, 17 | "rules": { 18 | "accessor-pairs": 2, 19 | "arrow-spacing": [ 20 | 2, 21 | { 22 | "before": true, 23 | "after": true 24 | } 25 | ], 26 | "block-spacing": [ 27 | 2, 28 | "always" 29 | ], 30 | "brace-style": [ 31 | 2, 32 | "1tbs", 33 | { 34 | "allowSingleLine": true 35 | } 36 | ], 37 | "comma-dangle": [ 38 | 2, 39 | "never" 40 | ], 41 | "comma-spacing": [ 42 | 2, 43 | { 44 | "before": false, 45 | "after": true 46 | } 47 | ], 48 | "comma-style": [ 49 | 2, 50 | "last" 51 | ], 52 | "constructor-super": 2, 53 | "curly": [ 54 | 2, 55 | "multi-line" 56 | ], 57 | "dot-location": [ 58 | 2, 59 | "property" 60 | ], 61 | "eol-last": 2, 62 | "eqeqeq": [ 63 | 2, 64 | "allow-null" 65 | ], 66 | "generator-star-spacing": [ 67 | 2, 68 | { 69 | "before": true, 70 | "after": true 71 | } 72 | ], 73 | "handle-callback-err": [ 74 | 2, 75 | "^(err|error)$" 76 | ], 77 | "indent": [ 78 | 2, 79 | 2, 80 | { 81 | "SwitchCase": 1 82 | } 83 | ], 84 | "key-spacing": [ 85 | 2, 86 | { 87 | "beforeColon": false, 88 | "afterColon": true 89 | } 90 | ], 91 | "new-cap": [ 92 | 2, 93 | { 94 | "newIsCap": true, 95 | "capIsNew": false 96 | } 97 | ], 98 | "new-parens": 2, 99 | "no-array-constructor": 2, 100 | "no-caller": 2, 101 | "no-class-assign": 2, 102 | "no-cond-assign": 2, 103 | "no-const-assign": 2, 104 | "no-control-regex": 2, 105 | "no-debugger": 2, 106 | "no-delete-var": 2, 107 | "no-dupe-args": 2, 108 | "no-dupe-class-members": 2, 109 | "no-dupe-keys": 2, 110 | "no-duplicate-case": 2, 111 | "no-empty-character-class": 2, 112 | "no-empty-label": 2, 113 | "no-eval": 2, 114 | "no-ex-assign": 2, 115 | "no-extend-native": 2, 116 | "no-extra-bind": 2, 117 | "no-extra-boolean-cast": 2, 118 | "no-extra-parens": [ 119 | 2, 120 | "functions" 121 | ], 122 | "no-fallthrough": 2, 123 | "no-floating-decimal": 2, 124 | "no-func-assign": 2, 125 | "no-implied-eval": 2, 126 | "no-inner-declarations": [ 127 | 2, 128 | "functions" 129 | ], 130 | "no-invalid-regexp": 2, 131 | "no-irregular-whitespace": 2, 132 | "no-iterator": 2, 133 | "no-label-var": 2, 134 | "no-labels": 2, 135 | "no-lone-blocks": 2, 136 | "no-mixed-spaces-and-tabs": 2, 137 | "no-multi-spaces": 2, 138 | "no-multi-str": 2, 139 | "no-multiple-empty-lines": [ 140 | 2, 141 | { 142 | "max": 1 143 | } 144 | ], 145 | "no-native-reassign": 2, 146 | "no-negated-in-lhs": 2, 147 | "no-new": 2, 148 | "no-new-func": 2, 149 | "no-new-object": 2, 150 | "no-new-require": 2, 151 | "no-new-wrappers": 2, 152 | "no-obj-calls": 2, 153 | "no-octal": 2, 154 | "no-octal-escape": 2, 155 | "no-proto": 0, 156 | "no-redeclare": 2, 157 | "no-regex-spaces": 2, 158 | "no-return-assign": 2, 159 | "no-self-compare": 2, 160 | "no-sequences": 2, 161 | "no-shadow-restricted-names": 2, 162 | "no-spaced-func": 2, 163 | "no-sparse-arrays": 2, 164 | "no-this-before-super": 2, 165 | "no-throw-literal": 2, 166 | "no-trailing-spaces": 0, 167 | "no-undef": 2, 168 | "no-undef-init": 2, 169 | "no-unexpected-multiline": 2, 170 | "no-unneeded-ternary": [ 171 | 2, 172 | { 173 | "defaultAssignment": false 174 | } 175 | ], 176 | "no-unreachable": 2, 177 | "no-unused-vars": [ 178 | 2, 179 | { 180 | "vars": "all", 181 | "args": "none" 182 | } 183 | ], 184 | "no-useless-call": 0, 185 | "no-with": 2, 186 | "one-var": [ 187 | 0, 188 | { 189 | "initialized": "never" 190 | } 191 | ], 192 | "operator-linebreak": [ 193 | 0, 194 | "after", 195 | { 196 | "overrides": { 197 | "?": "before", 198 | ":": "before" 199 | } 200 | } 201 | ], 202 | "padded-blocks": [ 203 | 0, 204 | "never" 205 | ], 206 | "quotes": [ 207 | 2, 208 | "single", 209 | "avoid-escape" 210 | ], 211 | "radix": 2, 212 | "semi": [ 213 | 2, 214 | "always" 215 | ], 216 | "semi-spacing": [ 217 | 2, 218 | { 219 | "before": false, 220 | "after": true 221 | } 222 | ], 223 | "space-after-keywords": [ 224 | 2, 225 | "always" 226 | ], 227 | "space-before-blocks": [ 228 | 2, 229 | "always" 230 | ], 231 | "space-before-function-paren": [ 232 | 2, 233 | "never" 234 | ], 235 | "space-before-keywords": [ 236 | 2, 237 | "always" 238 | ], 239 | "space-in-parens": [ 240 | 2, 241 | "never" 242 | ], 243 | "space-infix-ops": 2, 244 | "space-return-throw-case": 2, 245 | "space-unary-ops": [ 246 | 2, 247 | { 248 | "words": true, 249 | "nonwords": false 250 | } 251 | ], 252 | "spaced-comment": [ 253 | 0, 254 | "always", 255 | { 256 | "markers": [ 257 | "global", 258 | "globals", 259 | "eslint", 260 | "eslint-disable", 261 | "*package", 262 | "!", 263 | "," 264 | ] 265 | } 266 | ], 267 | "use-isnan": 2, 268 | "valid-typeof": 2, 269 | "wrap-iife": [ 270 | 2, 271 | "any" 272 | ], 273 | "yoda": [ 274 | 2, 275 | "never" 276 | ] 277 | } 278 | } 279 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Enforce Unix newlines 2 | * text eol=lf 3 | 4 | # binaries 5 | *.ai binary 6 | *.psd binary 7 | *.jpg binary 8 | *.gif binary 9 | *.png binary 10 | *.jpeg binary 11 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | 4 | *.rar 5 | *.zip 6 | tmp 7 | temp 8 | TODO.md 9 | 10 | *.sublime-* -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - "stable" 5 | - "4" 6 | - "0.12" 7 | - "0.10" 8 | matrix: 9 | fast_finish: true 10 | allow_failures: 11 | - node_js: "0.10" 12 | -------------------------------------------------------------------------------- /.verb.md: -------------------------------------------------------------------------------- 1 | # {%= name %} {%= badge("fury") %} 2 | 3 | > {%= description %} 4 | 5 | ## Heads up! Breaking changes 6 | 7 | v0.3.0 was a complete refactor. The readme [API](#API) section describes the new API. 8 | 9 | ## Install 10 | {%= include("install-npm", {save: true}) %} 11 | 12 | ## Usage 13 | 14 | ```js 15 | var config = require('{%= name %}'); 16 | ``` 17 | 18 | ## API 19 | {%= apidocs("index.js") %} 20 | 21 | ## Related projects 22 | {%= related(verb.related.list) %} 23 | 24 | ## Running tests 25 | 26 | Install dev dependencies: 27 | 28 | ```sh 29 | $ npm i -d && npm i -g verb-cli && npm test 30 | ``` 31 | 32 | ## Contributing 33 | {%= include("contributing") %} 34 | 35 | ## Author 36 | {%= include("author") %} 37 | 38 | ## License 39 | {%= copyright({start: 2014, linkify: true}) %} 40 | {%= license %} 41 | 42 | *** 43 | 44 | {%= include("footer") %} 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014-2015, Jon Schlinkert. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # config-file [![NPM version](https://badge.fury.io/js/config-file.svg)](http://badge.fury.io/js/config-file) 2 | 3 | > Find and load a YAML or JSON config file (like .jshintrc, package.json, bower.json etc) from either a local project, installed npm module, or the user's home directory. 4 | 5 | ## Heads up! Breaking changes 6 | 7 | v0.3.0 was a complete refactor. The readme [API](#API) section describes the new API. 8 | 9 | ## Install 10 | 11 | Install with [npm](https://www.npmjs.com/) 12 | 13 | ```sh 14 | $ npm i config-file --save 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```js 20 | var config = require('config-file'); 21 | ``` 22 | 23 | ## API 24 | 25 | ### [config](index.js#L29) 26 | 27 | Returns an object from parsing JSON or YAML from the given config file. Uses `config.resovle` to resolve the filepath. If no filepath is specified, `config.resolve` falls back to 'package.json' 28 | 29 | **Params** 30 | 31 | * `filename` **{String}**: The name of the file to parse 32 | * `options` **{Object}**: Optionally specify `{parse:'json'}` or `{parse:'yaml'}` 33 | * `returns` **{Object}** 34 | 35 | **Example** 36 | 37 | ```js 38 | var opts = config('.jshintrc'); 39 | ``` 40 | 41 | ### [.npm](index.js#L53) 42 | 43 | Parse a config file located in a locally installed npm package (in `node_modules`). 44 | 45 | **Params** 46 | 47 | * `moduleName` **{String}**: The name of the npm package to search in `node_modules` 48 | * `filename` **{String}**: Name of the file to find. 49 | * `options` **{Object}** 50 | 51 | **Example** 52 | 53 | ```js 54 | var data = config.npm('read-data', 'package.json'); 55 | //=> { name: "read-data", ... } 56 | ``` 57 | 58 | ### [.global](index.js#L78) 59 | 60 | Parse a config file in a globally installed npm package. 61 | 62 | **Params** 63 | 64 | * `moduleName` **{String}**: The name of the global module to search 65 | * `filename` **{String}**: Name of the file to find. 66 | * `options` **{Object}** 67 | 68 | **Example** 69 | 70 | ```js 71 | var data = config.global('verb-cli', 'package.json'); 72 | //=> { name: "verb-cli", ... } 73 | ``` 74 | 75 | ### [.home](index.js#L100) 76 | 77 | Return a filepath the user's home directory 78 | 79 | **Params** 80 | 81 | * `filepath` **{String}**: Filepath to find 82 | * `options` **{Object}** 83 | 84 | **Example** 85 | 86 | ```js 87 | var data = config.home('.jshintrc'); 88 | ``` 89 | 90 | ### [.resolve](index.js#L120) 91 | 92 | Returns the fully resolve path for the specified config file. Searches the local project first, then the user's home directory. 93 | 94 | **Params** 95 | 96 | * `filepath` **{String}**: Filepath to find 97 | * `options` **{Object}** 98 | * `returns` **{String}**: filepath to config file 99 | 100 | **Example** 101 | 102 | ```js 103 | var fp = config.resolve('.jshintrc'); 104 | //=> '/Users/jonschlinkert/dev/config-file/package.json' 105 | ``` 106 | 107 | ### [.parse](index.js#L142) 108 | 109 | Parse a config file. Same as using `config()`. 110 | 111 | **Params** 112 | 113 | * `filename` **{String}**: Name of the file to parse. 114 | * `options` **{Object}** 115 | * `returns` **{Object}** 116 | 117 | **Example** 118 | 119 | ```js 120 | var data = config.parse('.jshintrc'); 121 | ``` 122 | 123 | ## Related projects 124 | 125 | * [global-modules](https://github.com/jonschlinkert/global-modules): The directory used by npm for globally installed npm modules. 126 | * [look-up](https://github.com/jonschlinkert/look-up): Like findup-sync and supports the same features but 20x-40x faster on avg. 127 | * [read-data](https://github.com/jonschlinkert/read-data): Read JSON or YAML files. 128 | * [read-yaml](https://github.com/jonschlinkert/read-yaml): Very thin wrapper around js-yaml for directly reading in YAML files. 129 | 130 | ## Running tests 131 | 132 | Install dev dependencies: 133 | 134 | ```sh 135 | $ npm i -d && npm i -g verb-cli && npm test 136 | ``` 137 | 138 | ## Contributing 139 | 140 | Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](https://github.com/jonschlinkert/config-file/issues/new) 141 | 142 | ## Author 143 | 144 | **Jon Schlinkert** 145 | 146 | + [github/jonschlinkert](https://github.com/jonschlinkert) 147 | + [twitter/jonschlinkert](http://twitter.com/jonschlinkert) 148 | 149 | ## License 150 | 151 | Copyright © 2014-2015 [Jon Schlinkert](https://github.com/jonschlinkert) 152 | Released under the MIT license. 153 | 154 | *** 155 | 156 | _This file was generated by [verb-cli](https://github.com/assemble/verb-cli) on July 06, 2015._ -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "config-file", 3 | "description": "Find and load a YAML or JSON config file (like .jshintrc, package.json, bower.json etc) from either a local project, installed npm module, or the user's home directory.", 4 | "repository": "jonschlinkert/config-file", 5 | "license": "MIT", 6 | "homepage": "https://github.com/jonschlinkert/config-file", 7 | "authors": [ 8 | "Jon Schlinkert (https://github.com/jonschlinkert)" 9 | ], 10 | "main": [ 11 | "index.js" 12 | ], 13 | "dependencies": { 14 | "extend-shallow": "^2.0.0", 15 | "look-up": "^0.7.1", 16 | "read-data": "^0.3.0" 17 | }, 18 | "keywords": [ 19 | "config", 20 | "configuration", 21 | "detect", 22 | "file", 23 | "find", 24 | "get", 25 | "glob", 26 | "json", 27 | "load", 28 | "look", 29 | "rc", 30 | "resolve", 31 | "runtime", 32 | "runtime-config", 33 | "up", 34 | "yaml" 35 | ] 36 | } 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * Module dependencies 5 | */ 6 | 7 | var path = require('path'); 8 | var globalDir = require('global-modules'); 9 | var extend = require('extend-shallow'); 10 | var lookup = require('look-up'); 11 | var read = require('read-data'); 12 | var homedir = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE; 13 | 14 | /** 15 | * Returns an object from parsing JSON or YAML from the given 16 | * config file. Uses `config.resolve` to resolve the filepath. 17 | * If no filepath is specified, `config.resolve` falls back to 18 | * 'package.json' 19 | * 20 | * ```js 21 | * var opts = config('.jshintrc'); 22 | * ``` 23 | * @param {String} `filename` The name of the file to parse 24 | * @param {Object} `options` Optionally specify `{parse:'json'}` or `{parse:'yaml'}` 25 | * @return {Object} 26 | * @api public 27 | */ 28 | 29 | function config(filename, options) { 30 | return config.parse(filename, options); 31 | } 32 | 33 | /** 34 | * Expose `globalDir` 35 | */ 36 | 37 | config.globalDir = globalDir; 38 | 39 | /** 40 | * Parse a config file located in a locally installed npm 41 | * package (in `node_modules`). 42 | * 43 | * ```js 44 | * var data = config.npm('read-data', 'package.json'); 45 | * //=> { name: "read-data", ... } 46 | * ``` 47 | * @param {String} `moduleName` The name of the npm package to search in `node_modules` 48 | * @param {String} `filename` Name of the file to find. 49 | * @param {Object} `options` 50 | * @api public 51 | */ 52 | 53 | config.npm = function npmConfig(moduleName, filename, opts) { 54 | if (typeof filename === 'object') { 55 | opts = filename; 56 | filename = null; 57 | } 58 | 59 | opts = opts || {}; 60 | var base = path.join('node_modules', opts.base || '', moduleName); 61 | opts.cwd = path.resolve(opts.cwd || '', base); 62 | return config.parse(filename, opts); 63 | }; 64 | 65 | /** 66 | * Parse a config file in a globally installed npm package. 67 | * 68 | * ```js 69 | * var data = config.global('verb-cli', 'package.json'); 70 | * //=> { name: "verb-cli", ... } 71 | * ``` 72 | * @param {String} `moduleName` The name of the global module to search 73 | * @param {String} `filename` Name of the file to find. 74 | * @param {Object} `options` 75 | * @api public 76 | */ 77 | 78 | config.global = function globalConfig(moduleName, filename, opts) { 79 | if (typeof filename === 'object') { 80 | opts = filename; 81 | filename = null; 82 | } 83 | 84 | opts = opts || {}; 85 | opts.cwd = path.resolve(globalDir, opts.cwd || '', moduleName); 86 | return config.parse(filename, opts); 87 | }; 88 | 89 | /** 90 | * Return a filepath the user's home directory 91 | * 92 | * ```js 93 | * var data = config.home('.jshintrc'); 94 | * ``` 95 | * @param {String} `filepath` Filepath to find 96 | * @param {Object} `options` 97 | * @api public 98 | */ 99 | 100 | config.home = function homeConfig(filename, opts) { 101 | opts = opts || {}; 102 | var cwd = path.join(homedir, opts.cwd || ''); 103 | return config.parse(filename, { cwd: cwd }); 104 | }; 105 | 106 | /** 107 | * Returns the fully resolve path for the specified config file. 108 | * Searches the local project first, then the user's home directory. 109 | * 110 | * ```js 111 | * var fp = config.resolve('.jshintrc'); 112 | * //=> '/Users/jonschlinkert/dev/config-file/package.json' 113 | * ``` 114 | * @param {String} `filepath` Filepath to find 115 | * @param {Object} `options` 116 | * @returns {String} filepath to config file 117 | * @api public 118 | */ 119 | 120 | config.resolve = function resolveConfig(filename, options) { 121 | if (typeof filename === 'object') { 122 | options = filename; 123 | filename = null; 124 | } 125 | var opts = extend({cwd: process.cwd()}, options); 126 | filename = filename || 'package.json'; 127 | return lookup(filename, opts); 128 | }; 129 | 130 | /** 131 | * Parse a config file. Same as using `config()`. 132 | * 133 | * ```js 134 | * var data = config.parse('.jshintrc'); 135 | * ``` 136 | * @param {String} `filename` Name of the file to parse. 137 | * @param {Object} `options` 138 | * @return {Object} 139 | * @api public 140 | */ 141 | 142 | config.parse = function parseConfig(filename, options) { 143 | filename = filename || 'package.json'; 144 | var opts = extend({ parse: type(filename, options) }, options); 145 | var fp = config.resolve(filename, opts); 146 | try { 147 | return read.data.sync(fp, opts); 148 | } catch (err) {} 149 | return null; 150 | }; 151 | 152 | /** 153 | * Detect the type to parse, JSON or YAML. Sometimes this is based on file extension, 154 | * other times it must be specified on the options. 155 | * 156 | * @param {String} `filename` 157 | * @param {Object} `opts` If `filename` does not have an extension, specify the type on the `parse` option. 158 | * @return {String} The type to parse. 159 | */ 160 | 161 | function type(filename, opts) { 162 | opts = opts || {}; 163 | if (opts.parse && typeof opts.parse === 'string') { 164 | return opts.parse; 165 | } 166 | var ext = path.extname(filename); 167 | return ext || 'json'; 168 | } 169 | 170 | /** 171 | * Expose `config` 172 | */ 173 | 174 | module.exports = config; 175 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "config-file", 3 | "description": "Find and load a YAML or JSON config file (like .jshintrc, package.json, bower.json etc) from either a local project, installed npm module, or the user's home directory.", 4 | "version": "0.3.2", 5 | "homepage": "https://github.com/jonschlinkert/config-file", 6 | "author": "Jon Schlinkert (https://github.com/jonschlinkert)", 7 | "repository": "jonschlinkert/config-file", 8 | "bugs": { 9 | "url": "https://github.com/jonschlinkert/config-file/issues" 10 | }, 11 | "license": "MIT", 12 | "files": [ 13 | "index.js" 14 | ], 15 | "main": "index.js", 16 | "engines": { 17 | "node": ">= 0.8.0" 18 | }, 19 | "scripts": { 20 | "test": "mocha" 21 | }, 22 | "dependencies": { 23 | "extend-shallow": "^2.0.0", 24 | "global-modules": "^0.1.0", 25 | "look-up": "^0.7.1", 26 | "read-data": "^0.3.0" 27 | }, 28 | "keywords": [ 29 | "config", 30 | "configuration", 31 | "detect", 32 | "file", 33 | "find", 34 | "get", 35 | "glob", 36 | "json", 37 | "load", 38 | "look", 39 | "rc", 40 | "resolve", 41 | "runtime", 42 | "runtime-config", 43 | "up", 44 | "yaml" 45 | ], 46 | "verb": { 47 | "related": { 48 | "list": [ 49 | "global-modules", 50 | "look-up", 51 | "read-data", 52 | "read-yaml" 53 | ] 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /test/fixtures/.configrc: -------------------------------------------------------------------------------- 1 | { 2 | "name": "Config", 3 | "description": "A JSON config file" 4 | } -------------------------------------------------------------------------------- /test/fixtures/.configrc.yml: -------------------------------------------------------------------------------- 1 | name: Config 2 | description: A YAML config file -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var assert = require('assert'); 4 | var path = require('path'); 5 | var config = require('../'); 6 | 7 | describe('config():', function () { 8 | it('should parse and load package.json when no file is specified.', function () { 9 | var actual = config(); 10 | assert.equal(actual.name, 'config-file'); 11 | }); 12 | 13 | it('should parse and load the specified config file.', function () { 14 | var actual = config('test/fixtures/.configrc'); 15 | var expected = {name: 'Config', description: 'A JSON config file'}; 16 | assert.deepEqual(actual, expected); 17 | }); 18 | 19 | it('should parse the specified YAML config file.', function () { 20 | var actual = config('test/fixtures/.configrc.yml', {parse: 'yaml'}); 21 | var expected = {name: 'Config', description: 'A YAML config file'}; 22 | assert.deepEqual(actual, expected); 23 | }); 24 | 25 | it('should return null when no config file is found', function () { 26 | var actual = config('test/fixtures/.nothingrc.yml'); 27 | assert.deepEqual(actual === null, true); 28 | }); 29 | 30 | it('should load package.json from a specified cwd', function () { 31 | var cwd = path.resolve('node_modules', 'read-data'); 32 | var actual = config({cwd: cwd}); 33 | assert.deepEqual(actual.name, 'read-data'); 34 | }); 35 | 36 | it('should get a JSON config file when a cwd is specified.', function () { 37 | var actual = config('.configrc', {cwd: 'test/fixtures'}); 38 | var expected = {name: 'Config', description: 'A JSON config file'}; 39 | assert.deepEqual(actual, expected); 40 | }); 41 | 42 | it('should get a YAML config file when a cwd is specified.', function () { 43 | var actual = config('.configrc.yml', {parse: 'yaml', cwd: 'test/fixtures'}); 44 | var expected = {name: 'Config', description: 'A YAML config file'}; 45 | assert.deepEqual(actual, expected); 46 | }); 47 | 48 | it('should return null when no config file is found', function () { 49 | var actual = config('.nothingrc.yml', {cwd: 'fixtures'}); 50 | assert.equal(actual === null, true); 51 | }); 52 | }); 53 | 54 | describe('config.npm():', function () { 55 | it('should parse the specified config file.', function () { 56 | var actual = config.npm('read-data', 'package.json'); 57 | assert.equal(actual.name, 'read-data'); 58 | }); 59 | 60 | it('should find the config file from a cwd.', function () { 61 | var actual = config.npm('verb-default', 'package.json', { 62 | cwd: path.join(config.globalDir, 'verb-cli') 63 | }); 64 | assert.equal(actual.name, 'verb-default'); 65 | }); 66 | 67 | it('should find the config file from a base and cwd.', function () { 68 | var actual = config.npm('gulp-util', 'package.json', { 69 | cwd: path.join(config.globalDir, 'verb-cli'), 70 | base: 'verb-default/node_modules' 71 | }); 72 | assert.equal(actual.name, 'gulp-util'); 73 | }); 74 | 75 | it('should find the config file from a base.', function () { 76 | var actual = config.npm('is-glob', 'package.json', {base: 'look-up/node_modules'}); 77 | assert.equal(actual.name, 'is-glob'); 78 | }); 79 | 80 | it('should use package.json by default.', function () { 81 | var actual = config.npm('read-yaml', {base: 'read-data/node_modules'}); 82 | assert.equal(actual.name, 'read-yaml'); 83 | }); 84 | }); 85 | 86 | describe('config.global():', function () { 87 | it('should parse the specified config file.', function () { 88 | var actual = config.global('verb-cli', 'package.json'); 89 | assert.equal(actual.name, 'verb-cli'); 90 | }); 91 | 92 | it('should find the config file from a cwd.', function () { 93 | var actual = config.global('verb-default', 'package.json', {cwd: 'verb-cli/node_modules'}); 94 | assert.equal(actual.name, 'verb-default'); 95 | }); 96 | 97 | it('should use package.json by default.', function () { 98 | var actual = config.global('verb-default', {cwd: 'verb-cli/node_modules'}); 99 | assert.equal(actual.name, 'verb-default'); 100 | }); 101 | }); 102 | 103 | describe('config.resolve()', function () { 104 | it('should return the path for package.json when no file is specified.', function () { 105 | assert.deepEqual(config.resolve(), path.resolve('package.json')); 106 | }); 107 | 108 | it('should return the path for the specified config file.', function () { 109 | var actual = config.resolve('.configrc.yml', {cwd: 'test/fixtures'}); 110 | var expected = path.resolve('test/fixtures/.configrc.yml'); 111 | assert.equal(actual, expected); 112 | }); 113 | }); 114 | --------------------------------------------------------------------------------