config.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*******************************
  2. Set-up
  3. *******************************/
  4. var
  5. extend = require('extend'),
  6. fs = require('fs'),
  7. path = require('path'),
  8. defaults = require('../defaults')
  9. ;
  10. /*******************************
  11. Exports
  12. *******************************/
  13. module.exports = {
  14. getPath: function(file, directory) {
  15. var
  16. configPath,
  17. walk = function(directory) {
  18. var
  19. nextDirectory = path.resolve( path.join(directory, path.sep, '..') ),
  20. currentPath = path.normalize( path.join(directory, file) )
  21. ;
  22. if( fs.existsSync(currentPath) ) {
  23. // found file
  24. configPath = path.normalize(directory);
  25. return;
  26. }
  27. else {
  28. // reached file system root, let's stop
  29. if(nextDirectory == directory) {
  30. return;
  31. }
  32. // otherwise recurse
  33. walk(nextDirectory, file);
  34. }
  35. }
  36. ;
  37. // start walk from outside require-dot-files directory
  38. file = file || defaults.files.config;
  39. directory = directory || path.join(__dirname, path.sep, '..');
  40. walk(directory);
  41. return configPath || '';
  42. },
  43. // adds additional derived values to a config object
  44. addDerivedValues: function(config) {
  45. config = config || extend(false, {}, defaults);
  46. /*--------------
  47. File Paths
  48. ---------------*/
  49. var
  50. configPath = this.getPath(),
  51. sourcePaths = {},
  52. outputPaths = {},
  53. folder
  54. ;
  55. // resolve paths (config location + base + path)
  56. for(folder in config.paths.source) {
  57. if(config.paths.source.hasOwnProperty(folder)) {
  58. sourcePaths[folder] = path.resolve(path.join(configPath, config.base, config.paths.source[folder]));
  59. }
  60. }
  61. for(folder in config.paths.output) {
  62. if(config.paths.output.hasOwnProperty(folder)) {
  63. outputPaths[folder] = path.resolve(path.join(configPath, config.base, config.paths.output[folder]));
  64. }
  65. }
  66. // set config paths to full paths
  67. config.paths.source = sourcePaths;
  68. config.paths.output = outputPaths;
  69. // resolve "clean" command path
  70. config.paths.clean = path.resolve( path.join(configPath, config.base, config.paths.clean) );
  71. /*--------------
  72. CSS URLs
  73. ---------------*/
  74. // determine asset paths in css by finding relative path between themes and output
  75. // force forward slashes
  76. config.paths.assets = {
  77. source : '../../themes', // source asset path is always the same
  78. uncompressed : './' + path.relative(config.paths.output.uncompressed, config.paths.output.themes).replace(/\\/g, '/'),
  79. compressed : './' + path.relative(config.paths.output.compressed, config.paths.output.themes).replace(/\\/g, '/'),
  80. packaged : './' + path.relative(config.paths.output.packaged, config.paths.output.themes).replace(/\\/g, '/')
  81. };
  82. /*--------------
  83. Permission
  84. ---------------*/
  85. if(config.permission) {
  86. config.hasPermissions = true;
  87. config.parsedPermissions = typeof config.permission === 'string' ? parseInt(config.permission, 8) : config.permission;
  88. }
  89. else {
  90. // pass blank object to avoid causing errors
  91. config.permission = {};
  92. config.hasPermissions = false;
  93. config.parsedPermissions = {};
  94. }
  95. /*--------------
  96. Globs
  97. ---------------*/
  98. if(!config.globs) {
  99. config.globs = {};
  100. }
  101. // remove duplicates from component array
  102. if(config.components instanceof Array) {
  103. config.components = config.components.filter(function(component, index) {
  104. return config.components.indexOf(component) == index;
  105. });
  106. }
  107. // takes component object and creates file glob matching selected components
  108. config.globs.components = (typeof config.components == 'object')
  109. ? (config.components.length > 1)
  110. ? '{' + config.components.join(',') + '}'
  111. : config.components[0]
  112. : '{' + defaults.components.join(',') + '}'
  113. ;
  114. return config;
  115. }
  116. };