Jeff Williams 4badaef5fd better config loading (#1550)
+ Use `cosmiconfig` instead of rolling our own code (which gives us YAML support)
+ Look for the config in these locations, and in this order:
    + A `jsdoc` property in `package.json`
    + `.jsdocrc` (can be JSON or YAML; comments not allowed for JSON)
    + `.jsdocrc.json` (comments allowed)
    + `.jsdocrc.yaml`
    + `.jsdocrc.yml`
    + `.jsdocrc.js`
    + `jsdoc.config.js`
2019-01-25 19:33:18 -08:00

61 lines
1.3 KiB
JavaScript

/**
* @module @jsdoc/config
*/
const _ = require('lodash');
const cosmiconfig = require('cosmiconfig');
const defaults = require('./defaults');
const stripBom = require('strip-bom');
const stripJsonComments = require('strip-json-comments');
const MODULE_NAME = 'jsdoc';
class Config {
constructor(filepath, config) {
this.config = config;
this.filepath = filepath;
}
}
function loadJson(filepath, content) {
return cosmiconfig.loadJson(filepath, stripBom(stripJsonComments(content)));
}
function loadYaml(filepath, content) {
return cosmiconfig.loadYaml(filepath, stripBom(content));
}
const explorer = cosmiconfig(MODULE_NAME, {
cache: false,
loaders: {
'.json': loadJson,
'.yaml': loadYaml,
'.yml': loadYaml,
noExt: loadYaml
},
searchPlaces: [
'package.json',
`.${MODULE_NAME}rc`,
`.${MODULE_NAME}rc.json`,
`.${MODULE_NAME}rc.yaml`,
`.${MODULE_NAME}rc.yml`,
`.${MODULE_NAME}rc.js`,
`${MODULE_NAME}.config.js`
]
});
exports.loadSync = (filepath) => {
let loaded;
if (filepath) {
loaded = explorer.loadSync(filepath);
} else {
loaded = explorer.searchSync() || {};
}
return new Config(
loaded.filepath,
_.defaultsDeep({}, loaded.config, defaults)
);
};