Keine Beschreibung

config.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * grunt
  3. * http://gruntjs.com/
  4. *
  5. * Copyright (c) 2014 "Cowboy" Ben Alman
  6. * Licensed under the MIT license.
  7. * https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
  8. */
  9. 'use strict';
  10. var grunt = require('../grunt');
  11. // Get/set config data. If value was passed, set. Otherwise, get.
  12. var config = module.exports = function(prop, value) {
  13. if (arguments.length === 2) {
  14. // Two arguments were passed, set the property's value.
  15. return config.set(prop, value);
  16. } else {
  17. // Get the property's value (or the entire data object).
  18. return config.get(prop);
  19. }
  20. };
  21. // The actual config data.
  22. config.data = {};
  23. // Escape any . in name with \. so dot-based namespacing works properly.
  24. config.escape = function(str) {
  25. return str.replace(/\./g, '\\.');
  26. };
  27. // Return prop as a string.
  28. config.getPropString = function(prop) {
  29. return Array.isArray(prop) ? prop.map(config.escape).join('.') : prop;
  30. };
  31. // Get raw, unprocessed config data.
  32. config.getRaw = function(prop) {
  33. if (prop) {
  34. // Prop was passed, get that specific property's value.
  35. return grunt.util.namespace.get(config.data, config.getPropString(prop));
  36. } else {
  37. // No prop was passed, return the entire config.data object.
  38. return config.data;
  39. }
  40. };
  41. // Match '<%= FOO %>' where FOO is a propString, eg. foo or foo.bar but not
  42. // a method call like foo() or foo.bar().
  43. var propStringTmplRe = /^<%=\s*([a-z0-9_$]+(?:\.[a-z0-9_$]+)*)\s*%>$/i;
  44. // Get config data, recursively processing templates.
  45. config.get = function(prop) {
  46. return config.process(config.getRaw(prop));
  47. };
  48. // Expand a config value recursively. Used for post-processing raw values
  49. // already retrieved from the config.
  50. config.process = function(raw) {
  51. return grunt.util.recurse(raw, function(value) {
  52. // If the value is not a string, return it.
  53. if (typeof value !== 'string') { return value; }
  54. // If possible, access the specified property via config.get, in case it
  55. // doesn't refer to a string, but instead refers to an object or array.
  56. var matches = value.match(propStringTmplRe);
  57. var result;
  58. if (matches) {
  59. result = config.get(matches[1]);
  60. // If the result retrieved from the config data wasn't null or undefined,
  61. // return it.
  62. if (result != null) { return result; }
  63. }
  64. // Process the string as a template.
  65. return grunt.template.process(value, {data: config.data});
  66. });
  67. };
  68. // Set config data.
  69. config.set = function(prop, value) {
  70. return grunt.util.namespace.set(config.data, config.getPropString(prop), value);
  71. };
  72. // Deep merge config data.
  73. config.merge = function(obj) {
  74. grunt.util._.merge(config.data, obj);
  75. return config.data;
  76. };
  77. // Initialize config data.
  78. config.init = function(obj) {
  79. grunt.verbose.write('Initializing config...').ok();
  80. // Initialize and return data.
  81. return (config.data = obj || {});
  82. };
  83. // Test to see if required config params have been defined. If not, throw an
  84. // exception (use this inside of a task).
  85. config.requires = function() {
  86. var p = grunt.util.pluralize;
  87. var props = grunt.util.toArray(arguments).map(config.getPropString);
  88. var msg = 'Verifying propert' + p(props.length, 'y/ies') +
  89. ' ' + grunt.log.wordlist(props) + ' exist' + p(props.length, 's') +
  90. ' in config...';
  91. grunt.verbose.write(msg);
  92. var failProps = config.data && props.filter(function(prop) {
  93. return config.get(prop) == null;
  94. }).map(function(prop) {
  95. return '"' + prop + '"';
  96. });
  97. if (config.data && failProps.length === 0) {
  98. grunt.verbose.ok();
  99. return true;
  100. } else {
  101. grunt.verbose.or.write(msg);
  102. grunt.log.error().error('Unable to process task.');
  103. if (!config.data) {
  104. throw grunt.util.error('Unable to load config.');
  105. } else {
  106. throw grunt.util.error('Required config propert' +
  107. p(failProps.length, 'y/ies') + ' ' + failProps.join(', ') + ' missing.');
  108. }
  109. }
  110. };