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 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
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 @@ -191,8 +195,27 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
try {
const stats = fs.lstatSync(input)
if (stats.isDirectory()) {
const globOptions = { dot: true, mark: true, ignore: ['**/.git', '**/.git/**/*'] }
const outputPaths = (output) ? glob.sync(`${path.resolve(input)}/**/*`, globOptions) : []
coreyfarrell marked this conversation as resolved.
Show resolved Hide resolved

const partition = (universal, subsetFilter) => {
return universal.reduce(([a, aDash], member) => {
return subsetFilter(member) ? [[...a, member], aDash] : [a, [...aDash, member]]
}, [[], []])
}

const [dirs, files] = partition(outputPaths, filename => filename.endsWith('/'))

inputDir = input
this.walkAllFiles(input, visitor)

if (files.length || dirs.length) {
dirs.map(file => path.resolve(output, path.relative(input, file)))
.forEach(dir => mkdirp.sync(dir))

files.map(file => path.relative(input, file))
.forEach(file => { copySync(path.join(input, file), path.join(output, file), { overwrite: false }) })
}
} else {
visitor(input)
}
Expand Down
45 changes: 35 additions & 10 deletions lib/commands/instrument.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"exclude": [
"**/exclude-me/**"
]
}
1 change: 1 addition & 0 deletions test/fixtures/cli/subdir/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
output-dir
!node_modules
2 changes: 2 additions & 0 deletions test/fixtures/cli/subdir/input-dir/exclude-me/index.js
Original file line number Diff line number Diff line change
@@ -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.

Loading