From 4685f2dd527990256c50f4ced00adeb196f840c0 Mon Sep 17 00:00:00 2001 From: Suneil Nyamathi Date: Wed, 12 Jul 2023 10:56:15 -0700 Subject: [PATCH] feat: support .cjs extension (#49) --- lib/index.js | 2 ++ package.json | 12 ++++++------ .../touchdown-simple/configs/commonjs.cjs | 7 +++++++ tests/lib/index.js | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 6 deletions(-) create mode 100644 tests/fixtures/touchdown-simple/configs/commonjs.cjs diff --git a/lib/index.js b/lib/index.js index 6cd4a69..2150054 100644 --- a/lib/index.js +++ b/lib/index.js @@ -604,6 +604,8 @@ Config.prototype = { contents = libjson5.parse(contents); } else if ('.mjs' === ext || '.js' === ext) { return loadModule(path, callback); + } else if ('.cjs' === ext) { + contents = require(path); } else { contents = libyaml.parse(contents); } diff --git a/package.json b/package.json index 820b96a..20b4750 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ycb-config", - "version": "2.3.0", + "version": "2.4.0", "description": "Configuration manager for Yahoo configuration bundles", "author": "Drew Folta ", "contributors": [], @@ -13,16 +13,16 @@ }, "homepage": "https://github.com/yahoo/ycb-config", "dependencies": { - "json5": "^2.2.1", + "json5": "^2.2.3", "yamljs": "^0.3.0", "ycb": "^2.2.0" }, "devDependencies": { - "chai": "^4.3.6", - "eslint": "^8.23.1", - "mocha": "^10.0.0", + "chai": "^4.3.7", + "eslint": "^8.44.0", + "mocha": "^10.2.0", "prettier": "^3.0.0", - "sinon": "^15.0.0" + "sinon": "^15.2.0" }, "scripts": { "lint": "eslint lib/*.js tests/lib/*.js && prettier --check .", diff --git a/tests/fixtures/touchdown-simple/configs/commonjs.cjs b/tests/fixtures/touchdown-simple/configs/commonjs.cjs new file mode 100644 index 0000000..7024b78 --- /dev/null +++ b/tests/fixtures/touchdown-simple/configs/commonjs.cjs @@ -0,0 +1,7 @@ +module.exports = [ + { + settings: ['main'], + syntax: 'cjs', + transpiled: false + } +]; diff --git a/tests/lib/index.js b/tests/lib/index.js index 22aca3c..f818101 100644 --- a/tests/lib/index.js +++ b/tests/lib/index.js @@ -153,6 +153,22 @@ describe('config', function () { }); }); }); + it('reads .cjs config files', function () { + var config = new Config({ + dimensionsPath: libpath.resolve(fixtures, 'touchdown-simple/configs/dimensions.json'), + }), + fullPath = libpath.resolve(fixtures, 'touchdown-simple/configs/commonjs.cjs'); + config.addConfigContents('foo', 'bar', fullPath, null, function (err) { + expect(err).to.equal(null); + config.read('foo', 'bar', {}, function (err, have) { + expect(err).to.equal(null); + expect(have).to.deep.equal({ + syntax: 'cjs', + transpiled: false, + }); + }); + }); + }); it('should work twice in a row', function () { var config, object = { color: 'red' };