Skip to content

fix: Replaced hardcoded.nyc_output string to one from config #332

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

Merged
merged 5 commits into from
Nov 23, 2020
Merged
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
53 changes: 31 additions & 22 deletions task.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@ const debug = require('debug')('code-coverage')

// these are standard folder and file names used by NYC tools
const processWorkingDirectory = process.cwd()
const outputFolder = '.nyc_output'
const coverageFolder = join(processWorkingDirectory, outputFolder)
const nycFilename = join(coverageFolder, 'out.json')

// there might be custom "nyc" options in the user package.json
// see https://github.com/istanbuljs/nyc#configuring-nyc
Expand All @@ -33,10 +30,38 @@ const scripts = pkg.scripts || {}
const DEFAULT_CUSTOM_COVERAGE_SCRIPT_NAME = 'coverage:report'
const customNycReportScript = scripts[DEFAULT_CUSTOM_COVERAGE_SCRIPT_NAME]

const nycReportOptions = (function getNycOption() {
// https://github.com/istanbuljs/nyc#common-configuration-options
const nycReportOptions = readNycOptions(processWorkingDirectory)

if (nycReportOptions.exclude && !Array.isArray(nycReportOptions.exclude)) {
console.error('NYC options: %o', nycReportOptions)
throw new Error('Expected "exclude" to by an array')
}

if (nycReportOptions['temp-dir']) {
nycReportOptions['temp-dir'] = resolve(nycReportOptions['temp-dir'])
} else {
nycReportOptions['temp-dir'] = join(processWorkingDirectory, '.nyc_output')
}

nycReportOptions.tempDir = nycReportOptions['temp-dir']

if (nycReportOptions['report-dir']) {
nycReportOptions['report-dir'] = resolve(nycReportOptions['report-dir'])
}
// seems nyc API really is using camel cased version
nycReportOptions.reportDir = nycReportOptions['report-dir']

return nycReportOptions
})()

const nycFilename = join(nycReportOptions['temp-dir'], 'out.json')

function saveCoverage(coverage) {
if (!existsSync(coverageFolder)) {
mkdirSync(coverageFolder)
debug('created folder %s for output coverage', coverageFolder)
if (!existsSync(nycReportOptions.tempDir)) {
mkdirSync(nycReportOptions.tempDir)
debug('created folder %s for output coverage', nycReportOptions.tempDir)
}

writeFileSync(nycFilename, JSON.stringify(coverage, null, 2))
Expand Down Expand Up @@ -164,22 +189,6 @@ const tasks = {
})
}

// https://github.com/istanbuljs/nyc#common-configuration-options
const nycReportOptions = readNycOptions(processWorkingDirectory)

if (nycReportOptions.exclude && !Array.isArray(nycReportOptions.exclude)) {
console.error('NYC options: %o', nycReportOptions)
throw new Error('Expected "exclude" to by an array')
}

// override a couple of options
nycReportOptions.tempDir = coverageFolder
if (nycReportOptions['report-dir']) {
nycReportOptions['report-dir'] = resolve(nycReportOptions['report-dir'])
}
// seems nyc API really is using camel cased version
nycReportOptions.reportDir = nycReportOptions['report-dir']

if (nycReportOptions.all) {
debug('nyc needs to report on all included files')
includeAllFiles(nycFilename, nycReportOptions)
Expand Down