1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var grunt = require('../grunt');
|
11 |
|
12 |
|
13 | var data;
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | var config = module.exports = function(prop, value) {
|
19 | if (arguments.length === 2) {
|
20 |
|
21 | return config.set(prop, value);
|
22 | } else {
|
23 |
|
24 | return config.get(prop);
|
25 | }
|
26 | };
|
27 |
|
28 |
|
29 | function getPropString(prop) {
|
30 | if (grunt.utils.kindOf(prop) === 'array') {
|
31 | return prop.map(config.escape).join('.');
|
32 | }
|
33 | return prop;
|
34 | }
|
35 |
|
36 |
|
37 | function processDirectives() {
|
38 |
|
39 | var toProcess = ['config', 'json'];
|
40 | data = grunt.utils.recurse(data, function(value) {
|
41 | if (typeof value !== 'string') { return value; }
|
42 | var parts = grunt.task.getDirectiveParts(value) || [];
|
43 | return toProcess.indexOf(parts[0]) !== -1 ? grunt.task.directive(value) : value;
|
44 | });
|
45 | }
|
46 |
|
47 |
|
48 | config.get = function(prop) {
|
49 |
|
50 | if (!data) { return null; }
|
51 |
|
52 | prop = getPropString(prop);
|
53 |
|
54 | if (prop) {
|
55 |
|
56 | return grunt.utils.namespace.get(data, prop);
|
57 | } else {
|
58 |
|
59 | return grunt.utils._.clone(data);
|
60 | }
|
61 | };
|
62 |
|
63 |
|
64 | config.set = function(prop, value) {
|
65 |
|
66 | if (!data) { return null; }
|
67 |
|
68 | prop = getPropString(prop);
|
69 |
|
70 | var result = grunt.utils.namespace.set(data, prop, value);
|
71 |
|
72 | processDirectives();
|
73 |
|
74 | return result;
|
75 | };
|
76 |
|
77 |
|
78 | config.process = function(prop) {
|
79 | return grunt.utils.recurse(config.get(prop), function(value) {
|
80 | if (typeof value !== 'string') { return value; }
|
81 | return grunt.template.process(value, data);
|
82 | });
|
83 | };
|
84 |
|
85 |
|
86 | config.init = function(obj) {
|
87 | grunt.verbose.write('Initializing config...').ok();
|
88 |
|
89 | data = obj || {};
|
90 |
|
91 | processDirectives();
|
92 |
|
93 | return data;
|
94 | };
|
95 |
|
96 |
|
97 | config.escape = function(str) {
|
98 | return str.replace(/\./g, '\\.');
|
99 | };
|
100 |
|
101 |
|
102 |
|
103 | config.requires = function() {
|
104 | var p = grunt.utils.pluralize;
|
105 | var props = grunt.utils.toArray(arguments).map(getPropString);
|
106 | var msg = 'Verifying propert' + p(props.length, 'y/ies') +
|
107 | ' ' + grunt.log.wordlist(props) + ' exist' + p(props.length, 's') +
|
108 | ' in config...';
|
109 | grunt.verbose.write(msg);
|
110 | var failProps = data && props.filter(function(prop) {
|
111 | return config.get(prop) === undefined;
|
112 | }).map(function(prop) {
|
113 | return '"' + prop + '"';
|
114 | });
|
115 | if (data && failProps.length === 0) {
|
116 | grunt.verbose.ok();
|
117 | return true;
|
118 | } else {
|
119 | grunt.verbose.or.write(msg);
|
120 | grunt.log.error().error('Unable to process task.');
|
121 | if (!data) {
|
122 | throw grunt.task.taskError('Unable to load config.');
|
123 | } else {
|
124 | throw grunt.task.taskError('Required config propert' +
|
125 | p(failProps.length, 'y/ies') + ' ' + failProps.join(', ') +
|
126 | ' missing.');
|
127 | }
|
128 | }
|
129 | };
|