Config.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var lang = require('mout/lang');
  2. var object = require('mout/object');
  3. var rc = require('./util/rc');
  4. var defaults = require('./util/defaults');
  5. var expand = require('./util/expand');
  6. var path = require('path');
  7. function Config(cwd) {
  8. this._cwd = cwd || process.cwd();
  9. this._config = {};
  10. }
  11. Config.prototype.load = function () {
  12. this._config = rc('bower', defaults, this._cwd);
  13. return this;
  14. };
  15. Config.prototype.get = function (key) {
  16. // TODO
  17. };
  18. Config.prototype.set = function (key, value) {
  19. // TODO
  20. return this;
  21. };
  22. Config.prototype.del = function (key, value) {
  23. // TODO
  24. return this;
  25. };
  26. Config.prototype.save = function (where, callback) {
  27. // TODO
  28. };
  29. Config.prototype.toObject = function () {
  30. var config = lang.deepClone(this._config);
  31. config = Config.normalise(config);
  32. return config;
  33. };
  34. Config.create = function (cwd) {
  35. return new Config(cwd);
  36. };
  37. Config.read = function (cwd) {
  38. var config = new Config(cwd);
  39. return config.load().toObject();
  40. };
  41. Config.normalise = function (rawConfig) {
  42. var config = {};
  43. // Mix in defaults and raw config
  44. object.deepMixIn(config, expand(defaults), expand(rawConfig));
  45. // Some backwards compatible things..
  46. config.shorthandResolver = config.shorthandResolver
  47. .replace(/\{\{\{/g, '{{')
  48. .replace(/\}\}\}/g, '}}');
  49. // Ensure that every registry endpoint does not end with /
  50. config.registry.search = config.registry.search.map(function (url) {
  51. return url.replace(/\/+$/, '');
  52. });
  53. config.registry.register = config.registry.register.replace(/\/+$/, '');
  54. config.registry.publish = config.registry.publish.replace(/\/+$/, '');
  55. config.tmp = path.resolve(config.tmp);
  56. return config;
  57. };
  58. Config.DEFAULT_REGISTRY = defaults.registry;
  59. module.exports = Config;