Skip to content

Commit

Permalink
fix keyPath - create parent when it doesn't exists
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuzuhui authored and SBoudrias committed Mar 29, 2016
1 parent f0e0461 commit a37ec8d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ GruntfileEditor.prototype.insertConfig = function (name, config) {
var gruntConfig = this.gruntfile.callExpression('grunt.initConfig').arguments.at(0);
// Select nested paths
var targetConfig = name.split('.').reduce(function (nestedConfig, key) {
// If the parent key is undefined, then create it.
if (!nestedConfig.node.range) {
nestedConfig.value('{}');
}
return nestedConfig.key(key);
}, gruntConfig);

Expand Down
13 changes: 6 additions & 7 deletions test/insertconfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,22 +89,21 @@ describe('#insertConfig()', function () {
assert(this.str().indexOf(gf) < 0);
assert(this.str().indexOf(gf.replace('bar', 'baz')) >= 0);
});
});

describe('inserts sub task', function () {
it('add sub task ', function () {
// create first sub task.
this.editor.insertConfig('compass', JSON.stringify({
src1: {
files: ['./src1/**']
}
// add first sub task. create parent if not exist.
this.editor.insertConfig('compass.src1', JSON.stringify({
files: ['./src1/**']
}));

// add another sub task.
this.editor.insertConfig('compass.src2', JSON.stringify({
files: ['./src2/**']
}));

assert.equal(
this.str().replace(/\s/g, ''), "module.exports=function(grunt){'usestrict';grunt.initConfig({compass:{'src1':{'files':['./src1/**']},src2:{'files':['./src2/**']}}});};"
this.str().replace(/\s/g, ''), "module.exports=function(grunt){'usestrict';grunt.initConfig({compass:{src1:{'files':['./src1/**']},src2:{'files':['./src2/**']}}});};"
);
});
});
Expand Down

0 comments on commit a37ec8d

Please sign in to comment.