config.coffee 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. ### config.coffee ###
  2. fs = require 'fs'
  3. path = require 'path'
  4. async = require 'async'
  5. {readJSON, readJSONSync, fileExists, fileExistsSync} = require './utils'
  6. class Config
  7. ### The configuration object ###
  8. @defaults =
  9. # path to the directory containing content's to be scanned
  10. contents: './contents'
  11. # list of glob patterns to ignore
  12. ignore: []
  13. # context variables, passed to views/templates
  14. locals: {}
  15. # list of modules/files to load as plugins
  16. plugins: []
  17. # modules/files loaded and added to locals, name: module
  18. require: {}
  19. # path to the directory containing the templates
  20. templates: './templates'
  21. # directory to load custom views from
  22. views: null
  23. # built product goes here
  24. output: './build'
  25. # base url that site lives on, e.g. '/blog/'
  26. baseUrl: '/'
  27. # preview server settings
  28. hostname: null # INADDR_ANY
  29. port: 8080
  30. # options prefixed with _ are undocumented and should generally not be modified
  31. _fileLimit: 40 # max files to keep open at once
  32. _restartOnConfChange: true # restart preview server on config change
  33. constructor: (options={}) ->
  34. for option, value of options
  35. this[option] = value
  36. for option, defaultValue of @constructor.defaults
  37. this[option] ?= defaultValue
  38. Config.fromFile = (path, callback) ->
  39. ### Read config from *path* as JSON and *callback* with a Config instance. ###
  40. async.waterfall [
  41. (callback) ->
  42. fileExists path, (exists) ->
  43. if exists
  44. readJSON path, callback
  45. else
  46. callback new Error "Config file at '#{ path }' does not exist."
  47. (options, callback) ->
  48. config = new Config options
  49. config.__filename = path
  50. callback null, config
  51. ], callback
  52. Config.fromFileSync = (path) ->
  53. ### Read config from *path* as JSON return a Config instance. ###
  54. if not fileExistsSync path
  55. throw new Error "Config file at '#{ path }' does not exist."
  56. config = new Config readJSONSync path
  57. config.__filename = path
  58. return config
  59. ### Exports ###
  60. module.exports = {Config}