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

feat: Add support for yaml configuration file #1054

Merged
merged 10 commits into from Apr 16, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion lib/config-util.js
Expand Up @@ -18,18 +18,35 @@ function guessCWD (cwd) {
return cwd
}

function moduleExists (name) {
try {
return typeof require.resolve(name) !== 'undefined'
} catch (e) { return false }
furudean marked this conversation as resolved.
Show resolved Hide resolved
}

Config.loadConfig = function (argv, cwd) {
const rcOptions = [
argv.nycrcPath || '.nycrc',
'.nycrc.json',
'.nycrc.yml',
'.nycrc.yaml',
'nyc.config.js'
]
const rcPath = findUp.sync(rcOptions, { cwd: guessCWD(cwd) })
let config = {}

if (rcPath) {
if (rcPath.toLowerCase().endsWith('.js')) {
const rcPathLower = rcPath.toLowerCase()
if (rcPathLower.endsWith('.js')) {
config = require(rcPath)
} else if (/.+\.y(a?)ml$/.test(rcPathLower)) {
if (moduleExists('js-yaml')) {
furudean marked this conversation as resolved.
Show resolved Hide resolved
console.error('you need to install js-yaml to use a yaml configuration file')
process.exit(1)
}
config = require('js-yaml').load(
fs.readFileSync(rcPath, 'utf8')
)
} else {
config = JSON.parse(
fs.readFileSync(rcPath, 'utf-8')
Expand Down