From ca37ffa74c146b77c5fa9dc3411901e96e8c3a17 Mon Sep 17 00:00:00 2001 From: Cassidy Bandy Date: Tue, 16 Apr 2019 22:19:32 +0200 Subject: [PATCH] feat: add support for yaml configuration file (#1054) --- README.md | 10 ++++++- lib/config-util.js | 9 ++++++- package-lock.json | 14 ++++------ package.json | 1 + test/fixtures/cli/nycrc/.nycrc.yaml | 2 ++ test/fixtures/cli/nycrc/.nycrc.yml | 2 ++ test/nyc-integration.js | 42 +++++++++++++++++++++++++++++ 7 files changed, 69 insertions(+), 11 deletions(-) create mode 100644 test/fixtures/cli/nycrc/.nycrc.yaml create mode 100644 test/fixtures/cli/nycrc/.nycrc.yml diff --git a/README.md b/README.md index ee2b8810e..d003242b3 100644 --- a/README.md +++ b/README.md @@ -341,7 +341,15 @@ modules should be required in the subprocess collecting coverage: ## Configuring `nyc` -Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json, or within a `.nycrc`, `.nycrc.json`, or `nyc.config.js` file: +Any configuration options that can be set via the command line can also be specified in the `nyc` stanza of your package.json, or within a seperate configuration file - a variety of flavors are available: + +| File name | File Association | +|-----------------|------------------| +| `.nycrc` | JSON | +| `.nycrc.json` | JSON | +| `.nycrc.yaml` | YAML | +| `.nycrc.yml` | YAML | +| `nyc.config.js` | CommonJS export | **package.json:** diff --git a/lib/config-util.js b/lib/config-util.js index 5ca4043a5..55c47aa77 100644 --- a/lib/config-util.js +++ b/lib/config-util.js @@ -21,14 +21,21 @@ Config.loadConfig = function (argv, cwd) { const rcOptions = [ argv.nycrcPath || '.nycrc', '.nycrc.json', + '.nycrc.yml', + '.nycrc.yaml', 'nyc.config.js' ] const rcPath = findUp.sync(rcOptions, { cwd: guessCWD(cwd) }) let config = {} if (rcPath) { - if (rcPath.toLowerCase().endsWith('.js')) { + const rcExt = path.extname(rcPath.toLowerCase()) + if (rcExt === '.js') { config = require(rcPath) + } else if (rcExt === '.yml' || rcExt === '.yaml') { + config = require('js-yaml').load( + fs.readFileSync(rcPath, 'utf8') + ) } else { config = JSON.parse( fs.readFileSync(rcPath, 'utf-8') diff --git a/package-lock.json b/package-lock.json index d043317c2..0fbf2cc3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -189,7 +189,6 @@ "version": "1.0.10", "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz", "integrity": "sha512-o5Roy6tNG4SL/FOkCAN6RzjiakZS25RLYFrcMttJqbdd8BWrnA+fGz57iN5Pb06pvBGvl5gQ0B48dJlslXvoTg==", - "dev": true, "requires": { "sprintf-js": "~1.0.2" } @@ -1546,8 +1545,7 @@ "esprima": { "version": "4.0.1", "resolved": "https://registry.npmjs.org/esprima/-/esprima-4.0.1.tgz", - "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==", - "dev": true + "integrity": "sha512-eGuFFw7Upda+g4p+QHvnW0RyTX/SVeJBDM/gCtMARO0cLuT2HcEKnTPvhjV6aGeqrCB/sbNop0Kszm0jsaWU4A==" }, "esquery": { "version": "1.0.1", @@ -2468,10 +2466,9 @@ "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" }, "js-yaml": { - "version": "3.13.0", - "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.0.tgz", - "integrity": "sha512-pZZoSxcCYco+DIKBTimr67J6Hy+EYGZDY/HCWC+iAEA9h1ByhMXAIVUXMcMFpOCxQ/xjXmPI2MkDL5HRm5eFrQ==", - "dev": true, + "version": "3.13.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.13.1.tgz", + "integrity": "sha512-YfbcO7jXDdyj0DGxYVSlSeQNHbD7XPWvrVWeVUujrQEoZzWJIRrCPoyk6kL6IAjAG2IolMK4T0hNUe0HOUs5Jw==", "requires": { "argparse": "^1.0.7", "esprima": "^4.0.0" @@ -4716,8 +4713,7 @@ "sprintf-js": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz", - "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=", - "dev": true + "integrity": "sha1-BOaSb2YolTVPPdAVIDYzuFcpfiw=" }, "sshpk": { "version": "1.16.1", diff --git a/package.json b/package.json index 7503983b9..372e2c28d 100644 --- a/package.json +++ b/package.json @@ -80,6 +80,7 @@ "istanbul-lib-report": "^2.0.7", "istanbul-lib-source-maps": "^3.0.5", "istanbul-reports": "^2.2.2", + "js-yaml": "^3.13.1", "make-dir": "^2.1.0", "merge-source-map": "^1.1.0", "resolve-from": "^4.0.0", diff --git a/test/fixtures/cli/nycrc/.nycrc.yaml b/test/fixtures/cli/nycrc/.nycrc.yaml new file mode 100644 index 000000000..ad4f1b6cf --- /dev/null +++ b/test/fixtures/cli/nycrc/.nycrc.yaml @@ -0,0 +1,2 @@ +exclude: +- ignore.js diff --git a/test/fixtures/cli/nycrc/.nycrc.yml b/test/fixtures/cli/nycrc/.nycrc.yml new file mode 100644 index 000000000..ad4f1b6cf --- /dev/null +++ b/test/fixtures/cli/nycrc/.nycrc.yml @@ -0,0 +1,2 @@ +exclude: +- ignore.js diff --git a/test/nyc-integration.js b/test/nyc-integration.js index 8b6d45d01..91bd358ca 100644 --- a/test/nyc-integration.js +++ b/test/nyc-integration.js @@ -488,6 +488,48 @@ describe('the nyc cli', function () { }) }) + it('loads configuration from .nycrc.yml', function (done) { + var args = [bin, '--nycrc-path', './.nycrc.yml', process.execPath, './index.js'] + + var proc = spawn(process.execPath, args, { + cwd: cwd, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (code) { + code.should.equal(0) + stdout.should.match(/SF:.*index\.js/) + stdout.should.not.match(/SF:.*ignore\.js/) + done() + }) + }) + + it('loads configuration from .nycrc.yaml', function (done) { + var args = [bin, '--nycrc-path', './.nycrc.yaml', process.execPath, './index.js'] + + var proc = spawn(process.execPath, args, { + cwd: cwd, + env: env + }) + + var stdout = '' + proc.stdout.on('data', function (chunk) { + stdout += chunk + }) + + proc.on('close', function (code) { + code.should.equal(0) + stdout.should.match(/SF:.*index\.js/) + stdout.should.not.match(/SF:.*ignore\.js/) + done() + }) + }) + it('allows .nycrc configuration to be overridden with command line args', function (done) { var args = [bin, '--exclude=foo.js', process.execPath, './index.js']