Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(config.get): process intermediate templates in path #1524

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion lib/grunt/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,28 @@ var propStringTmplRe = /^<%=\s*([a-z0-9_$]+(?:\.[a-z0-9_$]+)*)\s*%>$/i;

// Get config data, recursively processing templates.
config.get = function(prop) {
return config.process(config.getRaw(prop));
var props = getParts(config.getPropString(prop));
var currentData = config.data;
// from https://github.com/cowboy/node-getobject/blob/master/lib/getobject.js
// Split strings on dot, but only if dot isn't preceded by a backslash. Since
// JavaScript doesn't support lookbehinds, use a placeholder for "\.", split
// on dot, then replace the placeholder character with a dot.
function getParts(str) {
return str.replace(/\\\./g, '\uffff').split('.').map(function(s) {
return s.replace(/\uffff/g, '.');
});
}

props.forEach(function(item) {
currentData = currentData[item];
// if current value is template -- expand it
if (typeof currentData === 'string' &&
currentData.match(propStringTmplRe)) {
currentData = config.process(currentData);
}
});

return config.process(currentData);
};

// Expand a config value recursively. Used for post-processing raw values
Expand Down
6 changes: 5 additions & 1 deletion test/grunt/config_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ exports.config = {
bar: 'bar',
arr: ['foo', '<%= obj.foo2 %>'],
arr2: ['<%= arr %>', '<%= obj.Arr %>'],
arr3: ['<%= meta %>'],
buffer: new Buffer('test'),
});
done();
Expand Down Expand Up @@ -61,7 +62,7 @@ exports.config = {
test.done();
},
'config.get': function(test) {
test.expect(10);
test.expect(13);
test.equal(grunt.config.get('foo'), 'bar', 'Should process templates.');
test.equal(grunt.config.get('foo2'), 'bar', 'Should process templates recursively.');
test.equal(grunt.config.get('obj.foo2'), 'bar', 'Should process deeply nested templates recursively.');
Expand All @@ -73,6 +74,9 @@ exports.config = {
var buf = grunt.config.get('buffer');
test.ok(Buffer.isBuffer(buf), 'Should retrieve Buffer instances as Buffer.');
test.deepEqual(buf, new Buffer('test'), 'Should return buffers as-is.');
test.deepEqual(grunt.config.get('arr3.0'), {foo: 'bar', baz: [1, 2, 3]});
test.deepEqual(grunt.config.get('arr3.0').foo, 'bar');
test.deepEqual(grunt.config.get('arr3.0.foo'), 'bar');
test.done();
},
'config.set': function(test) {
Expand Down