config.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* config.coffee */
  2. (function() {
  3. var Config, async, fileExists, fileExistsSync, fs, path, readJSON, readJSONSync, ref;
  4. fs = require('fs');
  5. path = require('path');
  6. async = require('async');
  7. ref = require('./utils'), readJSON = ref.readJSON, readJSONSync = ref.readJSONSync, fileExists = ref.fileExists, fileExistsSync = ref.fileExistsSync;
  8. Config = (function() {
  9. /* The configuration object */
  10. Config.defaults = {
  11. contents: './contents',
  12. ignore: [],
  13. locals: {},
  14. plugins: [],
  15. require: {},
  16. templates: './templates',
  17. views: null,
  18. output: './build',
  19. baseUrl: '/',
  20. hostname: null,
  21. port: 8080,
  22. _fileLimit: 40,
  23. _restartOnConfChange: true
  24. };
  25. function Config(options) {
  26. var defaultValue, option, ref1, value;
  27. if (options == null) {
  28. options = {};
  29. }
  30. for (option in options) {
  31. value = options[option];
  32. this[option] = value;
  33. }
  34. ref1 = this.constructor.defaults;
  35. for (option in ref1) {
  36. defaultValue = ref1[option];
  37. if (this[option] == null) {
  38. this[option] = defaultValue;
  39. }
  40. }
  41. }
  42. return Config;
  43. })();
  44. Config.fromFile = function(path, callback) {
  45. /* Read config from *path* as JSON and *callback* with a Config instance. */
  46. return async.waterfall([
  47. function(callback) {
  48. return fileExists(path, function(exists) {
  49. if (exists) {
  50. return readJSON(path, callback);
  51. } else {
  52. return callback(new Error("Config file at '" + path + "' does not exist."));
  53. }
  54. });
  55. }, function(options, callback) {
  56. var config;
  57. config = new Config(options);
  58. config.__filename = path;
  59. return callback(null, config);
  60. }
  61. ], callback);
  62. };
  63. Config.fromFileSync = function(path) {
  64. /* Read config from *path* as JSON return a Config instance. */
  65. var config;
  66. if (!fileExistsSync(path)) {
  67. throw new Error("Config file at '" + path + "' does not exist.");
  68. }
  69. config = new Config(readJSONSync(path));
  70. config.__filename = path;
  71. return config;
  72. };
  73. /* Exports */
  74. module.exports = {
  75. Config: Config
  76. };
  77. }).call(this);