Created
February 8, 2017 08:14
-
-
Save aarjithn/3bd33bd67bb81e3619635aaa8cb7de77 to your computer and use it in GitHub Desktop.
Gulp environment config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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