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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
/**
* Utilities and shared functionality for the build hooks.
*/
var fs = require('fs');
var path = require("path");
var parser = require('xml-js');
var _configXml, _pluginXml;
var Utilities = {};
fs.ensureDirSync = function(dir){
if(!fs.existsSync(dir)){
dir.split(path.sep).reduce(function(currentPath, folder){
currentPath += folder + path.sep;
if(!fs.existsSync(currentPath)){
fs.mkdirSync(currentPath);
}
return currentPath;
}, '');
}
};
Utilities.parsePackageJson = function(){
return JSON.parse(fs.readFileSync(path.resolve('./package.json')));
};
Utilities.parseConfigXml = function(){
if(_configXml) return _configXml;
_configXml = Utilities.parseXmlFileToJson("config.xml");
return _configXml;
};
Utilities.parsePluginXml = function(){
if(_pluginXml) return _pluginXml;
_pluginXml = Utilities.parseXmlFileToJson("plugins/"+Utilities.getPluginId()+"/plugin.xml");
return _pluginXml;
};
Utilities.parseXmlFileToJson = function(filepath, parseOpts){
parseOpts = parseOpts || {compact: true};
return JSON.parse(parser.xml2json(fs.readFileSync(path.resolve(filepath), 'utf-8'), parseOpts));
};
Utilities.writeJsonToXmlFile = function(jsonObj, filepath, parseOpts){
parseOpts = parseOpts || {compact: true, spaces: 4};
var xmlStr = parser.json2xml(JSON.stringify(jsonObj), parseOpts);
fs.writeFileSync(path.resolve(filepath), xmlStr);
};
/**
* Used to get the name of the application as defined in the config.xml.
*/
Utilities.getAppName = function(){
return Utilities.parseConfigXml().widget.name._text.toString().trim();
};
/**
* The ID of the plugin; this should match the ID in plugin.xml.
*/
Utilities.getPluginId = function(){
return "cordova-plugin-firebasex";
};
Utilities.copyKey = function(platform){
for(var i = 0; i < platform.src.length; i++){
var file = platform.src[i];
if(this.fileExists(file)){
try{
var contents = fs.readFileSync(path.resolve(file)).toString();
try{
var destinationPath = platform.dest;
var folder = destinationPath.substring(0, destinationPath.lastIndexOf('/'));
fs.ensureDirSync(folder);
fs.writeFileSync(path.resolve(destinationPath), contents);
}catch(e){
// skip
}
}catch(err){
console.log(err);
}
break;
}
}
};
Utilities.fileExists = function(filePath){
try{
return fs.statSync(path.resolve(filePath)).isFile();
}catch(e){
return false;
}
};
Utilities.directoryExists = function(dirPath){
try{
return fs.statSync(path.resolve(dirPath)).isDirectory();
}catch(e){
return false;
}
};
Utilities.log = function(msg){
console.log(Utilities.getPluginId()+': '+msg);
};
module.exports = Utilities;
|