Skip to content

Commit

Permalink
Add option for dynamic updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Ollie Edwards committed May 12, 2014
1 parent b61cbf7 commit 4ff861d
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 14 deletions.
17 changes: 16 additions & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ module.exports = function (grunt) {
files: {
'tmp/assignToVar.js': ['test/fixtures/default.js']
}
},
dynamicReplacement: {
options: {
vars: {
FOO_BAR: {
key: 'foo.bar',
callback : function(newV, oldV) {
return oldV + ' ' + newV;
}
}
}
},
files: {
'tmp/dynamicReplacement.js': ['test/fixtures/default.js']
}
}
},

Expand All @@ -90,7 +105,7 @@ module.exports = function (grunt) {

// Whenever the "test" task is run, first clean the "tmp" dir, then run this
// plugin's task(s), then test the result.
grunt.registerTask('test', ['env:default', 'clean', 'envtojson', 'nodeunit', ]);
grunt.registerTask('test', ['env:default', 'clean', 'envtojson', 'nodeunit', 'clean']);

// By default, lint and run all tests.
grunt.registerTask('default', ['jshint', 'test']);
Expand Down
30 changes: 20 additions & 10 deletions tasks/envToJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = function (grunt) {
// Merge task-specific and/or target-specific options with these defaults.
options = this.options({
vars: {},
assignToVar: 'module.exports',
assignToVar: false,
whiteSpace: 4
});

Expand All @@ -38,43 +38,53 @@ module.exports = function (grunt) {
}).map(function (filepath) {
// Read file source.
var modulePath = path.resolve(filepath);
var js = require(modulePath);
writeFile(f, JSON.stringify(updateValues(js, options.vars), null, (options.whiteSpace)), options.assignToVar);
var js = _.clone(require(modulePath), true);
writeFile(f.dest, JSON.stringify(updateValues(js, options.vars), null, (options.whiteSpace)), options.assignToVar);
});
});
});

var updateValues = function (js, vars) {
_.each(vars, function (path, key) {
_.each(vars, function (replace, key) {
var val = process.env[key];
if (val) {
js = updateValue(js, path.split('.'), val) || js;
var keyPath, cb;
if (typeof replace === 'string') {
keyPath = replace;
cb = function(newV, oldV) {
return newV;
};
} else {
keyPath = replace.key;
cb = replace.callback;
}
js = updateValue(js, keyPath.split('.'), _.bind(cb, this, val)) || js;
}
});
return js;
};

var updateValue = function (object, segments, val) {
var updateValue = function (object, segments, cb) {
if (object && segments.length) {
var segment = segments.shift();
if (segments.length === 0) {
object[segment] = val;
object[segment] = cb(object[segment]);
return object;
} else {
object[segment] = updateValue((object[segment] || {}), segments, val);
object[segment] = updateValue((object[segment] || {}), segments, cb);
return object;
}
}
};

var writeFile = function(file, content, assignToVar) {
var writeFile = function(dest, content, assignToVar) {
if (assignToVar) {
content = _.template("<%=assignToVar%>=<%=content%>;")({
content: content,
assignToVar: assignToVar
});
}
grunt.file.write(file.dest, content);
grunt.file.write(dest, content);
};

};
10 changes: 10 additions & 0 deletions test/envToJson_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ exports.envtojson = {
var expected = grunt.file.read('test/expected/noWhiteSpace.js');
test.equal(actual, expected, 'plain javascript object with altered/created keys no whitespace output');

test.done();
},

dynamic: function(test) {
test.expect(1);

var actual = grunt.file.read('tmp/dynamicReplacement.js');
var expected = grunt.file.read('test/expected/dynamicReplacement.js');
test.equal(actual, expected, 'plain javascript object with first key dynamically altered');

test.done();
}
};
4 changes: 2 additions & 2 deletions test/expected/default.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports={
{
"foo": {
"bar": "altered"
},
Expand All @@ -13,4 +13,4 @@ module.exports={
"stones": "newProp"
}
}
};
}
11 changes: 11 additions & 0 deletions test/expected/dynamicReplacement.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"foo": {
"bar": "howdy altered"
},
"spam": {
"eggs": {
"parrot": "grail"
}
},
"beer": "good"
}
2 changes: 1 addition & 1 deletion test/expected/noWhiteSpace.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4ff861d

Please sign in to comment.