Skip to content

Instantly share code, notes, and snippets.

@aarjithn
Created February 8, 2017 08:14
Show Gist options
  • Save aarjithn/3bd33bd67bb81e3619635aaa8cb7de77 to your computer and use it in GitHub Desktop.
Save aarjithn/3bd33bd67bb81e3619635aaa8cb7de77 to your computer and use it in GitHub Desktop.
Gulp environment config
gulp.task('config', function () {
var parser = new xml2js.Parser();
var builder = new xml2js.Builder({
renderOpts: {
pretty: true,
indent: ' '
},
xmldec: {
version: '1.0',
encoding: 'utf-8'
}
});
// read & parse file
var xmlFile = fs.readFileSync('config.xml');
parser.parseString(xmlFile, function (err, result) {
// get values
if (options.getWidgetAttr) {
console.log(result.widget.$[options.getWidgetAttr]);
}
// update values
if (options.setWidgetAttr) {
var split = options.setWidgetAttr.split('=');
var key = split[0];
var value = split[1];
result.widget.$[key] = value;
}
if (options.setName) {
result.widget.name = options.setName;
}
if (options.setDescription) {
result.widget.description = options.setDescription;
}
if (options.setAuthor) {
var splits = options.setAuthor.split('---');
if (splits[0]) {
result.widget.author[0]._ = splits[0];
}
if (splits[1]) {
result.widget.author[0].$.email = splits[1];
}
if (splits[2]) {
result.widget.author[0].$.href = splits[2];
}
}
// write file
var xml = builder.buildObject(result);
fs.writeFileSync('config.xml', xml);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment