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 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
4 changes: 3 additions & 1 deletion index.js
Expand Up @@ -179,9 +179,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 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
38 changes: 19 additions & 19 deletions package-lock.json

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

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/include-me/exclude-me.js
@@ -0,0 +1,2 @@
'use strict';
console.log('Hello, World!')
2 changes: 2 additions & 0 deletions test/fixtures/cli/subdir/input-dir/include-me/include-me.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.