Skip to content

Commit

Permalink
feat: add support for yaml configuration file (#1054)
Browse files Browse the repository at this point in the history
  • Loading branch information
furudean authored and JaKXz committed Apr 16, 2019
1 parent c4fcf5e commit ca37ffa
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 11 deletions.
10 changes: 9 additions & 1 deletion README.md
Expand Up @@ -341,7 +341,15 @@ modules should be required in the subprocess collecting coverage:

## Configuring `nyc`

Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json, or within a `.nycrc`, `.nycrc.json`, or `nyc.config.js` file:
Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json, or within a seperate configuration file - a variety of flavors are available:

| File name | File Association |
|-----------------|------------------|
| `.nycrc` | JSON |
| `.nycrc.json` | JSON |
| `.nycrc.yaml` | YAML |
| `.nycrc.yml` | YAML |
| `nyc.config.js` | CommonJS export |

**package.json:**

Expand Down
9 changes: 8 additions & 1 deletion lib/config-util.js
Expand Up @@ -21,14 +21,21 @@ 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 rcExt = path.extname(rcPath.toLowerCase())
if (rcExt === '.js') {
config = require(rcPath)
} else if (rcExt === '.yml' || rcExt === '.yaml') {
config = require('js-yaml').load(
fs.readFileSync(rcPath, 'utf8')
)
} else {
config = JSON.parse(
fs.readFileSync(rcPath, 'utf-8')
Expand Down
14 changes: 5 additions & 9 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -80,6 +80,7 @@
"istanbul-lib-report": "^2.0.7",
"istanbul-lib-source-maps": "^3.0.5",
"istanbul-reports": "^2.2.2",
"js-yaml": "^3.13.1",
"make-dir": "^2.1.0",
"merge-source-map": "^1.1.0",
"resolve-from": "^4.0.0",
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/cli/nycrc/.nycrc.yaml
@@ -0,0 +1,2 @@
exclude:
- ignore.js
2 changes: 2 additions & 0 deletions test/fixtures/cli/nycrc/.nycrc.yml
@@ -0,0 +1,2 @@
exclude:
- ignore.js
42 changes: 42 additions & 0 deletions test/nyc-integration.js
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

0 comments on commit ca37ffa

Please sign in to comment.