From 10efbf2f32ebaaff417f207a7ffc9b88aef1d9f0 Mon Sep 17 00:00:00 2001 From: James Reggio Date: Thu, 28 May 2020 14:12:35 -0700 Subject: [PATCH 1/3] Add support for other NYC config files --- common-utils.js | 15 ++--------- cypress/integration/combine-spec.js | 24 ++++++++--------- package-lock.json | 10 +++---- package.json | 1 + task-utils.js | 42 ++++++++++++++++++++++++++--- 5 files changed, 57 insertions(+), 35 deletions(-) diff --git a/common-utils.js b/common-utils.js index a5711c64..a68f7d8b 100644 --- a/common-utils.js +++ b/common-utils.js @@ -1,18 +1,7 @@ // @ts-check -function combineNycOptions({ - pkgNycOptions, - nycrc, - nycrcJson, - defaultNycOptions -}) { +function combineNycOptions(...options) { // last option wins - const nycOptions = Object.assign( - {}, - defaultNycOptions, - nycrc, - nycrcJson, - pkgNycOptions - ) + const nycOptions = Object.assign({}, ...options) if (typeof nycOptions.reporter === 'string') { nycOptions.reporter = [nycOptions.reporter] diff --git a/cypress/integration/combine-spec.js b/cypress/integration/combine-spec.js index 5b13ac06..e6e4514f 100644 --- a/cypress/integration/combine-spec.js +++ b/cypress/integration/combine-spec.js @@ -5,10 +5,7 @@ describe('Combine NYC options', () => { extends: '@istanbuljs/nyc-config-typescript', all: true } - const combined = combineNycOptions({ - pkgNycOptions, - defaultNycOptions - }) + const combined = combineNycOptions(defaultNycOptions, pkgNycOptions) cy.wrap(combined).should('deep.equal', { extends: '@istanbuljs/nyc-config-typescript', all: true, @@ -23,10 +20,7 @@ describe('Combine NYC options', () => { const pkgNycOptions = { reporter: 'text' } - const combined = combineNycOptions({ - pkgNycOptions, - defaultNycOptions - }) + const combined = combineNycOptions(defaultNycOptions, pkgNycOptions) cy.wrap(combined).should('deep.equal', { 'report-dir': './coverage', reporter: ['text'], @@ -47,15 +41,19 @@ describe('Combine NYC options', () => { exclude: ['bar.js'], reporter: ['json'] } - const combined = combineNycOptions({ - pkgNycOptions, + const nycConfig = { + 'report-dir': './report' + } + const combined = combineNycOptions( + defaultNycOptions, nycrc, nycrcJson, - defaultNycOptions - }) + nycConfig, + pkgNycOptions + ) cy.wrap(combined).should('deep.equal', { all: true, - 'report-dir': './coverage', + 'report-dir': './report', reporter: ['json'], extension: ['.js'], excludeAfterRemap: false, diff --git a/package-lock.json b/package-lock.json index adfb71a3..b872f31d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4202,8 +4202,8 @@ }, "argparse": { "version": "1.0.10", - "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", - "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", + "resolved": "https://packages.convoy.com:443/artifactory/api/npm/npm/argparse/-/argparse-1.0.10.tgz", + "integrity": "sha1-vNZ5HqWuCXJeF+WtmIE0zUCz2RE=", "requires": { "sprintf-js": "~1.0.2" } @@ -9978,9 +9978,9 @@ "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" }, "js-yaml": { - "version": "3.13.1", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", - "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", + "version": "3.14.0", + "resolved": "https://packages.convoy.com:443/artifactory/api/npm/npm/js-yaml/-/js-yaml-3.14.0.tgz", + "integrity": "sha1-p6NBcPJqIbsWJCTYray0ETpp5II=", "requires": { "argparse": "^1.0.7", "esprima": "^4.0.0" diff --git a/package.json b/package.json index effed011..af564a06 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "execa": "4.0.2", "globby": "11.0.0", "istanbul-lib-coverage": "3.0.0", + "js-yaml": "3.14.0", "nyc": "15.0.1" }, "devDependencies": { diff --git a/task-utils.js b/task-utils.js index 2ec90681..fe0a73e3 100644 --- a/task-utils.js +++ b/task-utils.js @@ -8,6 +8,7 @@ const { isAbsolute, resolve, join } = require('path') const debug = require('debug')('code-coverage') const chalk = require('chalk') const globby = require('globby') +const yaml = require('js-yaml') const { combineNycOptions, defaultNycOptions } = require('./common-utils') function readNycOptions(workingDirectory) { @@ -27,12 +28,45 @@ function readNycOptions(workingDirectory) { ? JSON.parse(readFileSync(nycrcJsonFilename, 'utf8')) : {} - const nycOptions = combineNycOptions({ - pkgNycOptions, + const nycrcYamlFilename = join(workingDirectory, '.nycrc.yaml') + let nycrcYaml = {} + if (existsSync(nycrcYamlFilename)) { + try { + nycrcYaml = yaml.safeLoad(readFileSync(nycrcYamlFilename, 'utf8')) + } catch (error) { + throw new Error(`Failed to load .nycrc.yaml: ${error.message}`) + } + } + + const nycrcYmlFilename = join(workingDirectory, '.nycrc.yml') + let nycrcYml = {} + if (existsSync(nycrcYmlFilename)) { + try { + nycrcYml = yaml.safeLoad(readFileSync(nycrcYmlFilename, 'utf8')) + } catch (error) { + throw new Error(`Failed to load .nycrc.yml: ${error.message}`) + } + } + + const nycConfigFilename = join(workingDirectory, 'nyc.config.js') + let nycConfig = {} + if (existsSync(nycConfigFilename)) { + try { + nycConfig = require(nycConfigFilename) + } catch (error) { + throw new Error(`Failed to load nyc.config.js: ${error.message}`) + } + } + + const nycOptions = combineNycOptions( + defaultNycOptions, nycrc, nycrcJson, - defaultNycOptions - }) + nycrcYaml, + nycrcYml, + nycConfig, + pkgNycOptions + ) debug('combined NYC options %o', nycOptions) return nycOptions From 2c6d1459ef14117b48bdd5a79bb03500cb406246 Mon Sep 17 00:00:00 2001 From: James Reggio Date: Fri, 29 May 2020 10:00:30 -0700 Subject: [PATCH 2/3] Fix new test --- cypress/integration/combine-spec.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cypress/integration/combine-spec.js b/cypress/integration/combine-spec.js index 6253806a..8600b009 100644 --- a/cypress/integration/combine-spec.js +++ b/cypress/integration/combine-spec.js @@ -75,12 +75,12 @@ describe('Combine NYC options', () => { exclude: 'bar.js', reporter: ['json'] } - const combined = combineNycOptions({ - pkgNycOptions, + const combined = combineNycOptions( + defaultNycOptions nycrc, nycrcJson, - defaultNycOptions - }) + pkgNycOptions, + ) cy.wrap(combined).should('deep.equal', { all: true, 'report-dir': './coverage', From cb559ccc77a70eb2bf07c12b53569b72e5eb8591 Mon Sep 17 00:00:00 2001 From: James Reggio Date: Fri, 29 May 2020 10:02:48 -0700 Subject: [PATCH 3/3] Don't use the GH editor, kids --- cypress/integration/combine-spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cypress/integration/combine-spec.js b/cypress/integration/combine-spec.js index 8600b009..a0c25473 100644 --- a/cypress/integration/combine-spec.js +++ b/cypress/integration/combine-spec.js @@ -76,10 +76,10 @@ describe('Combine NYC options', () => { reporter: ['json'] } const combined = combineNycOptions( - defaultNycOptions + defaultNycOptions, nycrc, nycrcJson, - pkgNycOptions, + pkgNycOptions ) cy.wrap(combined).should('deep.equal', { all: true,