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

make input relative to --cwd path #1484

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
18 changes: 8 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class NYC {
}

async instrumentAllFiles (input, output) {
let inputDir = '.' + path.sep
const inputDir = path.resolve(this.cwd, input)
const visitor = async relFile => {
const inFile = path.resolve(inputDir, relFile)
const inCode = await fs.readFile(inFile, 'utf-8')
Expand All @@ -232,21 +232,19 @@ class NYC {

this._loadAdditionalModules()

const stats = await fs.lstat(input)
const stats = await fs.lstat(inputDir)
if (stats.isDirectory()) {
inputDir = input

const filesToInstrument = await this.exclude.glob(input)
const filesToInstrument = await this.exclude.glob(inputDir)

const concurrency = output ? os.cpus().length : 1
if (this.config.completeCopy && output) {
const files = await glob(path.resolve(input, '**'), {
const files = await glob(path.resolve(inputDir, '**'), {
dot: true,
nodir: true,
ignore: ['**/.git', '**/.git/**', path.join(output, '**')]
})
const destDirs = new Set(
files.map(src => path.dirname(path.join(output, path.relative(input, src))))
files.map(src => path.dirname(path.join(output, path.relative(inputDir, src))))
)

await pMap(
Expand All @@ -256,14 +254,14 @@ class NYC {
)
await pMap(
files,
src => fs.copyFile(src, path.join(output, path.relative(input, src))),
src => fs.copyFile(src, path.join(output, path.relative(inputDir, src))),
{ concurrency }
)
}

await pMap(filesToInstrument, visitor, { concurrency })
} else {
await visitor(input)
await visitor(inputDir)
}
}

Expand Down Expand Up @@ -294,7 +292,7 @@ class NYC {

return (code, metadata, hash) => {
const filename = metadata.filename
const sourceMap = this._getSourceMap(code, filename, hash)
const sourceMap = this._getSourceMap(code, path.resolve(this.cwd, filename), hash)

try {
instrumented = instrumenter.instrumentSync(code, filename, sourceMap)
Expand Down
3 changes: 2 additions & 1 deletion lib/commands/instrument.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ exports.handler = cliWrapper(async argv => {
throw new Error('cannot instrument files in place, <input> must differ from <output>. Set \'--in-place\' to force')
}

argv.cwd = path.resolve(argv.cwd)
if (path.relative(argv.cwd, path.resolve(argv.cwd, argv.input)).startsWith('..')) {
throw new Error('cannot instrument files outside project root directory')
}
Expand All @@ -47,7 +48,7 @@ exports.handler = cliWrapper(async argv => {
: './lib/instrumenters/noop'

if (argv.inPlace) {
argv.output = argv.input
argv.output = path.join(argv.cwd, argv.input)
argv.completeCopy = false
}

Expand Down