Skip to content
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