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

Use nyc instead of config.nyc in package.json #132

Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 5 additions & 6 deletions README.md
Expand Up @@ -82,27 +82,26 @@ nyc report --reporter=lcov
## Excluding Files

You can tell nyc to exclude specific files and directories by adding
an `config.nyc.exclude` array to your `package.json`. Each element of
an `nyc.exclude` array to your `package.json`. Each element of
the array is a glob pattern indicating which paths should be omitted.

Globs are matched using [micromatch](https://www.npmjs.com/package/micromatch)

In addition to patterns specified in the package, nyc will always exclude
files in `node_modules`.

For example, the following config will exclude all `node_modules`,
For example, the following config will exclude everything in `node_modules`,
any files with the extension `.spec.js`, and anything in the `build`
directory:

```json
{"config": {
"nyc": {
"exclude": [
{"nyc": {
"exclude": [
"**/*.spec.js",
"build"
]
}
}}
}
```

> Note: exclude defaults to `['test', 'test{,-*}.js']`, which would exclude
Expand Down
29 changes: 24 additions & 5 deletions index.js
Expand Up @@ -15,6 +15,8 @@ var SourceMapCache = require('./lib/source-map-cache')
var convertSourceMap = require('convert-source-map')
var md5hex = require('md5-hex')
var findCacheDir = require('find-cache-dir')
var pkgUp = require('pkg-up')
var readPkg = require('read-pkg')

/* istanbul ignore next */
if (/index\.covered\.js$/.test(__filename)) {
Expand All @@ -27,12 +29,11 @@ function NYC (opts) {
this._istanbul = opts.istanbul
this.subprocessBin = opts.subprocessBin || path.resolve(__dirname, './bin/nyc.js')
this._tempDirectory = opts.tempDirectory || './.nyc_output'
this.cwd = opts.cwd || process.env.NYC_CWD || process.cwd()
this.reporter = arrify(opts.reporter || 'text')

// you can specify config in the nyc stanza of package.json.
var config = require(path.resolve(this.cwd, './package.json')).config || {}
config = config.nyc || {}
var config = this._loadConfig(opts)
this.cwd = config.cwd

this.reporter = arrify(opts.reporter || 'text')

// load exclude stanza from config.
this.include = false
Expand All @@ -59,6 +60,24 @@ function NYC (opts) {
this.loadedMaps = null
}

NYC.prototype._loadConfig = function (opts) {
var cwd = opts.cwd || process.env.NYC_CWD || process.cwd()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rock on \o/

var pkgPath = pkgUp.sync(cwd)

// you can specify config in the nyc stanza of package.json.
var config
if (pkgPath) {
cwd = path.dirname(pkgPath)
var pkg = readPkg.sync(pkgPath, {normalize: false})
config = pkg.nyc || (pkg.config && pkg.config.nyc)
}
config = config || {}

config.cwd = cwd

return config
}

NYC.prototype._createTransform = function () {
var _this = this
return cachingTransform({
Expand Down
28 changes: 14 additions & 14 deletions package.json
Expand Up @@ -23,20 +23,18 @@
"lib/*.js",
"!**/*covered.js"
],
"config": {
"nyc": {
"exclude": [
"node_modules",
"bin",
"coverage",
"test/fixtures/coverage.js",
"test/build/*",
"test/nyc-test.js",
"test/source-map-cache.js",
"index.covered.js",
"test/fixtures/_generateCoverage.js"
]
}
"nyc": {
"exclude": [
"node_modules",
"bin",
"coverage",
"test/fixtures/coverage.js",
"test/build/*",
"test/nyc-test.js",
"test/source-map-cache.js",
"index.covered.js",
"test/fixtures/_generateCoverage.js"
]
},
"standard": {
"ignore": [
Expand Down Expand Up @@ -74,6 +72,8 @@
"md5-hex": "^1.2.0",
"micromatch": "~2.1.6",
"mkdirp": "^0.5.0",
"pkg-up": "^1.0.0",
"read-pkg": "^1.1.0",
"resolve-from": "^2.0.0",
"rimraf": "^2.5.0",
"signal-exit": "^2.1.1",
Expand Down
15 changes: 14 additions & 1 deletion test/src/nyc-test.js
Expand Up @@ -59,16 +59,29 @@ describe('nyc', function () {

nyc.cwd.should.equal(path.resolve(__dirname, '../fixtures'))
})

it('will look upwards for package.json from cwd', function () {
var nyc = new NYC({cwd: __dirname})
nyc.cwd.should.eql(path.join(__dirname, '../..'))
})
})

describe('config', function () {
it("loads 'exclude' patterns from package.json", function () {
it("loads 'exclude' patterns from package.json#config.nyc", function () {
var nyc = new NYC({
cwd: path.resolve(__dirname, '../fixtures')
})

nyc.exclude.length.should.eql(5)
})

it("loads 'exclude' patterns from package.json#nyc", function () {
var nyc = new NYC({
cwd: path.resolve(__dirname, '../..')
})

nyc.exclude.length.should.eql(19)
})
})

describe('_prepGlobPatterns', function () {
Expand Down