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 cwd option to es6+ instrument command #1024

Merged
Merged
Show file tree
Hide file tree
Changes from 5 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: 6 additions & 5 deletions index.js
Expand Up @@ -193,15 +193,15 @@ NYC.prototype.addAllFiles = function () {
NYC.prototype.instrumentAllFiles = function (input, output, cb) {
let inputDir = '.' + path.sep
const visitor = filename => {
const inFile = path.resolve(inputDir, filename)
const inFile = path.resolve(this.cwd, inputDir, filename)
Copy link
Member

Choose a reason for hiding this comment

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

My gut reaction is that we these changes to index.js should not happen.

const inCode = fs.readFileSync(inFile, 'utf-8')

const extname = path.extname(filename).toLowerCase()
const transform = this.transforms[extname] || (code => code)
const outCode = transform(inCode, { filename: inFile })

if (output) {
const outFile = path.resolve(output, filename)
const outFile = path.resolve(this.cwd, output, filename)
mkdirp.sync(path.dirname(outFile))
fs.writeFileSync(outFile, outCode, 'utf-8')
} else {
Expand All @@ -212,10 +212,11 @@ NYC.prototype.instrumentAllFiles = function (input, output, cb) {
this._loadAdditionalModules()

try {
const stats = fs.lstatSync(input)
const inputPath = path.resolve(this.cwd, input)
const stats = fs.lstatSync(inputPath)
if (stats.isDirectory()) {
inputDir = input
this.walkAllFiles(input, visitor)
inputDir = inputPath
this.walkAllFiles(inputPath, visitor)
} else {
visitor(input)
}
Expand Down
6 changes: 6 additions & 0 deletions lib/commands/instrument.js
Expand Up @@ -7,6 +7,7 @@ exports.command = 'instrument <input> [output]'
exports.describe = 'instruments a file or a directory tree and writes the instrumented code to the desired output location'

exports.builder = function (yargs) {
const cwd = process.cwd()
return yargs
.option('require', {
alias: 'i',
Expand All @@ -28,6 +29,10 @@ exports.builder = function (yargs) {
type: 'boolean',
description: "should nyc's instrumenter produce source maps?"
})
.option('cwd', {
Copy link
Member

Choose a reason for hiding this comment

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

cwd is actually already defined in lib/config-util.js in a way that allows it to be global. I think we should not add the .option to this file but we should update the describe string in lib/config-util.js. Note the current definition of lib/config-util.js often does not result in cwd: process.cwd(). It uses find-up to search for the package.json which owns process.cwd(), then uses the dirname of that package.json.

Existing: working directory used when resolving paths
Maybe change to: base directory of this project

The important thing is that --cwd effects the interpretation of include and exclude. I think it should not effect the interpretation of the input/output positional arguments. Consider this, when you run nyc --cwd=.. ./somescript.js the ./somescript.js is found from $PWD, not from the value of --cwd.

describe: 'working directory used when resolving paths',
default: cwd
})
.option('compact', {
default: true,
type: 'boolean',
Expand Down Expand Up @@ -73,6 +78,7 @@ exports.handler = function (argv) {
produceSourceMap: argv.produceSourceMap,
extension: argv.extension,
require: argv.require,
cwd: argv.cwd,
coreyfarrell marked this conversation as resolved.
Show resolved Hide resolved
compact: argv.compact,
preserveComments: argv.preserveComments,
esModules: argv.esModules,
Expand Down
36 changes: 36 additions & 0 deletions test/nyc-integration.js
Expand Up @@ -606,6 +606,42 @@ describe('the nyc cli', function () {
})
})

it('works with a different working directory', function (done) {
const subdir = path.resolve(fixturesCLI, 'subdir')
const args = [bin, 'instrument', '--cwd', subdir, './input-dir', './output-dir']

const proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

proc.on('exit', code => {
code.should.equal(0)
const target = path.resolve(subdir, 'output-dir', 'index.js')
fs.readFileSync(target, 'utf8')
.should.match(/console.log\('Hello, World!'\)/)
done()
})
})

it('works with a deep folder structure working directory', function (done) {
const subdir = path.resolve(fixturesCLI, 'subdir')
const args = [bin, 'instrument', '--cwd', subdir, '.', './output-dir']

const proc = spawn(process.execPath, args, {
cwd: fixturesCLI,
env: env
})

proc.on('exit', code => {
code.should.equal(0)
const target = path.resolve(subdir, 'output-dir', 'input-dir', 'index.js')
fs.readFileSync(target, 'utf8')
.should.match(/console.log\('Hello, World!'\)/)
done()
})
})

it('can be configured to exit on error', function (done) {
var args = [
bin,
Expand Down