Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for explicitly passed config file #78

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ module.exports = function (name, defaults, argv, parse) {

var configs = [defaults]
var configFiles = []
function addConfigFile (file) {
function addConfigFile (file, throwError) {
if (configFiles.indexOf(file) >= 0) return
var fileConfig = cc.file(file)
var fileConfig = cc.file(file, throwError)
if (fileConfig) {
configs.push(parse(fileConfig))
configFiles.push(file)
Expand All @@ -43,8 +43,8 @@ module.exports = function (name, defaults, argv, parse) {
join(home, '.' + name, 'config'),
join(home, '.' + name + 'rc')].forEach(addConfigFile)
addConfigFile(cc.find('.'+name+'rc'))
if (env.config) addConfigFile(env.config)
if (argv.config) addConfigFile(argv.config)
if (env.config) addConfigFile(env.config, true)
if (argv.config) addConfigFile(argv.config, true)

return deepExtend.apply(null, configs.concat([
env,
Expand Down
30 changes: 13 additions & 17 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,15 @@ var parse = exports.parse = function (content) {

}

var file = exports.file = function () {
var args = [].slice.call(arguments).filter(function (arg) { return arg != null })

//path.join breaks if it's a not a string, so just skip this.
for(var i in args)
if('string' !== typeof args[i])
return

var file = path.join.apply(null, args)
var content
// reads a file and returns it's content
var file = exports.file = function (file, throwError) {
try {
return fs.readFileSync(file,'utf-8')
return fs.readFileSync(file, 'utf-8')
} catch (err) {
// Array.forEach() passes in the index, therefore check if second value is boolean
if (throwError === true) {
throw new Error('Explicitly passed config file could not be found at: ' + file);
}
return
}
}
Expand Down Expand Up @@ -85,19 +81,19 @@ var env = exports.env = function (prefix, env) {
return obj
}

var find = exports.find = function () {
var rel = path.join.apply(null, [].slice.call(arguments))
// returns first occurrence of the filename or undefined if none exists
var find = exports.find = function (fileName) {

function find(start, rel) {
var file = path.join(start, rel)
function find(start, fileName) {
var file = path.join(start, fileName)
try {
fs.statSync(file)
return file
} catch (err) {
if(path.dirname(start) !== start) // root
return find(path.dirname(start), rel)
return find(path.dirname(start), fileName)
}
}
return find(process.cwd(), rel)
return find(process.cwd(), fileName)
}

9 changes: 9 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ assert.equal(commentedJSON.envOption, 42)
assert.equal(commentedJSON.config, jsonrc)
assert.equal(commentedJSON.configs.length, 1)
assert.equal(commentedJSON.configs[0], jsonrc)

// check if error is thrown for missing file passed through --config
process.argv.push('--config', '.filedoesnotexistrc')

assert.throws(
function() {
require('../')(n);
}
);