-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (57 loc) · 1.68 KB
/
index.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
'use strict';
var fs = require('fs');
var xml2js = require('xml2js');
var npmQ = require('q');
var semver = require('semver-utils');
function updateVersion(context) {
var Q = context ? context.requireCordovaModule('q') : npmQ;
var dfd = new Q.defer();
var parser = new xml2js.Parser();
var builder = new xml2js.Builder();
var output;
var dir;
var path;
var pkg;
var manifestExists = false;
try {
dir = context ? context.opts.projectRoot : process.cwd();
pkg = semver.parse(require(dir + '/package.json').version);
path = dir + '/config.xml';
manifestExists = fs.existsSync(path);
} catch (error) {
dfd.reject('Unable to find config.xml in path');
}
if (manifestExists) {
fs.readFile(path, function(err, data) {
parser.parseString(data, function(err, result) {
var modified = result;
var build;
function callback(err) {
if (err) {
dfd.reject('Error writing to config.xml');
} else {
dfd.resolve('Updated config.xml successfully');
}
}
try {
modified.widget.$['version'] = pkg.version;
if (pkg.build) {
build = pkg.build.replace(/\D/g,'');
modified.widget.$['android-versionCode'] = build;
modified.widget.$['ios-CFBundleVersion'] = build;
}
output = builder.buildObject(modified);
fs.writeFile(path, output, callback);
} catch (error) {
dfd.reject('Error writing to config.xml');
}
});
});
} else {
dfd.reject('Unable to find config.xml in path');
}
return dfd.promise;
}
module.exports = {
update: updateVersion
};