-
Notifications
You must be signed in to change notification settings - Fork 97
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
Adds support for #82's conclusion. #85
Open
legodude17
wants to merge
3
commits into
dominictarr:master
Choose a base branch
from
legodude17:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ var home = win | |
? process.env.USERPROFILE | ||
: process.env.HOME | ||
|
||
module.exports = function (name, defaults, argv, parse) { | ||
module.exports = function (name, defaults, argv, parsers) { | ||
if('string' !== typeof name) | ||
throw new Error('rc(name): name *must* be string') | ||
if(!argv) | ||
|
@@ -18,34 +18,39 @@ module.exports = function (name, defaults, argv, parse) { | |
? cc.json(defaults) : defaults | ||
) || {} | ||
|
||
parse = parse || cc.parse | ||
parsers = parsers || cc.parsers | ||
|
||
var env = cc.env(name + '_') | ||
|
||
var configs = [defaults] | ||
var configFiles = [] | ||
function addConfigFile (file) { | ||
if (configFiles.indexOf(file) >= 0) return | ||
var fileConfig = cc.file(file) | ||
if (fileConfig) { | ||
configs.push(parse(fileConfig)) | ||
configs.push(cc.parse(fileConfig, file, parsers)) | ||
configFiles.push(file) | ||
} | ||
} | ||
|
||
// which files do we look at? | ||
if (!win) | ||
[join(etc, name, 'config'), | ||
join(etc, name + 'rc')].forEach(addConfigFile) | ||
[join(etc, name, 'config')].concat(Object.keys(parsers).map(function (v) { | ||
return [etc, '.' + name + 'rc.' + v.toLowerCase()] | ||
})).forEach(addConfigFile) | ||
if (home) | ||
[join(home, '.config', name, 'config'), | ||
join(home, '.config', name), | ||
join(home, '.' + name, 'config'), | ||
join(home, '.' + name + 'rc')].forEach(addConfigFile) | ||
addConfigFile(cc.find('.'+name+'rc')) | ||
join(home, '.' + name, 'config')].concat(Object.keys(parsers).map(function (v) { | ||
return [home, '.' + name + 'rc.' + v.toLowerCase()] | ||
})).forEach(addConfigFile) | ||
Object.keys(parsers).map(function (v) { | ||
return v.toLowerCase(); | ||
}).forEach(function (v) { | ||
addConfigFile(cc.find('.' + name + 'rc' + (v ? '.' + v : ''))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does anyone have a better idea for how to do this searching? |
||
}) | ||
if (env.config) addConfigFile(env.config) | ||
if (argv.config) addConfigFile(argv.config) | ||
|
||
return deepExtend.apply(null, configs.concat([ | ||
env, | ||
argv, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,17 +4,46 @@ var ini = require('ini') | |
var path = require('path') | ||
var stripJsonComments = require('strip-json-comments') | ||
|
||
var parse = exports.parse = function (content) { | ||
|
||
//if it ends in .json or starts with { then it must be json. | ||
//must be done this way, because ini accepts everything. | ||
//can't just try and parse it and let it throw if it's not ini. | ||
//everything is ini. even json with a syntax error. | ||
var parsers = exports.parsers = { | ||
'': function (content) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is for backwards compatibility. |
||
try { | ||
return JSON.parse(stripJsonComments(content)); | ||
} catch (e) { | ||
return ini.parse(content) | ||
} | ||
}, | ||
'json': function (content) { | ||
try { | ||
return JSON.parse(stripJsonComments(content)); | ||
} catch (e) { | ||
return null; | ||
} | ||
}, | ||
'ini': function (content) { | ||
return ini.parse(content) | ||
} | ||
}; | ||
|
||
if(/^\s*{/.test(content)) | ||
return JSON.parse(stripJsonComments(content)) | ||
return ini.parse(content) | ||
function getExt(file) { | ||
var ext = file.split('.').pop() | ||
if (ext.slice(-2) === 'rc') { | ||
return ''; | ||
} | ||
return ext; | ||
} | ||
|
||
var parse = exports.parse = function (contents, file, ps) { | ||
var ext | ||
if (file) { | ||
ext = getExt(file) | ||
} else { | ||
ext = '' | ||
} | ||
ps = ps || parsers | ||
if (typeof ps[ext] !== 'function') { | ||
throw new Error('Extension ' + ext + ' does not have a parser. Valid parsers: ' + Object.keys(ps).join(', ')) | ||
} | ||
return ps[ext](contents) | ||
} | ||
|
||
var file = exports.file = function () { | ||
|
@@ -100,4 +129,3 @@ var find = exports.find = function () { | |
} | ||
return find(process.cwd(), rel) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
|
||
var n = 'rc'+Math.random() | ||
var assert = require('assert') | ||
var yaml = require('yaml') | ||
var fs = require('fs') | ||
var path = require('path') | ||
var jsonrc = path.resolve('.' + n + 'rc.json'); | ||
|
||
console.log('---extensions---\n\n') | ||
|
||
process.env[n + '_envOption'] = 42 | ||
|
||
fs.writeFileSync(jsonrc, [ | ||
'{', | ||
'// json overrides default', | ||
'"option": false,', | ||
'/* env overrides json */', | ||
'"envOption": 24', | ||
'}' | ||
].join('\n')); | ||
|
||
var commentedJSON = require('../')(n, { | ||
option: true | ||
}) | ||
|
||
fs.unlinkSync(jsonrc); | ||
|
||
console.log('Commented:', commentedJSON, commentedJSON.configs) | ||
|
||
assert.equal(commentedJSON.option, false) | ||
assert.equal(commentedJSON.envOption, 42) | ||
|
||
assert.equal(commentedJSON.config, jsonrc) | ||
assert.equal(commentedJSON.configs.length, 1) | ||
assert.equal(commentedJSON.configs[0], jsonrc) | ||
|
||
var inirc = path.resolve('.' + n + 'rc.ini'); | ||
|
||
fs.writeFileSync(inirc, [ | ||
'; ini overrides default', | ||
'option=false', | ||
'; env overrides ini', | ||
'envOption=24' | ||
].join('\n')); | ||
|
||
var ini = require('../')(n, { | ||
option: true | ||
}) | ||
|
||
fs.unlinkSync(inirc); | ||
|
||
console.log(ini) | ||
|
||
assert.equal(ini.option, false) | ||
assert.equal(ini.envOption, 42) | ||
|
||
assert.equal(ini.config, inirc) | ||
assert.equal(ini.configs.length, 1) | ||
assert.equal(ini.configs[0], inirc) | ||
|
||
var yamlrc = path.resolve('.' + n + 'rc.yaml'); | ||
|
||
fs.writeFileSync(yamlrc, [ | ||
'---', | ||
' envOption: 24', | ||
' option: false', | ||
'' | ||
].join('\n')); | ||
|
||
var yamlconfig = require('../')(n, { | ||
option: true | ||
}, false, { | ||
'yaml': function (contents) { | ||
return yaml.eval(contents) | ||
} | ||
}) | ||
|
||
fs.unlinkSync(yamlrc); | ||
|
||
console.log(yamlconfig) | ||
|
||
assert.equal(yamlconfig.option, false) | ||
assert.equal(yamlconfig.envOption, 42) | ||
|
||
assert.equal(yamlconfig.config, yamlrc) | ||
assert.equal(yamlconfig.configs.length, 1) | ||
assert.equal(yamlconfig.configs[0], yamlrc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I kind of think that the parse function should be created inside the main function. I did it this way so that
test/ini.js
still passed.