-
Notifications
You must be signed in to change notification settings - Fork 16
/
androidresources.js
47 lines (37 loc) · 1.18 KB
/
androidresources.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module.exports = AndroidStyles;
var _ = require('lodash');
var fs = require('fs');
var pd = require('pretty-data2').pd;
var cheerio = require('cheerio');
function AndroidStyles() { this.data = {} }
AndroidStyles.prototype.toXML = function() {
return pd.xml(this.$.xml());
}
AndroidStyles.prototype.writeFile = function(path) {
var xmlString = this.toXML();
fs.writeFileSync(path, xmlString);
return this;
}
AndroidStyles.prototype.readFile = function(path) {
var xml = fs.readFileSync(path).toString();
this.$ = cheerio.load(xml, { xmlMode: true });
return this;
}
AndroidStyles.prototype.findOrCreateByAndroidName = function(parent, tag, name) {
var manifest = this;
var $ = manifest.$;
var nodes = $(parent).find(' > '+tag);
var dupe = _.find(nodes, { attribs: { 'android:name': name } });
if (dupe) return $(dupe);
else {
var elem = $('<'+tag+'>').attr('name', name);
$(parent).append(elem);
return $(elem);
}
}
AndroidStyles.prototype.string = function(name) {
return this.findOrCreateByAndroidName('resources', 'string', name);
}
AndroidStyles.prototype.style = function(name) {
return this.findOrCreateByAndroidName('resources', 'style', name);
}