forked from arnesson/cordova-plugin-firebase
-
-
Notifications
You must be signed in to change notification settings - Fork 458
/
utilities.js
164 lines (140 loc) · 4.97 KB
/
utilities.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/**
* Utilities and shared functionality for the build hooks.
*/
var fs = require('fs');
var path = require("path");
var parser = require('xml-js');
var _configXml, _pluginXml, _context, _pluginVariables;
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.setContext = function(context){
_context = context;
};
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(){
// if(!_context) throw "Cannot retrieve plugin ID as hook context is not set";
return _context.opts.plugin.id;
};
Utilities.parsePluginVariables = function(){
if(_pluginVariables) return _pluginVariables;
var pluginVariables = {};
// Parse plugin.xml
var plugin = Utilities.parsePluginXml();
var prefs = [];
if(plugin.plugin.preference){
prefs = prefs.concat(plugin.plugin.preference);
}
if(typeof plugin.plugin.platform.length === 'undefined') plugin.plugin.platform = [plugin.plugin.platform];
plugin.plugin.platform.forEach(function(platform){
if(platform.preference){
prefs = prefs.concat(platform.preference);
}
});
prefs.forEach(function(pref){
if (pref._attributes){
pluginVariables[pref._attributes.name] = pref._attributes.default;
}
});
// Parse config.xml
var config = Utilities.parseConfigXml();
(config.widget.plugin ? [].concat(config.widget.plugin) : []).forEach(function(plugin){
(plugin.variable ? [].concat(plugin.variable) : []).forEach(function(variable){
if((plugin._attributes.name === Utilities.getPluginId() || plugin._attributes.id === Utilities.getPluginId()) && variable._attributes.name && variable._attributes.value){
pluginVariables[variable._attributes.name] = variable._attributes.value;
}
});
});
// Parse package.json
var packageJSON = Utilities.parsePackageJson();
if(packageJSON.cordova && packageJSON.cordova.plugins){
for(const pluginId in packageJSON.cordova.plugins){
if(pluginId === Utilities.getPluginId()){
for(const varName in packageJSON.cordova.plugins[pluginId]){
var varValue = packageJSON.cordova.plugins[pluginId][varName];
pluginVariables[varName] = varValue;
}
}
}
}
_pluginVariables = pluginVariables;
return pluginVariables;
};
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;