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
@@ -18,18 +18,35 @@ function guessCWD (cwd) {
return cwd
}

function moduleExists (name) {
try {
return typeof require.resolve(name) !== 'undefined'
} catch (e) { return false }
This conversation was marked as resolved by c-bandy

This comment has been minimized.

Copy link
@coreyfarrell

coreyfarrell Apr 5, 2019

Member

I missed this before, please add line-feeds around the return false and tell nyc to ignore the unreachable code:

try {
  return typeof require.resolve(name) !== 'undefined';
} catch (e) {
  /* istanbul ignore next */
  return false;
}
}

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')) {
This conversation was marked as resolved by c-bandy

This comment has been minimized.

Copy link
@coreyfarrell

coreyfarrell Apr 4, 2019

Member

Before this please add /* istanbul ignore if */ since we won't be able to test the case where js-yaml is not installed (it's installed by other dev dependencies).

This comment has been minimized.

Copy link
@isaacs

isaacs Apr 5, 2019

Collaborator

It might be worth check for yaml as well? It's pretty nice. The only difference is that you'd use require('yaml').parse() instead of require('js-yaml').load().

This comment has been minimized.

Copy link
@JaKXz
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')
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.