Skip to content

Commit 8da097e

Browse files
AndrewFinlaycoreyfarrell
authored andcommitted
feat: add include and exclude options to instrument command (#1007)
BREAKING CHANGE: `nyc instrument` now honors `include` and `exclude` settings, potentially resulting in some files that were previously instrumented being ignored.
1 parent 31817de commit 8da097e

File tree

10 files changed

+254
-59
lines changed

10 files changed

+254
-59
lines changed

index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,11 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
179179
const outCode = this._transform(inCode, inFile) || inCode
180180

181181
if (output) {
182+
const mode = fs.statSync(inFile).mode
182183
const outFile = path.resolve(output, relFile)
183184
mkdirp.sync(path.dirname(outFile))
184-
fs.writeFileSync(outFile, outCode, 'utf-8')
185+
fs.writeFileSync(outFile, outCode)
186+
fs.chmodSync(outFile, mode)
185187
} else {
186188
console.log(outCode)
187189
}

lib/commands/instrument.js

+35-10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const NYC = require('../../index.js')
22
const path = require('path')
33
const rimraf = require('rimraf')
4+
const testExclude = require('test-exclude')
45

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

@@ -48,6 +49,16 @@ exports.builder = function (yargs) {
4849
type: 'boolean',
4950
description: 'should nyc exit when an instrumentation failure occurs?'
5051
})
52+
.option('include', {
53+
alias: 'n',
54+
default: [],
55+
describe: 'a list of specific files and directories that should be instrumented, glob patterns are supported'
56+
})
57+
.option('exclude', {
58+
alias: 'x',
59+
default: testExclude.defaultExclude,
60+
describe: 'a list of specific files and directories that should not be instrumented, glob patterns are supported'
61+
})
5162
.option('es-modules', {
5263
default: true,
5364
type: 'boolean',
@@ -62,6 +73,28 @@ exports.builder = function (yargs) {
6273
}
6374

6475
exports.handler = function (argv) {
76+
if (argv.output && (path.resolve(argv.cwd, argv.input) === path.resolve(argv.cwd, argv.output))) {
77+
console.error(`nyc instrument failed: cannot instrument files in place, <input> must differ from <output>`)
78+
process.exitCode = 1
79+
return
80+
}
81+
82+
if (path.relative(argv.cwd, path.resolve(argv.cwd, argv.input)).startsWith('..')) {
83+
console.error(`nyc instrument failed: cannot instrument files outside of project root directory`)
84+
process.exitCode = 1
85+
return
86+
}
87+
88+
if (argv.delete && argv.output && argv.output.length !== 0) {
89+
const relPath = path.relative(process.cwd(), path.resolve(argv.output))
90+
if (relPath !== '' && !relPath.startsWith('..')) {
91+
rimraf.sync(argv.output)
92+
} else {
93+
console.error(`nyc instrument failed: attempt to delete '${process.cwd()}' or containing directory.`)
94+
process.exit(1)
95+
}
96+
}
97+
6598
// If instrument is set to false enable a noop instrumenter.
6699
argv.instrumenter = (argv.instrument)
67100
? './lib/instrumenters/istanbul'
@@ -76,20 +109,12 @@ exports.handler = function (argv) {
76109
cwd: argv.cwd,
77110
compact: argv.compact,
78111
preserveComments: argv.preserveComments,
112+
include: argv.include,
113+
exclude: argv.exclude,
79114
esModules: argv.esModules,
80115
exitOnError: argv.exitOnError
81116
})
82117

83-
if (argv.delete && argv.output && argv.output.length !== 0) {
84-
const relPath = path.relative(process.cwd(), path.resolve(argv.output))
85-
if (relPath !== '' && !relPath.startsWith('..')) {
86-
rimraf.sync(argv.output)
87-
} else {
88-
console.error(`nyc instrument failed: attempt to delete '${process.cwd()}' or containing directory.`)
89-
process.exit(1)
90-
}
91-
}
92-
93118
nyc.instrumentAllFiles(argv.input, argv.output, err => {
94119
if (err) {
95120
console.error(err.message)

package-lock.json

+19-19
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/cli/.instrument-nycrc

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"exclude": [
3+
"**/exclude-me/**"
4+
]
5+
}

test/fixtures/cli/subdir/.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
output-dir
2+
!node_modules
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
console.log('Hello, World!')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
console.log('Hello, World!')
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
'use strict';
2+
console.log('Hello, World!')

test/fixtures/cli/subdir/input-dir/node_modules/index.js

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)