Skip to content

Commit

Permalink
Add support for default configuration values. Closes Issue jsdoc#129.…
Browse files Browse the repository at this point in the history
… References Issue jsdoc#126.
  • Loading branch information
micmath committed Jun 21, 2012
1 parent 23d0383 commit c29e4e3
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 6 deletions.
7 changes: 3 additions & 4 deletions jsdoc.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,13 @@ function main() {
}
},
resolver,
fs = require('fs');
fs = require('fs'),
Config = require('jsdoc/config');

env.opts = jsdoc.opts.parser.parse(env.args);

try {
env.conf = JSON.parse(
fs.readFileSync( env.opts.configure || __dirname + '/conf.json' )
);
env.conf = new Config( fs.readFileSync( env.opts.configure || __dirname + '/conf.json' ) ).get();
}
catch (e) {
try {
Expand Down
20 changes: 18 additions & 2 deletions node_modules/common/util.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 43 additions & 0 deletions rhino_modules/jsdoc/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
@overview
@author Michael Mathews <[email protected]>
@license Apache License 2.0 - See file 'LICENSE.md' in this project.
*/

/**
@module jsdoc/config
@requires common/util
*/

// used to do recursive merge
var util = require('common/util');

// required config values, override these defaults in your config.json if necessary
const defaults = {
"source": {
"includePattern": ".+\\.js(doc)?$",
"excludePattern": "(^|\\/)_"
},
"jsVersion": 180
};

module.exports = Config;

/**
@class
@classdesc Represents a JSDoc application configuration.
@param {string} [json] - The contents of config.json.
*/
function Config(json) {
var json = JSON.parse( (json || "{}") );

this._config = util.mergeRecurse(defaults, json);
}

/**
Get the merged configuration values.
*/
Config.prototype.get = function() {
return this._config;
}

48 changes: 48 additions & 0 deletions test/specs/jsdoc/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
describe("jsdoc/config", function() {
var jsdoc = {config: require('jsdoc/config') };

it("should exist", function() {
expect(jsdoc.config).toBeDefined();
expect(typeof jsdoc.config).toEqual("function");
});

it("should provide a 'get' instance function", function() {
var config = new jsdoc.config();
expect(config.get).toBeDefined();
expect(typeof config.get).toEqual("function");
});

describe ("constructor with empty", function() {
it('should be possible to construct a Config with an empty arguments', function() {
var config = new jsdoc.config().get();

expect(config.plugins).toEqual([]);
});
});

describe ("constructor with {}", function() {
it('should be possible to construct a Config with JSON of an object literal that is emptys', function() {
var config = new jsdoc.config('{}').get();

expect(config.plugins).toEqual([]);
});
});

describe ("constructor with plugins value", function() {
it('should be possible to construct a Config with JSON of an object literal that has a plugin value', function() {
var config = new jsdoc.config('{"plugins":[42]}').get();

expect(config.plugins).toEqual([42]);
expect(config.jsVersion).toEqual(180);
});
});

describe ("constructor with source value", function() {
it('should be possible to construct a Config with JSON of an object literal that has a source value', function() {
var config = new jsdoc.config('{"source":{"includePattern":"hello"}}').get();

expect(config.source.includePattern).toEqual('hello');
expect(config.source.excludePattern).toEqual('(^|\\/)_');
});
});
});

0 comments on commit c29e4e3

Please sign in to comment.