-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathupdateInstallRdf.js
executable file
·70 lines (65 loc) · 2.24 KB
/
updateInstallRdf.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env node
/* global require */
(function() {
'use strict';
var fs = require('fs'),
xml2js = require('xml2js'),
Q = require('q');
var version = 'add-update-key-option-github-joscha';
var rdfFile = 'tmp/mozilla-addon-sdk/addon-sdk-'+version+'/app-extension/install.rdf';
Q.all([
Q.nfcall(fs.readFile, 'firefox/appvers.txt', 'utf8'),
Q.nfcall(fs.readFile, rdfFile, 'utf-8')
])
.then(function buildObj(data) {
return {
appvers: data[0],
rdf: data[1]
};
})
.then(function gatherVersionTuples(files) {
var tuples = {};
files.appvers.split(/\n/).forEach(function(line) {
line = line.trim();
if (!line) {
return;
}
var tuple = line.split(/\s+/);
var id = tuple[0];
var maxVersion = tuple[1];
tuples[id] = maxVersion;
});
files.appvers = tuples;
return files;
})
.then(function parseRdf(files) {
var parser = new xml2js.Parser();
return Q.nfcall(parser.parseString, files.rdf).then(function(xml) {
files.rdf = xml;
return files;
});
})
.then(function replaceMaxVersion(data) {
var targetApplications = data.rdf.RDF.Description[0]['em:targetApplication'];
targetApplications.forEach(function(targetApplication) {
var description = targetApplication.Description[0];
var id = description['em:id'][0];
if (typeof data.appvers[id] !== 'undefined') {
description['em:maxVersion'][0] = data.appvers[id];
}
});
return data.rdf;
})
.then(function buildRdf(xmlObj) {
return new xml2js.Builder().buildObject(xmlObj);
})
.then(function writeRdfFile(xml) {
return Q.nfcall(fs.writeFile, rdfFile, xml)
})
.then(function done() {
console.log('written "'+rdfFile+'"');
})
.catch(function() {
console.error(arguments);
});
})();