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
Show file tree
Hide file tree
Changes from 4 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
29 changes: 28 additions & 1 deletion lib/config-util.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,45 @@ function guessCWD (cwd) {
return cwd
}

function moduleExists (name) {
try {
return typeof require.resolve(name) !== 'undefined'
} catch (e) { 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)) {
const installedYamlParser = ['js-yaml', 'yaml'].find((name) => moduleExists(name))
switch (installedYamlParser) {
case 'js-yaml':
config = require('js-yaml').load(
fs.readFileSync(rcPath, 'utf8')
)
break
case 'yaml':
config = require('yaml').parse(
fs.readFileSync(rcPath, 'utf8')
)
break
/* istanbul ignore next */
default:
console.error('you need to install a yaml parser to use a yaml configuration file')
process.exit(1)
}
} else {
config = JSON.parse(
fs.readFileSync(rcPath, 'utf-8')
Expand Down
30 changes: 27 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"coveralls": "^3.0.3",
"glob": "^7.1.3",
"is-windows": "^1.0.2",
"js-yaml": "^3.13.0",
"lodash": "^4.17.11",
"newline-regex": "^0.2.1",
"requirejs": "^2.3.6",
Expand All @@ -107,6 +108,7 @@
"strip-indent": "^2.0.0",
"tap": "^12.6.1",
"which": "^1.3.1",
"yaml": "^1.4.0",
"zero-fill": "^2.2.3"
},
"engines": {
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/cli/nycrc/.nycrc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exclude:
- ignore.js
2 changes: 2 additions & 0 deletions test/fixtures/cli/nycrc/.nycrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
exclude:
- ignore.js
42 changes: 42 additions & 0 deletions test/nyc-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,48 @@ describe('the nyc cli', function () {
})
})

it('loads configuration from .nycrc.yml', function (done) {
var args = [bin, '--nycrc-path', './.nycrc.yml', process.execPath, './index.js']

var proc = spawn(process.execPath, args, {
cwd: cwd,
env: env
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdout.should.match(/SF:.*index\.js/)
stdout.should.not.match(/SF:.*ignore\.js/)
done()
})
})

it('loads configuration from .nycrc.yaml', function (done) {
var args = [bin, '--nycrc-path', './.nycrc.yaml', process.execPath, './index.js']

var proc = spawn(process.execPath, args, {
cwd: cwd,
env: env
})

var stdout = ''
proc.stdout.on('data', function (chunk) {
stdout += chunk
})

proc.on('close', function (code) {
code.should.equal(0)
stdout.should.match(/SF:.*index\.js/)
stdout.should.not.match(/SF:.*ignore\.js/)
done()
})
})

it('allows .nycrc configuration to be overridden with command line args', function (done) {
var args = [bin, '--exclude=foo.js', process.execPath, './index.js']

Expand Down