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 include and exclude options to instrument command #1007

Merged
Merged
Show file tree
Hide file tree
Changes from 14 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
12 changes: 11 additions & 1 deletion index.js
Expand Up @@ -6,7 +6,9 @@ const arrify = require('arrify')
const cachingTransform = require('caching-transform')
const util = require('util')
const findCacheDir = require('find-cache-dir')
const { copySync } = require('fs-extra')
const fs = require('fs')
const glob = require('glob')
const Hash = require('./lib/hash')
const libCoverage = require('istanbul-lib-coverage')
const libHook = require('istanbul-lib-hook')
Expand Down Expand Up @@ -178,9 +180,11 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
const outCode = this._transform(inCode, inFile) || inCode

if (output) {
const mode = fs.statSync(inFile).mode
const outFile = path.resolve(output, relFile)
mkdirp.sync(path.dirname(outFile))
fs.writeFileSync(outFile, outCode, 'utf-8')
fs.writeFileSync(outFile, outCode)
fs.chmodSync(outFile, mode)
} else {
console.log(outCode)
}
Expand All @@ -193,6 +197,12 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
if (stats.isDirectory()) {
inputDir = input
this.walkAllFiles(input, visitor)

if (output) {
const globOptions = { dot: true, ignore: ['**/.git', `**/${path.basename(output)}`] }
Copy link
Member

Choose a reason for hiding this comment

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

I suspect this is a problem for nyc instrument . output if ./lib/output/data.json exists (that output folder should be copied). Instead of ignoring **/${path.basename(output)} couldn't we just ignore output?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This only produces a list of top level files using glob.sync(`${path.resolve(input)}/*`, ...), so the inclusion of **/${path.basename(output)} won't exclude any further sub-dirs. The recursive copy of each top level sub-dir is then handled by fs-extra.copySync. I agree it looks a little silly and should really be just output, but I couldn't get it to work with glob.sync. I should point out that this also means we're only ignoring .git directories at the top level in this implementation.

Anyway I'll see if I can come up with a nicer solution here, in the meantime I'll drop in the change below and rebase

Copy link
Member

Choose a reason for hiding this comment

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

What if someone does nyc instrument . subdir/output? In that case wouldn't we need to ignore subdir, not output? I ask due to the use of path.basename.

glob.sync(`${path.resolve(input)}/*`, globOptions)
.forEach(src => copySync(src, path.join(output, path.relative(input, src)), { overwrite: false }))
}
} else {
visitor(input)
}
Expand Down
45 changes: 35 additions & 10 deletions lib/commands/instrument.js
@@ -1,6 +1,7 @@
const NYC = require('../../index.js')
const path = require('path')
const rimraf = require('rimraf')
const testExclude = require('test-exclude')

exports.command = 'instrument <input> [output]'

Expand Down Expand Up @@ -48,6 +49,16 @@ exports.builder = function (yargs) {
type: 'boolean',
description: 'should nyc exit when an instrumentation failure occurs?'
})
.option('include', {
alias: 'n',
default: [],
describe: 'a list of specific files and directories that should be instrumented, glob patterns are supported'
})
.option('exclude', {
alias: 'x',
default: testExclude.defaultExclude,
coreyfarrell marked this conversation as resolved.
Show resolved Hide resolved
describe: 'a list of specific files and directories that should not be instrumented, glob patterns are supported'
})
.option('es-modules', {
default: true,
type: 'boolean',
Expand All @@ -62,6 +73,28 @@ exports.builder = function (yargs) {
}

exports.handler = function (argv) {
if (argv.output && (path.resolve(argv.cwd, argv.input) === path.resolve(argv.cwd, argv.output))) {
console.error(`nyc instrument failed: cannot instrument files in place, <input> must differ from <output>`)
process.exitCode = 1
return
}

if (path.relative(argv.cwd, path.resolve(argv.cwd, argv.input)).startsWith('..')) {
console.error(`nyc instrument failed: cannot instrument files outside of project root directory`)
process.exitCode = 1
return
}

if (argv.delete && argv.output && argv.output.length !== 0) {
const relPath = path.relative(process.cwd(), path.resolve(argv.output))
if (relPath !== '' && !relPath.startsWith('..')) {
rimraf.sync(argv.output)
} else {
console.error(`nyc instrument failed: attempt to delete '${process.cwd()}' or containing directory.`)
process.exit(1)
}
}

// If instrument is set to false enable a noop instrumenter.
argv.instrumenter = (argv.instrument)
? './lib/instrumenters/istanbul'
Expand All @@ -76,20 +109,12 @@ exports.handler = function (argv) {
cwd: argv.cwd,
compact: argv.compact,
preserveComments: argv.preserveComments,
include: argv.include,
exclude: argv.exclude,
esModules: argv.esModules,
exitOnError: argv.exitOnError
})

if (argv.delete && argv.output && argv.output.length !== 0) {
const relPath = path.relative(process.cwd(), path.resolve(argv.output))
if (relPath !== '' && !relPath.startsWith('..')) {
rimraf.sync(argv.output)
} else {
console.error(`nyc instrument failed: attempt to delete '${process.cwd()}' or containing directory.`)
process.exit(1)
}
}

nyc.instrumentAllFiles(argv.input, argv.output, err => {
if (err) {
console.error(err.message)
Expand Down
61 changes: 42 additions & 19 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Expand Up @@ -74,6 +74,8 @@
"find-cache-dir": "^2.0.0",
"find-up": "^3.0.0",
"foreground-child": "^1.5.6",
"fs-extra": "^7.0.1",
"glob": "^7.1.3",
"istanbul-lib-coverage": "^2.0.4",
"istanbul-lib-hook": "^2.0.4",
"istanbul-lib-instrument": "^3.1.1",
Expand All @@ -95,7 +97,6 @@
"any-path": "^1.3.0",
"chai": "^4.2.0",
"coveralls": "^3.0.3",
"glob": "^7.1.3",
"is-windows": "^1.0.2",
"lodash": "^4.17.11",
"newline-regex": "^0.2.1",
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/cli/.instrument-nycrc
@@ -0,0 +1,5 @@
{
"exclude": [
"**/exclude-me/**"
]
}
1 change: 1 addition & 0 deletions test/fixtures/cli/subdir/.gitignore
@@ -1 +1,2 @@
output-dir
!node_modules
2 changes: 2 additions & 0 deletions test/fixtures/cli/subdir/input-dir/exclude-me/index.js
@@ -0,0 +1,2 @@
'use strict';
console.log('Hello, World!')
2 changes: 2 additions & 0 deletions test/fixtures/cli/subdir/input-dir/node_modules/index.js

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