Skip to content

Commit

Permalink
feat: support .cjs extension (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
snyamathi committed Jul 12, 2023
1 parent 1208038 commit 4685f2d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 6 deletions.
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>",
"contributors": [],
Expand All @@ -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 .",
Expand Down
7 changes: 7 additions & 0 deletions tests/fixtures/touchdown-simple/configs/commonjs.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = [
{
settings: ['main'],
syntax: 'cjs',
transpiled: false
}
];
16 changes: 16 additions & 0 deletions tests/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' };
Expand Down

0 comments on commit 4685f2d

Please sign in to comment.